How can I make a List show a set of selected items when editMode is .inactive and all selectable options when editMode is .active so the user can change the set of selected items? Here is what I have tried:
import SwiftUI
struct SelectionView: View {
@Environment(\.editMode) var editMode
@State var selectedItems = Set<String>(["1-item", "2-item", "3-item", "4-item"])
let allItems = ["1-item", "2-item", "3-item", "4-item"]
var items: [String] {
if editMode?.wrappedValue == .inactive {
return Array(selectedItems)
}
else {
return allItems
}
}
var body: some View {
NavigationView {
List(items, id: \.self, selection: $selectedItems) { item in
Text(item)
}
.navigationBarItems(trailing: EditButton())
.navigationBarTitle("Items")
}
}
}
The selected array is now unordered as it is generated from a set and I eventually want to have it in the correct order. But I'm first trying to get the list to work as it is.