Эх сурвалжийг харах

make credentials screen functional

Jonas Reinsch 7 жил өмнө
parent
commit
30b48df1e8

+ 19 - 11
deltachat-ios/AppCoordinator.swift

@@ -13,16 +13,30 @@ protocol Coordinator {
 }
 
 class AppCoordinator: Coordinator {
+    let tabBarController = UITabBarController()
+
     func setupViewControllers(window: UIWindow) {
-        let credentialsController = CredentialsController()
-        let credentialsNav = UINavigationController(rootViewController: credentialsController)
-        
-        window.rootViewController = credentialsNav
+        window.rootViewController = tabBarController
         window.makeKeyAndVisible()
         window.backgroundColor = UIColor.white
+        
+        let ud = UserDefaults.standard
+        if ud.bool(forKey: Constants.Keys.deltachatUserProvidedCredentialsKey) {
+            initCore(withCredentials: false)
+            setupInnerViewControllers()
+        } else {
+//            let email = "alice@librechat.net"
+//            let password = "foobar"
+//            initCore(email: email, password: password)
+            
+            let credentialsController = CredentialsController()
+            let credentialsNav = UINavigationController(rootViewController: credentialsController)
+
+            tabBarController.present(credentialsNav, animated: false, completion: nil)
+        }
     }
     
-    func setupViewControllersReal(window: UIWindow) {
+    func setupInnerViewControllers() {
 
         let contactViewController = ContactViewController(coordinator: self)
         let contactNavigationController = UINavigationController(rootViewController: contactViewController)
@@ -47,16 +61,10 @@ class AppCoordinator: Coordinator {
         chatNavigationController.tabBarItem = chatTabbarItem
         settingsNavigationController.tabBarItem = settingsTabbarItem
         
-        let tabBarController = UITabBarController()
-        
         tabBarController.viewControllers = [
 //            contactNavigationController,
             chatNavigationController,
             settingsNavigationController,
         ]
-        
-        window.rootViewController = tabBarController
-        window.makeKeyAndVisible()
-        window.backgroundColor = UIColor.white
     }
 }

+ 46 - 33
deltachat-ios/AppDelegate.swift

@@ -80,6 +80,7 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
 
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate {
+    static let appCoordinator = AppCoordinator()
     var window: UIWindow?
 
     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
@@ -89,41 +90,53 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
         guard let window = window else {
             fatalError("window was nil in app delegate")
         }
-        let appCoordinator = AppCoordinator()
-        appCoordinator.setupViewControllers(window: window)
-        
-        //       - second param remains nil (user data for more than one mailbox)
-        mailboxPointer = mrmailbox_new(callback_ios, nil, "iOS")
-        guard mailboxPointer != nil else {
-            fatalError("Error: mrmailbox_new returned nil")
-        }
-        
-        let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
-        let documentsPath = paths[0]
-        let dbfile = documentsPath + "/messenger.db"
-        print(dbfile)
-        
-        let r = mrmailbox_open(mailboxPointer, dbfile, nil)
-        
-        mrmailbox_set_config(mailboxPointer, "addr", "alice@librechat.net")
-        mrmailbox_set_config(mailboxPointer, "mail_pw", "foobar")
-        
-        mrmailbox_configure_and_connect(mailboxPointer)
-        
-        let nc = NotificationCenter.default
-        nc.addObserver(forName:Notification.Name(rawValue:"MrEventMsgsChanged"),
-                       object:nil, queue:nil) {
-                        notification in
-                        print("----------- MrEventMsgsChanged notification received --------")
+        AppDelegate.appCoordinator.setupViewControllers(window: window)
+
+        return true
+    }
+}
+
+
+func initCore(withCredentials: Bool, email: String = "", password: String = "") {
+    let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
+    let documentsPath = paths[0]
+    let dbfile = documentsPath + "/messenger.db"
+    print(dbfile)
+    
+    //       - second param remains nil (user data for more than one mailbox)
+    mailboxPointer = mrmailbox_new(callback_ios, nil, "iOS")
+    guard mailboxPointer != nil else {
+        fatalError("Error: mrmailbox_new returned nil")
+    }
+    
+    let _ = mrmailbox_open(mailboxPointer, dbfile, nil)
+    
+    if withCredentials {
+        if !(email.contains("@") && (email.count >= 3)) {
+            fatalError("initCore called with withCredentials flag set to true, but email not valid")
         }
-        
-        nc.addObserver(forName:Notification.Name(rawValue:"MrEventIncomingMsg"),
-                       object:nil, queue:nil) {
-                        notification in
-                        print("----------- MrEventIncomingMsg received --------")
-                        AudioServicesPlaySystemSound(UInt32(kSystemSoundID_Vibrate))
+        if password.isEmpty {
+            fatalError("initCore called with withCredentials flag set to true, password is empty")
         }
+        mrmailbox_set_config(mailboxPointer, "addr", email)
+        mrmailbox_set_config(mailboxPointer, "mail_pw", password)
+//            -        mrmailbox_set_config(mailboxPointer, "addr", "alice@librechat.net")
+//            -        mrmailbox_set_config(mailboxPointer, "mail_pw", "foobar")
+        UserDefaults.standard.set(true, forKey: Constants.Keys.deltachatUserProvidedCredentialsKey)
+        UserDefaults.standard.synchronize()
+    }
+    
+    mrmailbox_configure_and_connect(mailboxPointer)
+    
+    addVibrationOnIncomingMessage()
+}
 
-        return true
+func addVibrationOnIncomingMessage() {
+    let nc = NotificationCenter.default
+    nc.addObserver(forName:Notification.Name(rawValue:"MrEventIncomingMsg"),
+                   object:nil, queue:nil) {
+                    notification in
+                    print("----------- MrEventIncomingMsg received --------")
+                    AudioServicesPlaySystemSound(UInt32(kSystemSoundID_Vibrate))
     }
 }

+ 6 - 0
deltachat-ios/Constants.swift

@@ -12,4 +12,10 @@ struct Constants {
      struct Color {
         static let bubble = UIColor(netHex: 0xefffde)
     }
+    
+    struct Keys {
+        static let deltachatUserProvidedCredentialsKey = "__DELTACHAT_USER_PROVIDED_CREDENTIALS_KEY__"
+        static let deltachatImapEmailKey = "__DELTACHAT_IMAP_EMAIL_KEY__"
+        static let deltachatImapPasswordKey = "__DELTACHAT_IMAP_PASSWORD_KEY__"
+    }
 }

+ 4 - 2
deltachat-ios/CredentialsController.swift

@@ -150,7 +150,10 @@ class CredentialsController: UITableViewController {
     }
     
     @objc func saveAccountButtonPressed() {
-        print("model: \(model)")
+        dismiss(animated: true) {
+            initCore(withCredentials: true, email: self.model.email, password: self.model.password)
+            AppDelegate.appCoordinator.setupInnerViewControllers()
+        }
     }
     
     required init?(coder aDecoder: NSCoder) {
@@ -178,7 +181,6 @@ class CredentialsController: UITableViewController {
         
         return cells[row]
     }
-    
 
     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()