basic status showing based on friend status
This commit is contained in:
@@ -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 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
|
@Published var relevantMessagesByUserDict: [String: [Message]] = [:] // note, this is all messages related to me
|
||||||
|
|
||||||
|
private var friendsListeners: [ListenerRegistration] = []
|
||||||
|
|
||||||
private var dataListeners: [ListenerRegistration] = []
|
private var dataListeners: [ListenerRegistration] = []
|
||||||
private var listenersActive = false // false on app/processes killed
|
private var listenersActive = false // false on app/processes killed
|
||||||
|
|
||||||
@@ -126,7 +128,7 @@ final class AuthSessionStore: ObservableObject, SessionStore {
|
|||||||
do {
|
do {
|
||||||
try Auth.auth().signOut()
|
try Auth.auth().signOut()
|
||||||
|
|
||||||
self.deinitDataListeners()
|
self.deinitAllDataListeners()
|
||||||
} catch let signOutError as NSError {
|
} catch let signOutError as NSError {
|
||||||
print("Error signing out: %@", signOutError)
|
print("Error signing out: %@", signOutError)
|
||||||
}
|
}
|
||||||
@@ -243,6 +245,9 @@ extension AuthSessionStore {
|
|||||||
self.userFriendsDict.removeAll()
|
self.userFriendsDict.removeAll()
|
||||||
self.friendsArr.removeAll() // updating to keep the friends in order
|
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 {
|
for document in querySnapshot!.documents {
|
||||||
let userFriend:UserFriends? = try? document.data(as: UserFriends.self)
|
let userFriend:UserFriends? = try? document.data(as: UserFriends.self)
|
||||||
|
|
||||||
@@ -252,37 +257,49 @@ extension AuthSessionStore {
|
|||||||
|
|
||||||
self.userFriendsDict[userFriend!.friendId] = userFriend!
|
self.userFriendsDict[userFriend!.friendId] = userFriend!
|
||||||
|
|
||||||
// get user and initialize this user in dict
|
// making sure that I clear the inbox if this user came from the inbox...
|
||||||
self.db.collection("users").document(userFriend!.friendId).getDocument { (document, error) in
|
// could be an accepted or rejected user
|
||||||
if let document = document, document.exists {
|
if self.inboxUsersArr.contains(userFriend!.friendId) {
|
||||||
let returnedUser = try? document.data(as: User.self)
|
self.inboxUsersArr = self.inboxUsersArr.filter {inboxUserId in
|
||||||
|
return inboxUserId != userFriend!.friendId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
// only get friend's info if is an active friend
|
||||||
if returnedUser != nil {
|
if !userFriend!.isActive {
|
||||||
self.relevantUsersDict[userFriend!.friendId] = returnedUser!
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// making sure that I clear the inbox if this user came from the inbox...regardless of
|
// create a listener for this specific user
|
||||||
// whether I accepted or rejected them
|
let currFriendListener = self.db.collection("users").document(userFriend!.friendId)
|
||||||
if self.inboxUsersArr.contains(userFriend!.friendId) {
|
.addSnapshotListener {documentSnapshot, error in
|
||||||
self.inboxUsersArr = self.inboxUsersArr.filter {inboxUserId in
|
guard let document = documentSnapshot else {
|
||||||
return inboxUserId != userFriend!.friendId
|
print("Error fetching friend's data: \(error!)")
|
||||||
}
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if userFriend!.isActive {
|
guard let data = document.data() else {
|
||||||
self.friendsArr.append(userFriend!.friendId)
|
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()
|
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 {
|
for listener in self.dataListeners {
|
||||||
listener.remove()
|
listener.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get rid of the inner listeners
|
||||||
|
self.deinitFriendListeners()
|
||||||
|
|
||||||
print("deactivated listeners!")
|
print("deactivated listeners!")
|
||||||
|
|
||||||
self.listenersActive = false
|
self.listenersActive = false
|
||||||
|
|||||||
@@ -80,8 +80,15 @@ struct CircleGridView: View {
|
|||||||
.blur(radius: 8)
|
.blur(radius: 8)
|
||||||
.cornerRadius(100)
|
.cornerRadius(100)
|
||||||
|
|
||||||
// check if the last message in the conversation between me and my friend was me talking or him
|
// this friend is online
|
||||||
if self.haveNewMessageFromFriend(friendDbId: friendId) { // him talking
|
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")
|
Image(systemName: "arrow.down.left.circle.fill")
|
||||||
.foregroundColor(Color.orange)
|
.foregroundColor(Color.orange)
|
||||||
.font(.title2)
|
.font(.title2)
|
||||||
@@ -370,6 +377,8 @@ extension CircleGridView {
|
|||||||
extension CircleGridView {
|
extension CircleGridView {
|
||||||
// listening to messages
|
// listening to messages
|
||||||
private func handleTap(gridItemIndex: Int, friendId: String) {
|
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")
|
print("tap gesture activated")
|
||||||
|
|
||||||
// clearing the player to make room for this friend's convo or to deselect this user
|
// 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)
|
.frame(width: 40, height: 40)
|
||||||
.clipShape(Circle())
|
.clipShape(Circle())
|
||||||
.shadow(radius: 10)
|
.shadow(radius: 10)
|
||||||
.overlay(alignment: .bottomTrailing) {
|
.overlay(alignment: .topTrailing) {
|
||||||
// user status
|
// user status
|
||||||
switch self.authSessionStore.user?.userStatus {
|
switch self.authSessionStore.user?.userStatus {
|
||||||
case .online:
|
case .online:
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ struct InnerCircleView: View {
|
|||||||
// print("deiniting data listeners, but current data should still be cached!")
|
// print("deiniting data listeners, but current data should still be cached!")
|
||||||
//
|
//
|
||||||
// // deactivate all data listeners
|
// // deactivate all data listeners
|
||||||
// self.authSessionStore.deinitDataListeners()
|
// self.authSessionStore.deinitAllDataListeners()
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user