attempting to get working the killing of the app during a call
This commit is contained in:
@@ -276,7 +276,7 @@ extension AuthSessionStore {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a listener for this specific user
|
// create a listener for this specific friend if they are an active friend
|
||||||
let currFriendListener = self.db.collection("users").document(userFriend!.friendId)
|
let currFriendListener = self.db.collection("users").document(userFriend!.friendId)
|
||||||
.addSnapshotListener {documentSnapshot, error in
|
.addSnapshotListener {documentSnapshot, error in
|
||||||
guard let document = documentSnapshot else {
|
guard let document = documentSnapshot else {
|
||||||
|
|||||||
@@ -389,26 +389,10 @@ class ConvoViewModel: NSObject, ObservableObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var updatedConvo = convo
|
// handle edge cases
|
||||||
//update convo in database to show that I left...if I am the last person, also close out the channel
|
let updatedConvo = Self.updatedConvoOnLeaving(convo: convo!, userId: userId!)
|
||||||
|
|
||||||
// if I am the last one in the convo, then end the convo in the db
|
self?.firestoreService.updateConvo(convo: updatedConvo) {[weak self] res in
|
||||||
if updatedConvo!.users.count == 1 && updatedConvo!.users[0] == userId {
|
|
||||||
updatedConvo!.state = .complete
|
|
||||||
updatedConvo!.endedTimestamp = Date()
|
|
||||||
} // if I am second to last, the last guy is a loner and activate him to get out
|
|
||||||
else if updatedConvo!.users.count == 2 && updatedConvo!.users.contains(userId!) {
|
|
||||||
updatedConvo!.secondToLastUserEndedTimestamp = Date()
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove me from the users array to omit me from the convo
|
|
||||||
let updatedUsers: [String] = updatedConvo!.users.filter{ arrUserId in
|
|
||||||
return arrUserId != userId
|
|
||||||
}
|
|
||||||
|
|
||||||
updatedConvo!.users = updatedUsers
|
|
||||||
|
|
||||||
self?.firestoreService.updateConvo(convo: updatedConvo!) {[weak self] res in
|
|
||||||
self?.selectedConvoId = nil
|
self?.selectedConvoId = nil
|
||||||
|
|
||||||
print("left convo officially in our db as well")
|
print("left convo officially in our db as well")
|
||||||
@@ -422,6 +406,75 @@ class ConvoViewModel: NSObject, ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** force leave for when the user quits a session and is in a call */
|
||||||
|
static func leaveAnyConvo() {
|
||||||
|
print("leaving any convos user may be in")
|
||||||
|
|
||||||
|
let userId = AuthSessionStore.getCurrentUserId()
|
||||||
|
|
||||||
|
if userId == nil {
|
||||||
|
print("not authenticated...can't leave any channels")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let firestoreService = FirestoreService()
|
||||||
|
var db = Firestore.firestore()
|
||||||
|
|
||||||
|
// get all live convos where I am in the users array
|
||||||
|
db.collection("convos").whereField("state", isEqualTo: ConvoState.active.rawValue).whereField("users", arrayContains: userId)
|
||||||
|
.getDocuments() {(querySnapshot, err) in
|
||||||
|
if let err = err {
|
||||||
|
print("Error getting user's active friends: \(err)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// for each...prolly just one...remove me from the list of users
|
||||||
|
for document in querySnapshot!.documents {
|
||||||
|
var convo:Convo? = try? document.data(as: Convo.self)
|
||||||
|
|
||||||
|
if convo == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
print(convo)
|
||||||
|
|
||||||
|
// handle edge cases
|
||||||
|
let updatedConvo = self.updatedConvoOnLeaving(convo: convo!, userId: userId!)
|
||||||
|
|
||||||
|
// send update back to firestore
|
||||||
|
firestoreService.updateConvo(convo: updatedConvo) {res in
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
AgoraRtcEngineKit.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static func updatedConvoOnLeaving(convo: Convo, userId: String) -> Convo {
|
||||||
|
var updatedConvo = convo
|
||||||
|
//update convo in database to show that I left...if I am the last person, also close out the channel
|
||||||
|
|
||||||
|
// if I am the last one in the convo, then end the convo in the db
|
||||||
|
if updatedConvo.users.count == 1 && updatedConvo.users[0] == userId {
|
||||||
|
updatedConvo.state = .complete
|
||||||
|
updatedConvo.endedTimestamp = Date()
|
||||||
|
} // if I am second to last, the last guy is a loner and activate him to get out
|
||||||
|
else if updatedConvo.users.count == 2 && updatedConvo.users.contains(userId) {
|
||||||
|
updatedConvo.secondToLastUserEndedTimestamp = Date()
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove me from the users array to omit me from the convo
|
||||||
|
let updatedUsers: [String] = updatedConvo.users.filter{ arrUserId in
|
||||||
|
return arrUserId != userId
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedConvo.users = updatedUsers
|
||||||
|
|
||||||
|
return updatedConvo
|
||||||
|
}
|
||||||
|
|
||||||
func destroyInstance() {
|
func destroyInstance() {
|
||||||
AgoraRtcEngineKit.destroy()
|
AgoraRtcEngineKit.destroy()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,6 +143,15 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
|||||||
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
|
||||||
print("failed to register for remote notifications with error: \(error)")
|
print("failed to register for remote notifications with error: \(error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** when user kills the app process completely if it is in the background */
|
||||||
|
func applicationWillTerminate(_ application: UIApplication) {
|
||||||
|
// TODO: get user off of all live convos
|
||||||
|
|
||||||
|
ConvoViewModel.leaveAnyConvo()
|
||||||
|
|
||||||
|
print("user killed app, leaving convos if in any")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cloud messaging
|
// cloud messaging
|
||||||
|
|||||||
Reference in New Issue
Block a user