diff --git a/nirvana-ios/Views/Convos/ConvoViewModel.swift b/nirvana-ios/Views/Convos/ConvoViewModel.swift index c3cf191..5471299 100644 --- a/nirvana-ios/Views/Convos/ConvoViewModel.swift +++ b/nirvana-ios/Views/Convos/ConvoViewModel.swift @@ -9,13 +9,17 @@ import Foundation import AgoraRtcKit -class ConvoViewModel: ObservableObject { +class ConvoViewModel: NSObject, ObservableObject { + @Published var inCall = false + let firestoreService = FirestoreService() var agoraKit: AgoraRtcEngineKit? var agoraDelegate: AgoraRtcEngineDelegate? - init() { + override init() { + super.init() + // initiate convo listener to get all relevant convos // any convo that is active // and any of my active friends are in that convo @@ -58,19 +62,27 @@ class ConvoViewModel: ObservableObject { // ensure that this user leaves all other channels self.leaveConvo() + self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true) + // finally join channel - agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in - + self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in + self.inCall = true }) } // anyone can leave at any time - private func leaveConvo() { + func leaveConvo() { agoraKit?.leaveChannel(nil) + + // if I am the second person in the room, then end the convo + + self.inCall = false } // if you are the second to last person in the convo and you leave, you end the convo private func endConvo() { + self.leaveConvo() + AgoraRtcEngineKit.destroy() } } @@ -81,3 +93,32 @@ extension ConvoViewModel { agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate) } } + +extension ConvoViewModel: AgoraRtcEngineDelegate { + func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinChannel channel: String, withUid uid: UInt, elapsed: Int) { + print("I did join channel") + } + + func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) { + print("I did leave channel") + } + + 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)") + } + + func rtcEngine(_ engine: AgoraRtcEngineKit, didOfflineOfUid uid: UInt, reason: AgoraUserOfflineReason) { + print("user joined left") + } + + func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurWarning warningCode: AgoraWarningCode) { + print("did occur warning: \(warningCode.rawValue)") + } + + func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) { + print("did occur error: \(errorCode.rawValue)") + } +} diff --git a/nirvana-ios/Views/Convos/ConvosView.swift b/nirvana-ios/Views/Convos/ConvosView.swift index 1ad4b94..76eaca1 100644 --- a/nirvana-ios/Views/Convos/ConvosView.swift +++ b/nirvana-ios/Views/Convos/ConvosView.swift @@ -49,7 +49,14 @@ struct ConvosView: View { .scaleEffect(self.animate ? 1 : 0.85) .animation(animate ? Animation.easeInOut(duration: 4).repeatForever(autoreverses: true) : .default) .onTapGesture { - self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken) + // if not in a call already + if !self.vm.inCall { + self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken) + + return + } + + self.vm.leaveConvo() } } }