Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Last active August 5, 2022 00:44
Show Gist options
  • Save hmlongco/bf44dbd6a86e8f989cb98782a5040fa3 to your computer and use it in GitHub Desktop.
Save hmlongco/bf44dbd6a86e8f989cb98782a5040fa3 to your computer and use it in GitHub Desktop.
External Properties
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)
}
}
}
}
@hmlongco
Copy link
Author

hmlongco commented Aug 5, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment