aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Session
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-27 00:45:53 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-27 00:45:53 -0600
commitb4cecb08fb3b704733048c02c710dbd9f8205ec6 (patch)
tree51c4c6a8308caa0227afcc5baa1a93d0f4ba8adf /iosApp/iosApp/Session
parentf6b0f36d7d0bdfd1911483466f1753ea2f1000c2 (diff)
downloadetbsa-trackermap-mobile-b4cecb08fb3b704733048c02c710dbd9f8205ec6.tar.gz
etbsa-trackermap-mobile-b4cecb08fb3b704733048c02c710dbd9f8205ec6.tar.bz2
etbsa-trackermap-mobile-b4cecb08fb3b704733048c02c710dbd9f8205ec6.zip
Implemented functional view model for LoginView
Diffstat (limited to 'iosApp/iosApp/Session')
-rw-r--r--iosApp/iosApp/Session/LoginView.swift133
-rw-r--r--iosApp/iosApp/Session/LoginViewModel.swift38
2 files changed, 171 insertions, 0 deletions
diff --git a/iosApp/iosApp/Session/LoginView.swift b/iosApp/iosApp/Session/LoginView.swift
new file mode 100644
index 0000000..0637ff2
--- /dev/null
+++ b/iosApp/iosApp/Session/LoginView.swift
@@ -0,0 +1,133 @@
+import SwiftUI
+import shared
+
+struct RootView: View {
+ @StateObject private var loginViewModel = LoginViewModel()
+ @State private var username = ""
+ @State private var password = ""
+ @State private var server = ""
+
+ var body: some View {
+ switch loginViewModel.loginState {
+ case is SessionController.LoginStateLoading:
+ Text("loading")
+ case is SessionController.LoginStateSuccess:
+ UnitsView()
+ default:
+ LoginContentView(username: $username,
+ password: $password,
+ server: $server,
+ onLogin: loginViewModel.login)
+ }
+ }
+}
+
+struct LoginContentView: View {
+ @Binding var username: String
+ @Binding var password: String
+ @Binding var server: String
+
+ let onLogin: (SessionBody) -> Void
+
+ var body: some View {
+ VStack {
+ Spacer()
+ LoginTitleView()
+
+ Spacer()
+ LoginForm(username: $username,
+ password: $password,
+ server: $server)
+
+ Button(action: {
+ self.onLogin(SessionBody(url: server,
+ email: username,
+ password: password,
+ fcmToken: nil))
+ }) {
+ Text("login")
+ .font(.system(size: 18))
+ .bold()
+ }
+ .padding([.top, .bottom], 30)
+
+ Spacer()
+ }
+ .padding()
+ }
+}
+
+struct LoginTitleView: View {
+ var body: some View {
+ return VStack {
+ Text("app-name")
+ .font(.title)
+ .bold()
+ }
+ }
+}
+
+struct LoginForm: View {
+ @Binding var username: String
+ @Binding var password: String
+ @Binding var server: String
+
+ var body: some View {
+ VStack {
+ FormTextField(title: "username", text: $username)
+
+ FormTextField(title: "password", text: $password, isSecure: true)
+
+ #if os(macOS)
+ FormTextField(title: "server-url", text: $server)
+ #else
+ FormTextField(title: "server-url", text: $server, keyboardType: .URL)
+ #endif
+ }
+ }
+}
+
+private struct FormTextField: View {
+ @Environment(\.colorScheme) private var colorScheme
+
+ let title: LocalizedStringKey
+ @Binding var text: String
+ var onEditingChanged: ((Bool) -> Void)?
+
+ #if os(macOS)
+ #else
+ var keyboardType: UIKeyboardType = .default
+ #endif
+
+ var isSecure = false
+
+ var body: some View {
+ ZStack {
+ Capsule(style: .continuous)
+ .foregroundColor(colorScheme == .light
+ ? Color(#colorLiteral(red: 0.9395676295, green: 0.9395676295, blue: 0.9395676295, alpha: 1))
+ : Color(#colorLiteral(red: 0.2293992357, green: 0.2293992357, blue: 0.2293992357, alpha: 1)))
+ .frame(height: 50)
+ if isSecure {
+ SecureField(title, text: $text)
+ .padding()
+ .textContentType(.password)
+ } else {
+ #if os(macOS)
+ TextField(title, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
+ .padding()
+ .autocapitalization(.none)
+ .disableAutocorrection(true)
+ #else
+ TextField(title, text: $text, onEditingChanged: onEditingChanged ?? { _ in })
+ .padding()
+ .autocapitalization(.none)
+ .disableAutocorrection(true)
+ .keyboardType(keyboardType)
+ #endif
+ }
+ }
+ .padding(.horizontal)
+ .frame(maxWidth: 400)
+ }
+}
diff --git a/iosApp/iosApp/Session/LoginViewModel.swift b/iosApp/iosApp/Session/LoginViewModel.swift
new file mode 100644
index 0000000..ee3038e
--- /dev/null
+++ b/iosApp/iosApp/Session/LoginViewModel.swift
@@ -0,0 +1,38 @@
+//
+// LoginViewModel.swift
+// iosApp
+//
+// Created by Iván on 27/01/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import Foundation
+import shared
+
+class LoginViewModel: ObservableObject {
+ @Inject private var sessionController: SessionController
+
+ @Published var loginState: SessionController.LoginState = SessionController.LoginStateNothing()
+
+ init () {
+ let collector = Collector<SessionController.LoginState?>(callback: setLoginState)
+ sessionController.loginStateFlow.collect(collector: collector) { (unit, error) in }
+ restoreSession()
+ }
+
+ func setLoginState (state: SessionController.LoginState?) {
+ print("State is: \(state?.debugDescription ?? "")")
+ self.loginState = state ?? SessionController.LoginStateNothing()
+ }
+
+ func restoreSession() {
+ sessionController.restoreSession()
+ }
+
+ func login (session: SessionBody) {
+ print("Username: \(session.email)")
+ print("Password: \(session.password)")
+ print("Server URL: \(session.url)")
+ sessionController.login(body: session)
+ }
+}