diff --git a/nirvana-ios/Models/User.swift b/nirvana-ios/Models/User.swift index 3a2c842..c824f3a 100644 --- a/nirvana-ios/Models/User.swift +++ b/nirvana-ios/Models/User.swift @@ -10,6 +10,12 @@ import Firebase import FirebaseFirestore import FirebaseFirestoreSwift +enum UserStatus: String, Codable { + case online + case offline + case background +} + struct User: Identifiable, Codable { @DocumentID var id: String? var nickname: String? @@ -17,6 +23,8 @@ struct User: Identifiable, Codable { var emailAddress:String? var avatar:String? var deviceToken: String? + + var userStatus: UserStatus? @ServerTimestamp var lastLoggedInTimestamp: Date? @ServerTimestamp var createdTimestamp: Date? diff --git a/nirvana-ios/Repositories/FirestoreService.swift b/nirvana-ios/Repositories/FirestoreService.swift index 82308da..9a7b480 100644 --- a/nirvana-ios/Repositories/FirestoreService.swift +++ b/nirvana-ios/Repositories/FirestoreService.swift @@ -155,7 +155,10 @@ class FirestoreService { completion(ServiceState.error(ServiceError(description: error.localizedDescription))) } } - +} + +// updates to user +extension FirestoreService { func updateUserDeviceToken(userId: String, deviceToken: String, completion: @escaping((_ state: ServiceState) -> ())) { do { if userId != nil { @@ -170,4 +173,13 @@ class FirestoreService { } } + func updateUserStatus(userId: String, userStatus: UserStatus, completion: @escaping((_ state: ServiceState) -> ())) { + do { + let _ = try db.collection(Collection.users.rawValue).document(userId).setData(["userStatus": userStatus.rawValue], merge: true) + completion(ServiceState.success("Updated user status")) + } catch { + print("error in updating user \(error.localizedDescription)") + completion(ServiceState.error(ServiceError(description: error.localizedDescription))) + } + } } diff --git a/nirvana-ios/Services/AuthSessionStore.swift b/nirvana-ios/Services/AuthSessionStore.swift index fc79324..0a92dd3 100644 --- a/nirvana-ios/Services/AuthSessionStore.swift +++ b/nirvana-ios/Services/AuthSessionStore.swift @@ -411,3 +411,28 @@ extension AuthSessionStore { self.listenersActive = false } } + + +// manage whether user is online or not +extension AuthSessionStore { + func updateUserStatus(userStatus: UserStatus) { + // if user is authenticated + if self.sessionState == .isAuthenticated { + // if user already has the status that we want to update to, no need to update + // remember that the user is updated realtime so we have the newest user data + if self.user?.userStatus == userStatus { + // do nothing...already have this status + print("user already has status: \(userStatus)") + } + else { + if let uid = self.getCurrentUserId() { + self.firestoreService.updateUserStatus(userId: uid, userStatus: userStatus) {res in + print(res) + } + } + } + } + + print("not authenticated can't change user status") + } +} diff --git a/nirvana-ios/Views/InnerCircle/InnerCircleView.swift b/nirvana-ios/Views/InnerCircle/InnerCircleView.swift index 3074fa3..539fd1e 100644 --- a/nirvana-ios/Views/InnerCircle/InnerCircleView.swift +++ b/nirvana-ios/Views/InnerCircle/InnerCircleView.swift @@ -135,11 +135,16 @@ struct InnerCircleView: View { } .onAppear { // activate 3 data listeners once for authsessionstore/usermanager if not already called, but authsessionstore will handle that + // TODO: can/should do this on init of view model self.authSessionStore.activateMainDataListeners() // set up push notifications and such + save up to date device token // TODO: this is firing too often? self.innerCircleVM.setUpPushNotifications() + + // set status of user to online + self.authSessionStore.updateUserStatus(userStatus: .online) + } // .onDisappear { // print("deiniting data listeners, but current data should still be cached!") diff --git a/nirvana-ios/Views/Router/RouterView.swift b/nirvana-ios/Views/Router/RouterView.swift index e34e6d8..67e8d48 100644 --- a/nirvana-ios/Views/Router/RouterView.swift +++ b/nirvana-ios/Views/Router/RouterView.swift @@ -36,11 +36,14 @@ struct RouterView: View { if newPhase == .inactive { print("user is offline") // set firestore user document isOnline to false + self.authSessionStore.updateUserStatus(userStatus: .offline) } else if newPhase == .active { print("user is online again") // set firestore user document isOnline to true + self.authSessionStore.updateUserStatus(userStatus: .online) } else if newPhase == .background { print("app is in backgroun") + self.authSessionStore.updateUserStatus(userStatus: .background) } }