aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Session
diff options
context:
space:
mode:
Diffstat (limited to 'iosApp/iosApp/Session')
-rw-r--r--iosApp/iosApp/Session/AboutView.swift34
-rw-r--r--iosApp/iosApp/Session/AccountView.swift73
-rw-r--r--iosApp/iosApp/Session/AccountViewModel.swift29
-rw-r--r--iosApp/iosApp/Session/RootView.swift48
-rw-r--r--iosApp/iosApp/Session/RootViewModel.swift23
-rw-r--r--iosApp/iosApp/Session/UserInformationView.swift50
6 files changed, 229 insertions, 28 deletions
diff --git a/iosApp/iosApp/Session/AboutView.swift b/iosApp/iosApp/Session/AboutView.swift
new file mode 100644
index 0000000..c0f9099
--- /dev/null
+++ b/iosApp/iosApp/Session/AboutView.swift
@@ -0,0 +1,34 @@
+//
+// AboutView.swift
+// iosApp
+//
+// Created by Iván on 15/02/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import SwiftUI
+
+struct AboutView: View {
+ var body: some View {
+ List {
+ Text("about-text").padding()
+ HStack {
+ Text("version")
+ Spacer()
+ Text("1.0").foregroundColor(.secondary)
+ }
+
+ Button {
+ // TODO
+ } label: {
+ Label("source-code", systemImage: "chevron.left.forwardslash.chevron.right")
+ }
+
+ Button {
+ // TODO
+ } label: {
+ Label("website", systemImage: "globe")
+ }
+ }
+ }
+}
diff --git a/iosApp/iosApp/Session/AccountView.swift b/iosApp/iosApp/Session/AccountView.swift
new file mode 100644
index 0000000..4ca453a
--- /dev/null
+++ b/iosApp/iosApp/Session/AccountView.swift
@@ -0,0 +1,73 @@
+//
+// AccountView.swift
+// iosApp
+//
+// Created by Iván on 15/02/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import SwiftUI
+
+struct AccountView: View {
+ @StateObject var accountViewModel = AccountViewModel()
+ @EnvironmentObject var rootViewModel: RootViewModel
+
+ var body: some View {
+ List {
+ Section {
+ // MARK: - Name
+ if let name = accountViewModel.user?.name {
+ HStack {
+ Text("username")
+ Spacer()
+ Text(name).foregroundColor(.secondaryLabel)
+ }
+ }
+ // MARK: - E-mail
+ if let email = accountViewModel.user?.email {
+ HStack {
+ Text("email")
+ Spacer()
+ Text(email).foregroundColor(.secondaryLabel)
+ }
+ }
+ // MARK: - Unique ID
+ if let uid = accountViewModel.user?.id {
+ HStack {
+ Text("unique-id")
+ Spacer()
+ Text("\(uid)").foregroundColor(.secondaryLabel)
+ }
+ }
+ // MARK: - Administrator
+ if let admin = accountViewModel.user?.administrator {
+ HStack {
+ Text("admin")
+ Spacer()
+ Text("\(admin)").foregroundColor(.secondaryLabel)
+ }
+ }
+ // MARK: - Server URL
+ if let server = UserDefaults.standard.string(forKey: "server-url") {
+ HStack {
+ Text("server-url")
+ Spacer()
+ Text(server).foregroundColor(.secondaryLabel)
+ }
+ }
+ }
+
+ Section {
+ // MARK: - Sign out
+ Button {
+ rootViewModel.signOut()
+ } label: {
+ Label("signout", systemImage: "rectangle.portrait.and.arrow.right")
+ .foregroundColor(.systemRed)
+ }
+ }
+ }.onAppear {
+ accountViewModel.fetchUserInfo()
+ }
+ }
+}
diff --git a/iosApp/iosApp/Session/AccountViewModel.swift b/iosApp/iosApp/Session/AccountViewModel.swift
new file mode 100644
index 0000000..7953265
--- /dev/null
+++ b/iosApp/iosApp/Session/AccountViewModel.swift
@@ -0,0 +1,29 @@
+//
+// AccountViewModel.swift
+// iosApp
+//
+// Created by Iván on 15/02/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import Foundation
+import shared
+
+class AccountViewModel: ObservableObject {
+ @Inject var sessionController: SessionController
+
+ @Published var user: User? = nil
+
+ init() {
+ let userCollector = Collector<User?>(callback: setUser)
+ sessionController.userFlow.collect(collector: userCollector) {_, _ in }
+ }
+
+ func setUser(user: User?) {
+ self.user = user
+ }
+
+ func fetchUserInfo() {
+ self.sessionController.getSession()
+ }
+}
diff --git a/iosApp/iosApp/Session/RootView.swift b/iosApp/iosApp/Session/RootView.swift
index bc8a7d1..297a2aa 100644
--- a/iosApp/iosApp/Session/RootView.swift
+++ b/iosApp/iosApp/Session/RootView.swift
@@ -18,24 +18,35 @@
import SwiftUI
import shared
+class SessionState: ObservableObject {
+ @Published var loggedIn = false
+}
+
struct RootView: View {
@StateObject private var rootViewModel = RootViewModel()
@State private var username = ""
@State private var password = ""
- @State private var server = ""
+ @State private var server: String
+
+ init() {
+ server = UserDefaults.standard.string(forKey: "server-url")
+ ?? NSLocalizedString("app-server-url", comment: "")
+ }
var body: some View {
- switch rootViewModel.loginState {
- case is SessionController.LoginStateLoading:
- LoadingView()
- case is SessionController.LoginStateSuccess:
- UnitsView()
- default:
- LoginContentView(username: $username,
- password: $password,
- server: $server,
- onLogin: rootViewModel.login)
- }
+ 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)
+ }
+ }.environmentObject(rootViewModel)
}
}
@@ -44,13 +55,7 @@ struct LoginContentView: View {
@Binding var password: String
@Binding var server: String
- let onLogin: (SessionBody) -> Void
-
- func getFcmToken() -> String? {
- let token = UserDefaults.standard.string(forKey: "fcmtoken")
- print("FCM token is \(String(describing: token))")
- return token
- }
+ let onLogin: (String, String, String) -> Void
var body: some View {
VStack {
@@ -63,10 +68,7 @@ struct LoginContentView: View {
server: $server)
Button(action: {
- self.onLogin(SessionBody(url: server,
- email: username,
- password: password,
- fcmToken: getFcmToken()))
+ self.onLogin(username, password, server)
}) {
Text("login")
.font(.system(size: 18))
diff --git a/iosApp/iosApp/Session/RootViewModel.swift b/iosApp/iosApp/Session/RootViewModel.swift
index d065777..682deee 100644
--- a/iosApp/iosApp/Session/RootViewModel.swift
+++ b/iosApp/iosApp/Session/RootViewModel.swift
@@ -38,10 +38,23 @@ class RootViewModel: ObservableObject {
sessionController.restoreSession()
}
- func login(session: SessionBody) {
- print("Username: \(session.email)")
- print("Password: \(session.password)")
- print("Server URL: \(session.url)")
- sessionController.login(body: session)
+ private func getFcmToken() -> String? {
+ let token = UserDefaults.standard.string(forKey: "fcmtoken")
+ print("FCM token is \(String(describing: token))")
+ return token
+ }
+
+ func login(username: String, password: String, url: String) {
+ print("Username: \(username)")
+ print("Password: \(password)")
+ print("Server URL: \(url)")
+ sessionController.login(body: SessionBody(url: url,
+ email: username,
+ password: password,
+ fcmToken: getFcmToken()))
+ }
+
+ func signOut() {
+ sessionController.logout(token: getFcmToken())
}
}
diff --git a/iosApp/iosApp/Session/UserInformationView.swift b/iosApp/iosApp/Session/UserInformationView.swift
new file mode 100644
index 0000000..98d4bfd
--- /dev/null
+++ b/iosApp/iosApp/Session/UserInformationView.swift
@@ -0,0 +1,50 @@
+//
+// UserInformationView.swift
+// iosApp
+//
+// Created by Iván on 15/02/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import SwiftUI
+
+struct UserInformationView: View {
+ @Environment(\.presentationMode) var presentationMode
+ @State var action = Action.account
+
+ enum Action {
+ case account
+ case about
+ }
+
+ var body: some View {
+ NavigationView {
+ VStack {
+ switch action {
+ case .account:
+ AccountView()
+ case .about:
+ AboutView()
+ }
+ }
+ .navigationBarTitleView(
+ Picker(selection: $action) {
+ Text("account").tag(Action.account)
+ Text("about").tag(Action.about)
+ } label: {
+ EmptyView()
+ }.pickerStyle(SegmentedPickerStyle())
+ )
+ .navigationBarTitleDisplayMode(.inline)
+ .toolbar {
+ ToolbarItem(placement: .navigationBarTrailing) {
+ Button {
+ presentationMode.wrappedValue.dismiss()
+ } label: {
+ Text("done").bold()
+ }
+ }
+ }
+ }
+ }
+}