basic status showing based on friend status

This commit is contained in:
talksik
2021-12-28 16:36:40 -08:00
parent df801a4433
commit b60f7e77b3
4 changed files with 66 additions and 30 deletions
+53 -26
View File
@@ -42,6 +42,8 @@ final class AuthSessionStore: ObservableObject, SessionStore {
@Published var relevantUsersDict: [String: User] = [:] // all cached/snapshotted users from db for app use
@Published var relevantMessagesByUserDict: [String: [Message]] = [:] // note, this is all messages related to me
private var friendsListeners: [ListenerRegistration] = []
private var dataListeners: [ListenerRegistration] = []
private var listenersActive = false // false on app/processes killed
@@ -126,7 +128,7 @@ final class AuthSessionStore: ObservableObject, SessionStore {
do {
try Auth.auth().signOut()
self.deinitDataListeners()
self.deinitAllDataListeners()
} catch let signOutError as NSError {
print("Error signing out: %@", signOutError)
}
@@ -243,6 +245,9 @@ extension AuthSessionStore {
self.userFriendsDict.removeAll()
self.friendsArr.removeAll() // updating to keep the friends in order
// clear the listeners for the the previous set of users
self.deinitFriendListeners()
for document in querySnapshot!.documents {
let userFriend:UserFriends? = try? document.data(as: UserFriends.self)
@@ -252,37 +257,49 @@ extension AuthSessionStore {
self.userFriendsDict[userFriend!.friendId] = userFriend!
// get user and initialize this user in dict
self.db.collection("users").document(userFriend!.friendId).getDocument { (document, error) in
if let document = document, document.exists {
let returnedUser = try? document.data(as: User.self)
// making sure that I clear the inbox if this user came from the inbox...
// could be an accepted or rejected user
if self.inboxUsersArr.contains(userFriend!.friendId) {
self.inboxUsersArr = self.inboxUsersArr.filter {inboxUserId in
return inboxUserId != userFriend!.friendId
}
}
// only get friend's info if is an active friend
if !userFriend!.isActive {
continue
}
// create a listener for this specific user
let currFriendListener = self.db.collection("users").document(userFriend!.friendId)
.addSnapshotListener {documentSnapshot, error in
guard let document = documentSnapshot else {
print("Error fetching friend's data: \(error!)")
return
}
DispatchQueue.main.async {
if returnedUser != nil {
self.relevantUsersDict[userFriend!.friendId] = returnedUser!
// making sure that I clear the inbox if this user came from the inbox...regardless of
// whether I accepted or rejected them
if self.inboxUsersArr.contains(userFriend!.friendId) {
self.inboxUsersArr = self.inboxUsersArr.filter {inboxUserId in
return inboxUserId != userFriend!.friendId
}
}
if userFriend!.isActive {
self.friendsArr.append(userFriend!.friendId)
}
guard let data = document.data() else {
print("friend realtime data was empty.")
return
}
let updatedReturnedUser = try? document.data(as: User.self)
if updatedReturnedUser != nil {
DispatchQueue.main.async {
self.relevantUsersDict[userFriend!.friendId] = updatedReturnedUser!
self.friendsArr.append(userFriend!.friendId)
self.objectWillChange.send()
print("added this user to the array of users for user's circle\(returnedUser?.nickname)")
print("added this user to the array of users for user's circle\(updatedReturnedUser?.nickname)")
}
}
} else {
print("user doesn't exist from user friend relationship")
// should not happen
}
}
// adding to list of current friend listeners
self.friendsListeners.append(currFriendListener)
}
}
@@ -401,11 +418,21 @@ extension AuthSessionStore {
}
func deinitDataListeners() {
func deinitFriendListeners() {
for listener in self.friendsListeners {
listener.remove()
}
}
func deinitAllDataListeners() {
// get rid of the main listeners
for listener in self.dataListeners {
listener.remove()
}
// get rid of the inner listeners
self.deinitFriendListeners()
print("deactivated listeners!")
self.listenersActive = false
@@ -80,8 +80,15 @@ struct CircleGridView: View {
.blur(radius: 8)
.cornerRadius(100)
// check if the last message in the conversation between me and my friend was me talking or him
if self.haveNewMessageFromFriend(friendDbId: friendId) { // him talking
// this friend is online
if self.authSessionStore.relevantUsersDict[friendId]?.userStatus == .online {
Circle()
.frame(width: 20, height: 20)
.foregroundColor(Color.green)
.font(.title2)
.padding(5)
}
else if self.haveNewMessageFromFriend(friendDbId: friendId) { // check if the last message in the conversation between me and my friend was me talking or him
Image(systemName: "arrow.down.left.circle.fill")
.foregroundColor(Color.orange)
.font(.title2)
@@ -370,6 +377,8 @@ extension CircleGridView {
extension CircleGridView {
// listening to messages
private func handleTap(gridItemIndex: Int, friendId: String) {
// TODO: if there is a message, then listen to it
// then after that, check if friend is online, and if so, then connect with him/her if they are free/not in another call
print("tap gesture activated")
// clearing the player to make room for this friend's convo or to deselect this user
@@ -95,7 +95,7 @@ struct CircleNavigationView: View {
.frame(width: 40, height: 40)
.clipShape(Circle())
.shadow(radius: 10)
.overlay(alignment: .bottomTrailing) {
.overlay(alignment: .topTrailing) {
// user status
switch self.authSessionStore.user?.userStatus {
case .online:
@@ -149,7 +149,7 @@ struct InnerCircleView: View {
// print("deiniting data listeners, but current data should still be cached!")
//
// // deactivate all data listeners
// self.authSessionStore.deinitDataListeners()
// self.authSessionStore.deinitAllDataListeners()
// }
}
}