aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Session
diff options
context:
space:
mode:
Diffstat (limited to 'iosApp/iosApp/Session')
-rw-r--r--iosApp/iosApp/Session/RootView.swift38
-rw-r--r--iosApp/iosApp/Session/RootViewModel.swift36
2 files changed, 60 insertions, 14 deletions
diff --git a/iosApp/iosApp/Session/RootView.swift b/iosApp/iosApp/Session/RootView.swift
index 960187a..10498b2 100644
--- a/iosApp/iosApp/Session/RootView.swift
+++ b/iosApp/iosApp/Session/RootView.swift
@@ -35,16 +35,34 @@ struct RootView: View {
var body: some View {
Group {
- switch rootViewModel.loginState {
- case is SessionController.LoginStateLoading:
- LoadingView()
- case is SessionController.LoginStateSuccess:
- UnitsView()
- default:
- LoginContentView(username: $username,
- password: $password,
- server: $server,
- onLogin: rootViewModel.login)
+ VStack {
+ if (rootViewModel.networkAvailable != true) {
+ OfflineBanner()
+ }
+
+ if rootViewModel.showLoadingView {
+ LoadingView()
+ .frame(minHeight: 0, maxHeight: .infinity)
+ } else {
+ switch rootViewModel.loginState {
+ case is SessionController.LoginStateSuccess:
+ UnitsView()
+ case is SessionController.LoginStateLoading:
+ LoadingView()
+ .frame(minHeight: 0, maxHeight: .infinity)
+ default:
+ LoginContentView(username: $username,
+ password: $password,
+ server: $server,
+ onLogin: { username, password, server in
+ rootViewModel.login(
+ username: username,
+ password: password,
+ url: server
+ )
+ })
+ }
+ }
}
}.environmentObject(rootViewModel)
}
diff --git a/iosApp/iosApp/Session/RootViewModel.swift b/iosApp/iosApp/Session/RootViewModel.swift
index ec103ba..16f21f3 100644
--- a/iosApp/iosApp/Session/RootViewModel.swift
+++ b/iosApp/iosApp/Session/RootViewModel.swift
@@ -20,14 +20,42 @@ import shared
@MainActor
class RootViewModel: ObservableObject {
+ @Inject private var networkController: NetworkController
@Inject private var sessionController: SessionController
+ @Published var networkAvailable: Bool? = nil
@Published var loginState: SessionController.LoginState = SessionController.LoginStateNothing()
+ @Published var showLoadingView: Bool = false
+
+ var hasSession: Bool{
+ get { return sessionController.hasSession }
+ }
init() {
- let collector = Collector<SessionController.LoginState?>(callback: setLoginState)
- sessionController.loginStateFlow.collect(collector: collector) { _ in }
- restoreSession()
+ let networkCollector = Collector<Bool?>(callback: setNetworkState)
+ networkController.networkAvailable.collect(collector: networkCollector) { _ in }
+ let sessionCollector = Collector<SessionController.LoginState?>(callback: setLoginState)
+ sessionController.loginStateFlow.collect(collector: sessionCollector) { _ in }
+ }
+
+ func setNetworkState(state: Bool?) {
+ print("Network state is: \(state?.description ?? "")")
+ Task { @MainActor in
+ self.networkAvailable = state
+
+ // Wait for internet to restore session
+ if (state == true && sessionController.hasSession) {
+ showLoadingView = false
+ restoreSession()
+ return
+ }
+
+ if (state == nil) {
+ showLoadingView = true
+ } else {
+ showLoadingView = hasSession
+ }
+ }
}
func setLoginState(state: SessionController.LoginState?) {
@@ -38,7 +66,7 @@ class RootViewModel: ObservableObject {
}
func restoreSession() {
- sessionController.restoreSession()
+ sessionController.getSession()
}
private func getFcmToken() -> String? {