cleanup seems to be working
This commit is contained in:
@@ -188,6 +188,20 @@ extension FirestoreService {
|
|||||||
|
|
||||||
// convos
|
// convos
|
||||||
extension FirestoreService {
|
extension FirestoreService {
|
||||||
|
func getConvo(channelName: String, completion: @escaping((_ user: Convo?) -> ())) {
|
||||||
|
let docRef = db.collection(Collection.convos.rawValue).document(channelName)
|
||||||
|
|
||||||
|
docRef.getDocument { (document, error) in
|
||||||
|
if let document = document, document.exists {
|
||||||
|
let returnedConvo = try? document.data(as: Convo.self)
|
||||||
|
completion(returnedConvo)
|
||||||
|
} else {
|
||||||
|
print("convo doesn't exist")
|
||||||
|
completion(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createConvo(convo: Convo, completion: @escaping((_ state: ServiceState) -> ())) {
|
func createConvo(convo: Convo, completion: @escaping((_ state: ServiceState) -> ())) {
|
||||||
do {
|
do {
|
||||||
let _ = try db.collection(Collection.convos.rawValue).document(convo.id!).setData(from: convo)
|
let _ = try db.collection(Collection.convos.rawValue).document(convo.id!).setData(from: convo)
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ class ConvoViewModel: NSObject, ObservableObject {
|
|||||||
var convosListener: ListenerRegistration? = nil
|
var convosListener: ListenerRegistration? = nil
|
||||||
|
|
||||||
let agoraService = AgoraService()
|
let agoraService = AgoraService()
|
||||||
var agoraKit: AgoraRtcEngineKit?
|
// TODO: put the value in environment variables
|
||||||
|
lazy var agoraKit: AgoraRtcEngineKit? = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: self)
|
||||||
|
|
||||||
private var allConvos: [Convo] = []
|
private var allConvos: [Convo] = []
|
||||||
@Published var relevantConvos: [Convo] = []
|
@Published var relevantConvos: [Convo] = []
|
||||||
private var currConvo: Convo? = nil // local cache for updating
|
|
||||||
|
|
||||||
func isInCall() -> Bool {
|
func isInCall() -> Bool {
|
||||||
return self.selectedConvoId != nil
|
return self.selectedConvoId != nil
|
||||||
@@ -175,16 +175,12 @@ class ConvoViewModel: NSObject, ObservableObject {
|
|||||||
self.leaveConvo()
|
self.leaveConvo()
|
||||||
}
|
}
|
||||||
|
|
||||||
// add this convo for the delegate use
|
|
||||||
self.currConvo = convo
|
|
||||||
|
|
||||||
// update ui to select this one
|
// update ui to select this one
|
||||||
self.selectedConvoId = convo.id
|
self.selectedConvoId = convo.id
|
||||||
|
|
||||||
self.initializeAgoraEngine() // give delegate up to date object
|
|
||||||
|
|
||||||
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
|
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
|
||||||
// finally join channel
|
// finally join channel
|
||||||
|
self.agoraKit?.delegate = self
|
||||||
self.agoraKit?.joinChannel(byToken: convo.agoraToken, channelId: convo.id!, info: nil, uid: 0, joinSuccess: nil)
|
self.agoraKit?.joinChannel(byToken: convo.agoraToken, channelId: convo.id!, info: nil, uid: 0, joinSuccess: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,38 +207,81 @@ class ConvoViewModel: NSObject, ObservableObject {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.currConvo = convo
|
|
||||||
|
|
||||||
// TODO: if the convo has more than 10 people, stop user from joining...that's too expensive...
|
// TODO: if the convo has more than 10 people, stop user from joining...that's too expensive...
|
||||||
|
|
||||||
let convoAgoraToken:String = convo!.agoraToken
|
let convoAgoraToken:String = convo!.agoraToken
|
||||||
let channelName:String = convo!.id!
|
let channelName:String = convo!.id!
|
||||||
|
|
||||||
self.initializeAgoraEngine()
|
|
||||||
|
|
||||||
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
|
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
|
||||||
// finally join channel
|
// finally join channel
|
||||||
|
self.agoraKit?.delegate = self
|
||||||
self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: channelName, info: nil, uid: 0, joinSuccess: nil)
|
self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: channelName, info: nil, uid: 0, joinSuccess: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// anyone can leave at any time
|
// anyone can leave at any time
|
||||||
func leaveConvo() {
|
func leaveConvo() {
|
||||||
|
self.agoraKit?.delegate = self
|
||||||
agoraKit?.leaveChannel(nil)
|
agoraKit?.leaveChannel(nil)
|
||||||
|
|
||||||
// if I am the second person in the room, then end the convo
|
let userId = AuthSessionStore.getCurrentUserId()
|
||||||
|
|
||||||
self.selectedConvoId = nil
|
if userId == nil {
|
||||||
self.currConvo = nil
|
print("not authenticated...can't join channel")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.selectedConvoId == nil {
|
||||||
|
print("not in a convo currently!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.firestoreService.getConvo(channelName: self.selectedConvoId!) {convo in
|
||||||
|
if convo == nil {
|
||||||
|
print("no such convo found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var updatedConvo = convo
|
||||||
|
|
||||||
|
self.firestoreService.updateUserStatus(userId: userId!, userStatus: .online) {[weak self] res in
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
switch res {
|
||||||
|
case .success:
|
||||||
|
//update convo in database to show that I left...if I am the last person, also close out the channel
|
||||||
|
|
||||||
|
let updatedUsers: [String] = updatedConvo!.users.filter{ arrUserId in
|
||||||
|
return arrUserId != userId
|
||||||
|
}
|
||||||
|
|
||||||
|
updatedConvo!.users = updatedUsers
|
||||||
|
|
||||||
|
// if I am the last one in the convo, then end the convo
|
||||||
|
if updatedConvo!.users.count == 1 && updatedConvo!.users[0] == userId {
|
||||||
|
updatedConvo!.state = .complete
|
||||||
|
updatedConvo!.endedTimestamp = Date()
|
||||||
|
}
|
||||||
|
|
||||||
|
self?.firestoreService.updateConvo(convo: updatedConvo!) {[weak self] res in
|
||||||
|
self?.selectedConvoId = nil
|
||||||
|
|
||||||
|
print("left convo officially in our db as well")
|
||||||
|
}
|
||||||
|
case .error(let error):
|
||||||
|
print(error)
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func destroyInstance() {
|
||||||
AgoraRtcEngineKit.destroy()
|
AgoraRtcEngineKit.destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ConvoViewModel {
|
extension ConvoViewModel {
|
||||||
func initializeAgoraEngine() {
|
|
||||||
// TODO: put app id in environment variables
|
|
||||||
agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: self)
|
|
||||||
}
|
|
||||||
|
|
||||||
func playJoinAudioEffect(engine: AgoraRtcEngineKit) {
|
func playJoinAudioEffect(engine: AgoraRtcEngineKit) {
|
||||||
print("playing JOINED audio sound")
|
print("playing JOINED audio sound")
|
||||||
@@ -305,27 +344,35 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
|
|||||||
|
|
||||||
// TODO: leave channel if it's just me
|
// TODO: leave channel if it's just me
|
||||||
|
|
||||||
if let userId = AuthSessionStore.getCurrentUserId() {
|
let userId = AuthSessionStore.getCurrentUserId()
|
||||||
if self.currConvo == nil {
|
|
||||||
|
if userId == nil {
|
||||||
|
print("not authenticated...can't join channel")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.firestoreService.getConvo(channelName: channel) {convo in
|
||||||
|
if convo == nil {
|
||||||
print("no such convo found")
|
print("no such convo found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// set my user status to in convo
|
var updatedConvo = convo
|
||||||
self.firestoreService.updateUserStatus(userId: userId, userStatus: .inConvo) {[weak self] res in
|
|
||||||
|
// set my user status to inConvo
|
||||||
|
self.firestoreService.updateUserStatus(userId: userId!, userStatus: .inConvo) {[weak self] res in
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
switch res {
|
switch res {
|
||||||
case .success:
|
case .success:
|
||||||
// update convo in database users array to let them know I am in now
|
// update convo in database users array to let them know I am in now
|
||||||
|
|
||||||
self?.currConvo!.users.append(userId)
|
updatedConvo!.users.append(userId!)
|
||||||
self?.currConvo!.state = .active
|
updatedConvo!.state = .active
|
||||||
|
|
||||||
self?.firestoreService.updateConvo(convo: (self?.currConvo)!) {[weak self] res in
|
self?.firestoreService.updateConvo(convo: updatedConvo!) {[weak self] res in
|
||||||
print(res)
|
print(res)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
case .error(let error):
|
case .error(let error):
|
||||||
print(error)
|
print(error)
|
||||||
@@ -342,44 +389,7 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
|
|||||||
// sound effect upon leaving
|
// sound effect upon leaving
|
||||||
self.playLeaveAudioEffect(engine: engine)
|
self.playLeaveAudioEffect(engine: engine)
|
||||||
|
|
||||||
if self.currConvo == nil {
|
print("properties in leave channel: \(self)")
|
||||||
print("no such convo found")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if let userId = AuthSessionStore.getCurrentUserId() {
|
|
||||||
// set my user status to online
|
|
||||||
self.firestoreService.updateUserStatus(userId: userId, userStatus: .online) {[weak self] res in
|
|
||||||
print(res)
|
|
||||||
|
|
||||||
switch res {
|
|
||||||
case .success:
|
|
||||||
//update convo in database to show that I left...if I am the last person, also close out the channel
|
|
||||||
|
|
||||||
let updatedUsers: [String] = (self?.currConvo!.users.filter{ arrUserId in
|
|
||||||
return arrUserId != userId
|
|
||||||
})!
|
|
||||||
|
|
||||||
self?.currConvo!.users = updatedUsers
|
|
||||||
|
|
||||||
// if I am the last one in the convo, then end the convo
|
|
||||||
if self?.currConvo!.users.count == 1 && self?.currConvo!.users[0] == userId {
|
|
||||||
self?.currConvo!.state = .complete
|
|
||||||
self?.currConvo!.endedTimestamp = Date()
|
|
||||||
}
|
|
||||||
|
|
||||||
self?.firestoreService.updateConvo(convo: (self?.currConvo)!) {[weak self] res in
|
|
||||||
self?.selectedConvoId = nil
|
|
||||||
self?.currConvo = nil
|
|
||||||
print("left convo officially in our db as well")
|
|
||||||
}
|
|
||||||
case .error(let error):
|
|
||||||
print(error)
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
|
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user