diff --git a/nirvana-ios.xcodeproj/project.pbxproj b/nirvana-ios.xcodeproj/project.pbxproj index 4938b44..2d416d4 100644 --- a/nirvana-ios.xcodeproj/project.pbxproj +++ b/nirvana-ios.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 8904DEC8277055E90071E6CA /* OnboardingOneView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8904DEC7277055E90071E6CA /* OnboardingOneView.swift */; }; + 89091993277D958D0025F4BB /* ConvoViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89091992277D958D0025F4BB /* ConvoViewModel.swift */; }; 890C245D277BF628009B4A30 /* AgoraRtcKit in Frameworks */ = {isa = PBXBuildFile; productRef = 890C245C277BF628009B4A30 /* AgoraRtcKit */; }; 890C2460277BF896009B4A30 /* AgoraViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 890C245F277BF896009B4A30 /* AgoraViewController.swift */; }; 890C2462277BF935009B4A30 /* CallView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 890C2461277BF935009B4A30 /* CallView.swift */; }; @@ -92,6 +93,7 @@ /* Begin PBXFileReference section */ 8904DEC7277055E90071E6CA /* OnboardingOneView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnboardingOneView.swift; sourceTree = ""; }; + 89091992277D958D0025F4BB /* ConvoViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConvoViewModel.swift; sourceTree = ""; }; 890C245F277BF896009B4A30 /* AgoraViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgoraViewController.swift; sourceTree = ""; }; 890C2461277BF935009B4A30 /* CallView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CallView.swift; sourceTree = ""; }; 890C2463277BFA97009B4A30 /* AgroRep.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AgroRep.swift; sourceTree = ""; }; @@ -373,6 +375,7 @@ isa = PBXGroup; children = ( 89814682277D83F300DA46CD /* ConvosView.swift */, + 89091992277D958D0025F4BB /* ConvoViewModel.swift */, ); path = Convos; sourceTree = ""; @@ -640,6 +643,7 @@ 89BCABE5276B3D58009E9B10 /* InnerCircleView.swift in Sources */, 894E80C1276EFD0500624B72 /* AudioPlayerView.swift in Sources */, 89F18AF5276ECF2A00115B1E /* AddBasicInfoView.swift in Sources */, + 89091993277D958D0025F4BB /* ConvoViewModel.swift in Sources */, 89BCABF0276D53C4009E9B10 /* PhoneVerificationViewModel.swift in Sources */, 89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */, 890C2464277BFA97009B4A30 /* AgroRep.swift in Sources */, diff --git a/nirvana-ios/Views/Convos/ConvoViewModel.swift b/nirvana-ios/Views/Convos/ConvoViewModel.swift new file mode 100644 index 0000000..c3cf191 --- /dev/null +++ b/nirvana-ios/Views/Convos/ConvoViewModel.swift @@ -0,0 +1,83 @@ +// +// ConvoViewModel.swift +// nirvana-ios +// +// Created by Arjun Patel on 12/29/21. +// + +import Foundation +import AgoraRtcKit + + +class ConvoViewModel: ObservableObject { + let firestoreService = FirestoreService() + + var agoraKit: AgoraRtcEngineKit? + var agoraDelegate: AgoraRtcEngineDelegate? + + init() { + // initiate convo listener to get all relevant convos + // any convo that is active + // and any of my active friends are in that convo + // if am being "called" catch that and join the convo/channel + + // only show convos in which I know a majority of the people inside + + // initiate agora engine + self.initializeAgoraEngine() + } + + deinit { + // deinit the firestore listener + // deinit the agora engine + } + + /** + @param: friendId: the second person in the convo...the receiver + **/ + func startConvo(friendId: String) { + if let userId = AuthSessionStore.getCurrentUserId() { + + } + + // need to make sure the other user is online + + // get a new agora token from cloud function + + // create a convo/channel in db to notify the other user with proper attributes + + // join the convo myself + } + + /** + Allow user to join an active convo + */ + func joinConvo(convoId: String, convoAgoraToken: String) { + // 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() + + // finally join channel + agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in + + }) + } + + // anyone can leave at any time + private func leaveConvo() { + agoraKit?.leaveChannel(nil) + } + + // if you are the second to last person in the convo and you leave, you end the convo + private func endConvo() { + AgoraRtcEngineKit.destroy() + } +} + +extension ConvoViewModel { + func initializeAgoraEngine() { + // TODO: put app id in environment variables + agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate) + } +} diff --git a/nirvana-ios/Views/Convos/ConvosView.swift b/nirvana-ios/Views/Convos/ConvosView.swift index 0c4e182..1ad4b94 100644 --- a/nirvana-ios/Views/Convos/ConvosView.swift +++ b/nirvana-ios/Views/Convos/ConvosView.swift @@ -9,20 +9,21 @@ import SwiftUI // test convos let testConvos: [Convo] = [ - Convo(leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAIlYcWj21zCp4jq/WS2BMaiMYlKSax7ohIjzBx1BtpWAx+f9gAAAAAEADQ943gX6LOYQEAAQBfos5h", state: .connected) + Convo(id: "testChannel", leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAqbHMFow9oQ8BN0WCkFrkGBTqFTjlbr6tJmFH5judwbnZXrgMAAAAAEADQ943gC8nOYQEAAQALyc5h", state: .connected) ] struct ConvosView: View { + @ObservedObject var vm = ConvoViewModel() + @State var animate = false + var body: some View { ScrollView([.horizontal]) { HStack { ForEach(0..