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 {
@DocumentID var id: String?
@DocumentID var id: String? = UUID().uuidString
var leaderUserId: String
var receiverUserId: String
+31 -8
View File
@@ -10,12 +10,20 @@ import AgoraRtcKit
class ConvoViewModel: NSObject, ObservableObject {
@Published var inCall = false
@Published var selectedConvoId:String? = nil
let firestoreService = FirestoreService()
let testConvos: [Convo] = [
Convo(id: "testChannel", leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAqbHMFow9oQ8BN0WCkFrkGBTqFTjlbr6tJmFH5judwbnZXrgMAAAAAEADQ943gC8nOYQEAAQALyc5h", state: .connected)
]
var agoraKit: AgoraRtcEngineKit?
func isInCall() -> Bool {
return self.selectedConvoId != nil
}
override init() {
super.init()
@@ -55,18 +63,31 @@ class ConvoViewModel: NSObject, ObservableObject {
/**
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 that this user leaves all other channels
if self.inCall {
if self.isInCall() {
self.leaveConvo()
}
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
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
@@ -75,7 +96,7 @@ class ConvoViewModel: NSObject, ObservableObject {
// 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
@@ -98,8 +119,6 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
print("I did join channel")
// leave channel if it's just me
self.inCall = true
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didLeaveChannelWith stats: AgoraChannelStats) {
@@ -108,7 +127,7 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
print("I did leave channel")
self.inCall = false
self.selectedConvoId = nil
}
func rtcEngine(_ engine: AgoraRtcEngineKit, didJoinedOfUid uid: UInt, elapsed: Int) {
@@ -125,5 +144,9 @@ extension ConvoViewModel: AgoraRtcEngineDelegate {
func rtcEngine(_ engine: AgoraRtcEngineKit, didOccurError errorCode: AgoraErrorCode) {
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
// test convos
let testConvos: [Convo] = [
Convo(id: "testChannel", leaderUserId: "VWVPKCrNmMeZlkEeZxuJ0Wsgq0C3", receiverUserId: "zd6PpD3TmnQUDoy6noS9aZpuPWr1", agoraToken: "006c8dfd65deb5c4741bd564085627139d0IAAqbHMFow9oQ8BN0WCkFrkGBTqFTjlbr6tJmFH5judwbnZXrgMAAAAAEADQ943gC8nOYQEAAQALyc5h", state: .connected)
]
struct ConvosView: View {
@ObservedObject var vm = ConvoViewModel()
@EnvironmentObject var vm: ConvoViewModel
@State var animate = false
var body: some View {
ScrollView([.horizontal]) {
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) {
Circle()
.foregroundColor(NirvanaColor.dimTeal.opacity(0.4))
.foregroundColor(self.vm.selectedConvoId == currConvo.id ? Color.green : NirvanaColor.dimTeal.opacity(0.2))
ZStack {
Color.clear
@@ -47,11 +43,16 @@ struct ConvosView: View {
}
.frame(width: 100, height: 100)
.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 {
// if not in a call already
if !self.vm.inCall {
self.vm.joinConvo(convoId: testConvos[index].id!, convoAgoraToken: testConvos[index].agoraToken)
if !self.vm.isInCall() {
self.vm.selectedConvoId = self.vm.testConvos[index].id!
self.vm.joinConvo()
return
}
@@ -68,11 +69,11 @@ struct ConvosView: View {
}
}
struct ConvosView_Previews: PreviewProvider {
static var previews: some View {
ConvosView()
}
}
//struct ConvosView_Previews: PreviewProvider {
// static var previews: some View {
// ConvosView()
// }
//}
struct ProfilePictureOverlap: View {
let numberAttendees:Int = 5
@@ -9,6 +9,7 @@ import SwiftUI
import NavigationStack
struct InnerCircleView: View {
@StateObject var convoViewModel: ConvoViewModel = ConvoViewModel()
@StateObject var innerCircleVM: InnerCircleViewModel = InnerCircleViewModel()
@EnvironmentObject var authSessionStore: AuthSessionStore
@EnvironmentObject var navigationStack: NavigationStack
@@ -88,11 +89,12 @@ struct InnerCircleView: View {
VStack(alignment: .leading) {
// convos
ConvosView()
.environmentObject(self.convoViewModel)
.padding(.top, 50)
// inner circle grid
CircleGridView(selectedFriendIndex: self.$selectedFriendIndex)
.environmentObject(innerCircleVM)
.environmentObject(self.innerCircleVM)
Spacer()
}
@@ -105,7 +107,9 @@ struct InnerCircleView: View {
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
// TODO: make it back to 1 instead of 10...testing