Last active
August 5, 2022 00:44
-
-
Save hmlongco/bf44dbd6a86e8f989cb98782a5040fa3 to your computer and use it in GitHub Desktop.
External Properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ToDo: Codable, Identifiable { | |
let id: Int | |
let userId: Int | |
var title: String | |
var completed: Bool | |
} | |
class SelectionManager<Item: Identifiable>: ObservableObject { | |
@Published var status: [Item.ID:Bool] = [:] | |
func isSelected(_ item: Item) -> Bool { | |
status[item.id] ?? false | |
} | |
func toggle(_ item: Item) { | |
status[item.id] = !isSelected(item) | |
} | |
func allSelected(_ items: [Item]) -> [Item] { | |
items.filter { self.isSelected($0) } | |
} | |
func clear() { | |
status = [:] | |
} | |
} | |
struct SomeView: View { | |
let todo: ToDo | |
@EnvironmentObject var selections: SelectionManager<ToDo> | |
var body: some View { | |
VStack { | |
Text("\(todo.title) is \(selections.isSelected(todo) ? "" : "not ") selected") | |
Button("Toggle") { | |
selections.toggle(todo) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So one can also do something like this if you ask yourself the question:
In this case, is "selected" actually a property of the item, or is it a distinct and separate state maintained "about" the item (or items)?
Maintaining state separately could make it easier to save/restore the entire list of selections, clear them, and so on, without having to mutate an item from its persistent state to its "internal" state.