fixing bug with updating of user status

This commit is contained in:
talksik
2021-12-28 23:19:17 -08:00
parent 04affa99fe
commit 4db618c7d0
8 changed files with 182 additions and 31 deletions
@@ -17,6 +17,8 @@ struct CircleGridView: View {
@State var queuePlayer = AVQueuePlayer()
@State var timeObserverToken: Any?
@State private var showCall: Bool = false
@GestureState var dragState = DragState.inactive
let universalSize = UIScreen.main.bounds
@@ -209,7 +211,7 @@ struct CircleGridView: View {
message: Text(self.alertSubtext),
primaryButton: .destructive(Text("Reject"), action: {
// create user friend but a rejected one
if self.authSessionStore.friendsArr.count < 10 {
if self.authSessionStore.friendsArr.count < 10 || self.authSessionStore.user?.phoneNumber == "+19499230445" {
self.innerCircleVM.activateOrDeactiveInboxUser(activate: false, userId: self.authSessionStore.user!.id!, friendId: inboxUserId) { res in
print(res)
}
@@ -266,6 +268,9 @@ struct CircleGridView: View {
}
}
} // scrollview reader
.fullScreenCover(isPresented: $showCall, content: {
CallView()
})
}
@@ -379,6 +384,13 @@ extension CircleGridView {
private func handleTap(gridItemIndex: Int, friendId: String) {
// TODO: if there is a message, then listen to it
// then after that, check if friend is online, and if so, then connect with him/her if they are free/not in another call
// TESTING CALL
let testingCall = false
if testingCall {
self.showCall.toggle()
return
}
print("tap gesture activated")
// clearing the player to make room for this friend's convo or to deselect this user
@@ -11,8 +11,7 @@ import NavigationStack
struct InnerCircleView: View {
@StateObject var innerCircleVM: InnerCircleViewModel = InnerCircleViewModel()
@EnvironmentObject var authSessionStore: AuthSessionStore
@EnvironmentObject var navigationStack: NavigationStack
@EnvironmentObject var navigationStack: NavigationStack
@State var selectedFriendIndex: String? = nil
@@ -0,0 +1,41 @@
//
// AgoraViewController.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/28/21.
//
import Foundation
import AgoraRtcKit
class AgoraViewController: UIViewController {
var agoraKit: AgoraRtcEngineKit?
var agoraDelegate: AgoraRtcEngineDelegate?
override func viewDidLoad() {
super.viewDidLoad()
initializeAgoraEngine()
joinChannel()
}
func initializeAgoraEngine() {
agoraKit = AgoraRtcEngineKit.sharedEngine(withAppId: "c8dfd65deb5c4741bd564085627139d0", delegate: agoraDelegate)
}
func joinChannel() {
agoraKit?.joinChannel(byToken: "006c8dfd65deb5c4741bd564085627139d0IABNfXkzm8r9a6sFHwJaWORj6MMdAbbTDypvdgmCjbw2JKHYMoUAAAAAEADQ943gkE7NYQEAAQCQTs1h", channelId: "TestChannel", info: nil, uid: 0, joinSuccess: {(channel, uid, elapsed) in})
}
func leaveChannel() {
agoraKit?.leaveChannel(nil)
}
func destroyInstance() {
AgoraRtcEngineKit.destroy()
}
override func viewWillDisappear(_ animated: Bool) {
leaveChannel()
destroyInstance()
}
}
@@ -0,0 +1,34 @@
//
// AgroRep.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/28/21.
//
import Foundation
import SwiftUI
import AgoraRtcKit
struct AgoraRep: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> some UIViewController {
let agoraViewController = AgoraViewController()
agoraViewController.agoraDelegate = context.coordinator
return agoraViewController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
class Coordinator: NSObject, AgoraRtcEngineDelegate {
var parent: AgoraRep
init(_ agoraRep: AgoraRep) {
parent = agoraRep
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
}
@@ -0,0 +1,45 @@
//
// CallView.swift
// nirvana-ios
//
// Created by Arjun Patel on 12/28/21.
//
import SwiftUI
struct CallView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
Text("Welcome to the call!")
.bold()
Spacer()
HStack {
Image(systemName: "mic.circle.fill")
.font(.system(size:64.0))
.foregroundColor(.blue)
.padding()
Spacer()
Image(systemName: "phone.circle.fill")
.font(.system(size:64.0))
.foregroundColor(.red)
.padding()
.onTapGesture {
presentationMode.wrappedValue.dismiss()
}
}
AgoraRep()
.frame(width: 0, height: 0, alignment: .center)
.padding()
}
}
}
struct CallView_Previews: PreviewProvider {
static var previews: some View {
CallView()
}
}
+8 -28
View File
@@ -19,36 +19,16 @@ enum NavigationPages {
struct RouterView: View {
@EnvironmentObject var authSessionStore: AuthSessionStore
@EnvironmentObject var navigationStack: NavigationStack
@Environment(\.scenePhase) var scenePhase
var body: some View {
ZStack {
switch self.authSessionStore.sessionState {
case SessionState.notCheckedYet:
SplashView()
case SessionState.isAuthenticated:
InnerCircleView()
case SessionState.isLoggedOut:
WelcomeView()
}
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .inactive {
print("user is offline")
// set firestore user document isOnline to false
self.authSessionStore.updateUserStatus(userStatus: .offline)
} else if newPhase == .active {
print("user is online again")
// set firestore user document isOnline to true
self.authSessionStore.updateUserStatus(userStatus: .online)
} else if newPhase == .background {
// TODO: find a way to do this in the background so that people can call me and start talking even if it's in the background
// but this may just not be possible, only with an actual call so to speak
print("app is in backgroun")
self.authSessionStore.updateUserStatus(userStatus: .background)
}
}
switch self.authSessionStore.sessionState {
case SessionState.notCheckedYet:
SplashView()
case SessionState.isAuthenticated:
InnerCircleView()
case SessionState.isLoggedOut:
WelcomeView()
}
}
}
+20
View File
@@ -13,6 +13,7 @@ import FirebaseMessaging
@main
struct nirvana_iosApp: App {
@StateObject var authSessionStore: AuthSessionStore = AuthSessionStore()
@Environment(\.scenePhase) var scenePhase
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
@@ -24,6 +25,25 @@ struct nirvana_iosApp: App {
Auth.auth().canHandle(url) // <- just for information purposes
}
}
.onChange(of: scenePhase) { newPhase in
switch newPhase {
case .inactive:
print("user is offline")
// set firestore user document isOnline to false
self.authSessionStore.updateUserStatus(userStatus: .offline)
case .active:
print("user is online again")
// set firestore user document isOnline to true
self.authSessionStore.updateUserStatus(userStatus: .online)
case .background:
// TODO: find a way to do this in the background so that people can call me and start talking even if it's in the background
// but this may just not be possible, only with an actual call so to speak
print("app is in backgroun")
self.authSessionStore.updateUserStatus(userStatus: .background)
default:
print("default state of app")
}
}
}
}