more refactoring slowly adding in more and more

This commit is contained in:
talksik
2021-12-30 02:49:18 -08:00
parent cbd655466a
commit a9e6de6c44
4 changed files with 55 additions and 27 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ enum ConvoState: String, Codable {
} }
struct Convo: Identifiable, Codable { struct Convo: Identifiable, Codable {
@DocumentID var id: String? @DocumentID var id: String? = UUID().uuidString
var leaderUserId: String var leaderUserId: String
var receiverUserId: String var receiverUserId: String
+31 -8
View File
@@ -10,12 +10,20 @@ import AgoraRtcKit
class ConvoViewModel: NSObject, ObservableObject { class ConvoViewModel: NSObject, ObservableObject {
@Published var inCall = false @Published var selectedConvoId:String? = nil
let firestoreService = FirestoreService() let firestoreService = FirestoreService()
let testConvos: [Convo] = [
Convo(id: "testChannel", leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAqbHMFow9oQ8BN0WCkFrkGBTqFTjlbr6tJmFH5judwbnZXrgMAAAAAEADQ943gC8nOYQEAAQALyc5h", state: .connected)
]
var agoraKit: AgoraRtcEngineKit? var agoraKit: AgoraRtcEngineKit?
func isInCall() -> Bool {
return self.selectedConvoId != nil
}
override init() { override init() {
super.init() super.init()
@@ -55,18 +63,31 @@ class ConvoViewModel: NSObject, ObservableObject {
/** /**
Allow user to join an active convo Allow user to join an active convo
*/ */
func joinConvo(convoId: String, convoAgoraToken: String) { func joinConvo() {
// ensure the convo is active and includes 2 people in it currently...shouldn't be shown if not anyway // 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 // ensure that this user leaves all other channels
if self.inCall { if self.isInCall() {
self.leaveConvo() self.leaveConvo()
} }
self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true) self.agoraKit?.setDefaultAudioRouteToSpeakerphone(true)
let convo = self.testConvos.first {convo in
convo.id == self.selectedConvoId
}
if convo == nil {
// handles the offchance that the convo was deactivated or was mistakenly seen by this user
print("convo not available")
return
}
let convoAgoraToken:String = convo!.agoraToken
let channelName:String = convo!.id!
// finally join channel // finally join channel
self.agoraKit?.joinChannel(byToken: convoAgoraToken, channelId: "testChannel", 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
@@ -75,7 +96,7 @@ class ConvoViewModel: NSObject, ObservableObject {
// if I am the second person in the room, then end the convo // if I am the second person in the room, then end the convo
self.inCall = false self.selectedConvoId = nil
} }
// 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
@@ -98,8 +119,6 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
print("I did join channel") print("I did join channel")
// leave channel if it's just me // leave channel if it's just me
self.inCall = true
} }
func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) { func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) {
@@ -108,7 +127,7 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
print("I did leave channel") print("I did leave channel")
self.inCall = false self.selectedConvoId = nil
} }
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) { func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
@@ -125,5 +144,9 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) { func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
print("did occur error: \(errorCode.rawValue)") print("did occur error: \(errorCode.rawValue)")
// publish an error in view...toast or something
self.selectedConvoId = nil
} }
} }
+17 -16
View File
@@ -7,22 +7,18 @@
import SwiftUI import SwiftUI
// test convos
let testConvos: [Convo] = [
Convo(id: "testChannel", leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAqbHMFow9oQ8BN0WCkFrkGBTqFTjlbr6tJmFH5judwbnZXrgMAAAAAEADQ943gC8nOYQEAAQALyc5h", state: .connected)
]
struct ConvosView: View { struct ConvosView: View {
@ObservedObject var vm = ConvoViewModel() @EnvironmentObject var vm: ConvoViewModel
@State var animate = false @State var animate = false
var body: some View { var body: some View {
ScrollView([.horizontal]) { ScrollView([.horizontal]) {
HStack { HStack {
ForEach(0..<testConvos.count, id: \.self) {index in ForEach(0..<self.vm.testConvos.count, id: \.self) {index in
let currConvo = self.vm.testConvos[index]
ZStack(alignment: .topTrailing) { ZStack(alignment: .topTrailing) {
Circle() Circle()
.foregroundColor(NirvanaColor.dimTeal.opacity(0.4)) .foregroundColor(self.vm.selectedConvoId == currConvo.id ? Color.green : NirvanaColor.dimTeal.opacity(0.2))
ZStack { ZStack {
Color.clear Color.clear
@@ -47,11 +43,16 @@ struct ConvosView: View {
} }
.frame(width: 100, height: 100) .frame(width: 100, height: 100)
.scaleEffect(self.animate ? 1 : 0.85) .scaleEffect(self.animate ? 1 : 0.85)
.animation(animate ? Animation.easeInOut(duration: 4).repeatForever(autoreverses: true) : .default) .animation(
Animation.easeInOut(duration: self.vm.selectedConvoId == currConvo.id ? 2 : 4
).repeatForever(autoreverses: true),
value: self.animate
)
.onTapGesture { .onTapGesture {
// if not in a call already // if not in a call already
if !self.vm.inCall { if !self.vm.isInCall() {
self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken) self.vm.selectedConvoId = self.vm.testConvos[index].id!
self.vm.joinConvo()
return return
} }
@@ -68,11 +69,11 @@ struct ConvosView: View {
} }
} }
struct ConvosView_Previews: PreviewProvider { //struct ConvosView_Previews: PreviewProvider {
static var previews: some View { // static var previews: some View {
ConvosView() // ConvosView()
} // }
} //}
struct ProfilePictureOverlap: View { struct ProfilePictureOverlap: View {
let numberAttendees:Int = 5 let numberAttendees:Int = 5
@@ -9,6 +9,7 @@ import SwiftUI
import NavigationStack import NavigationStack
struct InnerCircleView: View { struct InnerCircleView: View {
@StateObject var convoViewModel: ConvoViewModel = ConvoViewModel()
@StateObject var innerCircleVM: InnerCircleViewModel = InnerCircleViewModel() @StateObject var innerCircleVM: InnerCircleViewModel = InnerCircleViewModel()
@EnvironmentObject var authSessionStore: AuthSessionStore @EnvironmentObject var authSessionStore: AuthSessionStore
@EnvironmentObject var navigationStack: NavigationStack @EnvironmentObject var navigationStack: NavigationStack
@@ -88,11 +89,12 @@ struct InnerCircleView: View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
// convos // convos
ConvosView() ConvosView()
.environmentObject(self.convoViewModel)
.padding(.top, 50) .padding(.top, 50)
// inner circle grid // inner circle grid
CircleGridView(selectedFriendIndex: self.$selectedFriendIndex) CircleGridView(selectedFriendIndex: self.$selectedFriendIndex)
.environmentObject(innerCircleVM) .environmentObject(self.innerCircleVM)
Spacer() Spacer()
} }
@@ -105,7 +107,9 @@ struct InnerCircleView: View {
CircleNavigationView(alertActive: self.$alertActive, alertText: self.$alertText, alertSubtext: self.$alertSubtext).environmentObject(innerCircleVM) CircleNavigationView(alertActive: self.$alertActive, alertText: self.$alertText, alertSubtext: self.$alertSubtext).environmentObject(innerCircleVM)
} }
CircleFooterView(selectedFriendIndex: self.$selectedFriendIndex).environmentObject(innerCircleVM) CircleFooterView(selectedFriendIndex: self.$selectedFriendIndex)
.environmentObject(self.innerCircleVM)
.environmentObject(self.convoViewModel)
// helper for new users // helper for new users
// TODO: make it back to 1 instead of 10...testing // TODO: make it back to 1 instead of 10...testing