From cbd655466a597422eb2439bcc9e4f107df38d6a7 Mon Sep 17 00:00:00 2001 From: talksik Date: Thu, 30 Dec 2021 02:21:10 -0800 Subject: [PATCH] good spot figuring out how to use the delegate --- nirvana-ios/Views/Convos/ConvoViewModel.swift | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/nirvana-ios/Views/Convos/ConvoViewModel.swift b/nirvana-ios/Views/Convos/ConvoViewModel.swift index 5471299..56657f0 100644 --- a/nirvana-ios/Views/Convos/ConvoViewModel.swift +++ b/nirvana-ios/Views/Convos/ConvoViewModel.swift @@ -15,7 +15,6 @@ class ConvoViewModel: NSObject, ObservableObject { let firestoreService = FirestoreService() var agoraKit: AgoraRtcEngineKit? - var agoraDelegate: AgoraRtcEngineDelegate? override init() { super.init() @@ -60,14 +59,14 @@ class ConvoViewModel: NSObject, ObservableObject { // ensure the convo is active and includes 2 people in it currently...shouldn't be shown if not anyway // ensure that this user leaves all other channels - self.leaveConvo() + if self.inCall { + self.leaveConvo() + } self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true) // finally join channel - self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in - self.inCall = true - }) + self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: nil) } // anyone can leave at any time @@ -90,23 +89,29 @@ class ConvoViewModel: NSObject, ObservableObject { extension ConvoViewModel { func initializeAgoraEngine() { // TODO: put app id in environment variables - agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate) + agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: self) } } extension ConvoViewModel: AgoraRtcEngineDelegate { func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) { print("I did join channel") + + // leave channel if it's just me + + self.inCall = true } func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) { + // leave channel if I am the only one in it + print("I did leave channel") + + + self.inCall = false } func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) { - // Only one remote video view is available for this - // tutorial. Here we check if there exists a surface - // view tagged as this uid. print("new user joined \(uid)") }