Browse Source

Move login view and extract logic into AppDelegate

Sergey 2 years ago
parent
commit
cc4bba13ac
4 changed files with 48 additions and 51 deletions
  1. 11 1
      Chat/Main/AppDelegate.swift
  2. 10 8
      Chat/UI/Auth/LoginStore.swift
  3. 3 9
      Chat/UI/Auth/LoginView.swift
  4. 24 33
      Chat/UI/ContentView.swift

+ 11 - 1
Chat/Main/AppDelegate.swift

@@ -13,14 +13,24 @@ import XMPPFramework
 class AppDelegate: UIResponder, UIApplicationDelegate {
 	
 	var window: UIWindow?
+	var rootViewController: UIViewController?
 
 	func application(
 		_ application: UIApplication,
 		didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
 	) -> Bool {
 
+		let loginStore: LoginStore = LoginStore { [weak self] login, password in
+			XMPPController.shared.login(with: login, and: password)
+			XMPPController.shared.connect()
+			self?.rootViewController = UIHostingController(rootView: ContentView())
+			self?.window?.rootViewController = self?.rootViewController
+		}
+		let loginView: LoginView = LoginView(store: loginStore)
+		rootViewController = UIHostingController(rootView: loginView)
+
 		window = UIWindow(frame: UIScreen.main.bounds)
-		window?.rootViewController = UIHostingController(rootView: ContentView())
+		window?.rootViewController = rootViewController
 		window?.makeKeyAndVisible()
 
 		DDLog.add(DDOSLogger.sharedInstance, with: DDLogLevel.all)

+ 10 - 8
Chat/UI/Auth/LoginStore.swift

@@ -10,14 +10,16 @@ import XMPPFramework
 import XMPPFrameworkSwift
 
 final class LoginStore: ObservableObject {
-    @Published var login: String = "test11@msg.sharix-app.org"
-    @Published var password: String = "test11_-"
+	let handler: (String, String) -> Void
 
-    func login(_ completion: @escaping (Bool) -> Void) {
-		XMPPController.shared.login(with: login, and: password)
-		XMPPController.shared.connect()
+	init(handler: @escaping (String, String) -> Void = { _, _ in }) {
+		self.handler = handler
+	}
 
-        completion(true)
-    }
-}
+	@Published var login: String = "test11@msg.sharix-app.org"
+	@Published var password: String = "test11_-"
 
+	func submit() {
+		handler(login, password)
+	}
+}

+ 3 - 9
Chat/UI/Auth/LoginView.swift

@@ -10,7 +10,6 @@ import SwiftUI
 struct LoginView: View {
     @ObservedObject var store: LoginStore
 
-    @Binding var authStatus: Bool
     var body: some View {
         VStack(alignment: .center, spacing: 16) {
             TextField("Login", text: $store.login)
@@ -18,13 +17,7 @@ struct LoginView: View {
             SecureField("Password", text: $store.password)
                 .textFieldStyle(.roundedBorder)
                 .textContentType(.password)
-            Button {
-                store.login { success in
-                    if success {
-                        authStatus = true
-                    }
-                }
-            } label: {
+			Button(action: store.submit) {
                 Text("Login")
             }
         }
@@ -34,7 +27,8 @@ struct LoginView: View {
 
 struct LoginView_Previews: PreviewProvider {
     static var store: LoginStore = LoginStore()
+
     static var previews: some View {
-        LoginView(store: store, authStatus: .constant(false))
+        LoginView(store: store)
     }
 }

+ 24 - 33
Chat/UI/ContentView.swift

@@ -8,44 +8,35 @@
 import SwiftUI
 
 struct ContentView: View {
-    @ObservedObject var loginStore: LoginStore = LoginStore()
-
-    @State var authStatus: Bool = false
     @State var selectedTab: Tab = .rooms
 	
     var body: some View {
-        Group {
-            if authStatus {
-                NavigationView {
-                    TabView(selection: $selectedTab) {
-                        ChatListView()
-                            .navigationViewStyle(StackNavigationViewStyle())
-                            .tabItem {
-								Label(Tab.contacts.string, systemImage: "person.crop.circle")
-                            }
-                            .tag(Tab.contacts)
+		NavigationView {
+			TabView(selection: $selectedTab) {
+				ChatListView()
+					.navigationViewStyle(StackNavigationViewStyle())
+					.tabItem {
+						Label(Tab.contacts.string, systemImage: "person.crop.circle")
+					}
+					.tag(Tab.contacts)
 
-                        RoomsListView()
-                            .navigationViewStyle(StackNavigationViewStyle())
-                            .tabItem {
-								Label(Tab.rooms.string, systemImage: "bubble.left.and.bubble.right")
-                            }
-                            .tag(Tab.rooms)
+				RoomsListView()
+					.navigationViewStyle(StackNavigationViewStyle())
+					.tabItem {
+						Label(Tab.rooms.string, systemImage: "bubble.left.and.bubble.right")
+					}
+					.tag(Tab.rooms)
 
-                        Text("")
-                            .navigationBarTitleDisplayMode(.inline)
-                            .navigationViewStyle(StackNavigationViewStyle())
-                            .tabItem {
-								Label(Tab.settings.string, systemImage: "gear")
-                            }
-                            .tag(Tab.settings)
-                    }
-                    .navigationBarTitle(returnNaviBarTitle(self.selectedTab))
-                }
-            } else {
-                LoginView(store: loginStore, authStatus: $authStatus)
-            }
-        }
+				Text("")
+					.navigationBarTitleDisplayMode(.inline)
+					.navigationViewStyle(StackNavigationViewStyle())
+					.tabItem {
+						Label(Tab.settings.string, systemImage: "gear")
+					}
+					.tag(Tab.settings)
+			}
+			.navigationBarTitle(returnNaviBarTitle(self.selectedTab))
+		}
     }
 
     func returnNaviBarTitle(_ tabSelection: Tab) -> String {