I am trying to implement a friend/followers function to my app. The most straightforward way seemed to be to create and update a local friends list. That being said, on my Home View I want to show the user's friends.
I tried to go about this by creating a list of cells for each user, but then filtering this by checking if each FireStore user is contained in the local Friends.list. When running the app there aren't any cells that show up regardless of if any users are added. I also print the Friends.list onAppear of the view to make sure the friends are being added. I'll also note that I did try to pre-load Friends.list with user and the cells did show up, but then the ui was not dynamic because when I un-added the user their cell did not go away. That being said I suspect this is an issue with the type of var I am using and I need a wrapper or something.
Thank you for any help!
The Friends class with list variable:
import Foundation
class Friends: ObservableObject {
static var list: [String] = []
}
Relevant HomeView code:
NavigationStack {
VStack {
ScrollView(showsIndicators: false) {
LazyVStack {
ForEach(viewModel.users) { user in
if (Friends.list.contains(user.id)) {
NavigationLink(value: user) {
VStack {
FriendCell(user: user)
}
...
The add button which appends and removes user id's from Friends.list:
var user: User
func addFriend(userId: String) {
if Friends.list.contains(userId) {
if let index = Friends.list.firstIndex(of: userId) {
Friends.list.remove(at: index)
}
} else {
Friends.list.append(userId)
print(Friends.list)
}
}
Button {
addFriend(userId: user.id)
} label: {
...