adding delegate methods and making speaker phone work
This commit is contained in:
@@ -9,13 +9,17 @@ import Foundation
|
|||||||
import AgoraRtcKit
|
import AgoraRtcKit
|
||||||
|
|
||||||
|
|
||||||
class ConvoViewModel: ObservableObject {
|
class ConvoViewModel: NSObject, ObservableObject {
|
||||||
|
@Published var inCall = false
|
||||||
|
|
||||||
let firestoreService = FirestoreService()
|
let firestoreService = FirestoreService()
|
||||||
|
|
||||||
var agoraKit: AgoraRtcEngineKit?
|
var agoraKit: AgoraRtcEngineKit?
|
||||||
var agoraDelegate: AgoraRtcEngineDelegate?
|
var agoraDelegate: AgoraRtcEngineDelegate?
|
||||||
|
|
||||||
init() {
|
override init() {
|
||||||
|
super.init()
|
||||||
|
|
||||||
// initiate convo listener to get all relevant convos
|
// initiate convo listener to get all relevant convos
|
||||||
// any convo that is active
|
// any convo that is active
|
||||||
// and any of my active friends are in that convo
|
// 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
|
// ensure that this user leaves all other channels
|
||||||
self.leaveConvo()
|
self.leaveConvo()
|
||||||
|
|
||||||
// finally join channel
|
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
|
||||||
agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in
|
|
||||||
|
|
||||||
|
// finally join channel
|
||||||
|
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
|
// anyone can leave at any time
|
||||||
private func leaveConvo() {
|
func leaveConvo() {
|
||||||
agoraKit?.leaveChannel(nil)
|
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
|
// if you are the second to last person in the convo and you leave, you end the convo
|
||||||
private func endConvo() {
|
private func endConvo() {
|
||||||
|
self.leaveConvo()
|
||||||
|
|
||||||
AgoraRtcEngineKit.destroy()
|
AgoraRtcEngineKit.destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,3 +93,32 @@ extension ConvoViewModel {
|
|||||||
agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate)
|
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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -49,7 +49,14 @@ struct ConvosView: View {
|
|||||||
.scaleEffect(self.animate ? 1 : 0.85)
|
.scaleEffect(self.animate ? 1 : 0.85)
|
||||||
.animation(animate ? Animation.easeInOut(duration: 4).repeatForever(autoreverses: true) : .default)
|
.animation(animate ? Animation.easeInOut(duration: 4).repeatForever(autoreverses: true) : .default)
|
||||||
.onTapGesture {
|
.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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user