HomeiOS Developmentios - Tapping Button in ForEach all the time selects final button

ios – Tapping Button in ForEach all the time selects final button


I’m utilizing a category to retailer an array of colours and persist the index of that array comparable to a person chosen coloration in UserDefaults.

class AccentColorManager: ObservableObject {
    
    let colours: [Color] = [.orange, .yellow, .green, .blue, .purple, .brown, .cyan, .indigo, .mint, .teal]

    @Printed var accentColor: Shade = .orange // on preliminary launch orange is default coloration

    @Printed var accentColorIndex: Int = 0 {
        didSet {
            accentColor = colours[accentColorIndex]
            saveAccentColorIndex()
            objectWillChange.ship()
        }
    }

    init() {
        loadAccentColorIndex()
    }
    
    non-public let accentColorIndexKey = "AccentColorIndex"
    
    non-public func saveAccentColorIndex() {
        UserDefaults.commonplace.set(accentColorIndex, forKey: accentColorIndexKey)
    }

    non-public func loadAccentColorIndex() {
        if let storedIndex = UserDefaults.commonplace.worth(forKey: accentColorIndexKey) as? Int {
            accentColorIndex = storedIndex
        }
    }
}

The view beneath presents the person with an inventory of colours. No matter what coloration the person selects, the final listing merchandise is all the time chosen, and subsequent choices haven’t any impact.

struct ActiveColorSectionView: View {
    @EnvironmentObject var accentColorManager: AccentColorManager

    var physique: some View {
        Part(header: Textual content("Lively Shade")) {
            VStack(alignment: .main) {
                ForEach(accentColorManager.colours.indices, id: .self) { index in
                    Button(motion: {
                        accentColorManager.accentColorIndex = index
                    }) {
                        HStack {
                            Textual content(accentColorManager.colours[index].description)
                            Spacer()
                            Circle()
                                .body(width: 20, top: 20)
                                .foregroundColor(accentColorManager.accentColorIndex == index ? accentColorManager.colours[index] : .clear)
                                .overlay(
                                    Circle()
                                        .stroke(Shade.major, lineWidth: 2)
                                )
                        }
                    }
                    .foregroundColor(accentColorManager.colours[index])
                    .padding(.vertical, 5)
                }
            }
        }
        .listRowBackground(Shade(UIColor.systemBackground))
    }
}

I think that my use of utilizing an index array could also be problematic right here, however I can’t see the precise downside and this appears extra acceptable than saving a Shade kind to UserDefaults.

Moreover, I’m utilizing the AccentColorManager class as an setting variable:

@essential
struct my_app: App {
    @StateObject non-public var accentColorManager = AccentColorManager()

    var physique: some Scene {
        WindowGroup {
            WelcomeView()
                .environmentObject(accentColorManager)
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments