I am attempting to create a favorites functionality using a List with two ForEach loops. When I click on the button, the item moves to the correct section, but the button image and functionality are not updated. Is there something I am missing or doing incorrectly?
If I navigate to another view and back, the list is rendered correctly initially, but still exhibits the same behavior if I click on the button.
If I use two separate lists with exactly the same setup, everything works fine, but that looks strange in the UI because each list takes up the same amount of room even if there are no items in the list.
Alternatively, is there a way to force the list to redraw as part of the button tap?
Thanks!
import SwiftUI
struct BusinessListView: View {
@EnvironmentObject var state: ApplicationState
var body: some View {
VStack{
List {
Section(header: Text("Favorites")) {
if(state.favorites.count == 0){
Text("No Favorites")
}
else {
ForEach(state.favorites, id: \.self) { favorite in
HStack{
Button(
action: {},
label: { Image(systemName: "heart.fill").foregroundColor(.red) }
)
.onTapGesture { self.toggleFavorite(business: favorite)}
Text("\(favorite.name)")
}
}
}
}
Section(header: Text("Other Businesses")) {
ForEach(state.others, id: \.self) { business in
HStack{
Button(
action: {},
label: { Image(systemName: "heart") }
)
.onTapGesture { self.toggleFavorite(business: business)}
Text("\(business.name)")
}
}
}
}
}
}
func toggleFavorite(business: Business){
if(state.favorites.contains(business)){
self.state.favorites = self.state.favorites.filter {$0 != business}
self.state.others.append(business)
}
else{
self.state.others = self.state.others.filter {$0 != business}
self.state.favorites.append(business)
}
sortBusinesses()
}
func sortBusinesses(){
self.state.favorites.sort {
$0.name < $1.name
}
self.state.others.sort {
$0.name < $1.name
}
}
}
