updating in the right spots
This commit is contained in:
@@ -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?
|
||||
|
||||
@@ -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)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user