Selaa lähdekoodia

hudhandler into seperate file

Bastian van de Wetering 6 vuotta sitten
vanhempi
commit
caf283d68b

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -94,6 +94,7 @@
 		AEACE2E31FB32B5C00DCDD78 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2E21FB32B5C00DCDD78 /* Constants.swift */; };
 		AEACE2E51FB32E1900DCDD78 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2E41FB32E1900DCDD78 /* Utils.swift */; };
 		AEE56D762253431E007DC082 /* AccountSetupController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D752253431E007DC082 /* AccountSetupController.swift */; };
+		AEE56D7D2253ADB4007DC082 /* HudHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D7C2253ADB4007DC082 /* HudHandler.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -267,6 +268,7 @@
 		AEACE2E21FB32B5C00DCDD78 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
 		AEACE2E41FB32E1900DCDD78 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
 		AEE56D752253431E007DC082 /* AccountSetupController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSetupController.swift; sourceTree = "<group>"; };
+		AEE56D7C2253ADB4007DC082 /* HudHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HudHandler.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -460,6 +462,7 @@
 				78E45E3B21D3D03700D4B15E /* TableViewCell.swift */,
 				78ED838221D5379000243125 /* TextFieldCell.swift */,
 				AEE56D752253431E007DC082 /* AccountSetupController.swift */,
+				AEE56D7C2253ADB4007DC082 /* HudHandler.swift */,
 			);
 			path = "deltachat-ios";
 			sourceTree = "<group>";
@@ -815,6 +818,7 @@
 				78ED838F21D5927A00243125 /* ProfileViewController.swift in Sources */,
 				78E45E4221D3DB4000D4B15E /* UIViewController+Extension.swift in Sources */,
 				7A9FB1441FB061E2001FEA36 /* AppDelegate.swift in Sources */,
+				AEE56D7D2253ADB4007DC082 /* HudHandler.swift in Sources */,
 				7032FF8F2149C1DB00B7EC83 /* BaseController.swift in Sources */,
 				7070FB9320FF4118000DC258 /* dc_msg.c in Sources */,
 				7070FB6820FF345F000DC258 /* dc_array.c in Sources */,

+ 88 - 137
deltachat-ios/AccountSetupController.swift

@@ -7,53 +7,50 @@
 //
 
 import UIKit
-import JGProgressHUD
-
 
 class AccountSetupController: UITableViewController {
-    
+
     var backupProgressObserver: Any?
     var configureProgressObserver: Any?
-    
+
     lazy var hudHandler: HudHandler = {
         let hudHandler = HudHandler(parentView: self.tableView)
         return hudHandler
     }()
-    
+
     lazy var emailCell:InputTableViewCell = {
-       let cell = InputTableViewCell()
+        let cell = InputTableViewCell()
         cell.textLabel?.text = "Email"
         cell.inputField.placeholder = "user@example.com"
         return cell
     }()
-    
+
     lazy var passwordCell:PasswordInputCell = {
         let cell = PasswordInputCell()
         cell.textLabel?.text = "Password"
         cell.inputField.placeholder = "Required"
         return cell
     }()
-  
-  init() {
-    super.init(style: .grouped)
-    tableView.allowsSelection = false
-  }
-  
-  required init?(coder aDecoder: NSCoder) {
-    fatalError("init(coder:) has not been implemented")
-  }
-  
-  override func viewDidLoad() {
-      super.viewDidLoad()
-    self.title = "Login to your server"
-    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
-  }
-    
+
+    init() {
+        super.init(style: .grouped)
+    }
+
+    required init?(coder aDecoder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        self.title = "Login to your server"
+        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
+    }
+
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         addProgressHudEventListener()
     }
-    
+
     override func viewDidDisappear(_ animated: Bool) {
         let nc = NotificationCenter.default
         if let backupProgressObserver = self.backupProgressObserver {
@@ -66,13 +63,13 @@ class AccountSetupController: UITableViewController {
 
     // MARK: - Table view data source
 
-  override func numberOfSections(in tableView: UITableView) -> Int {
-      // #warning Incomplete implementation, return the number of sections
-      return 2
-  }
+    override func numberOfSections(in tableView: UITableView) -> Int {
+        // #warning Incomplete implementation, return the number of sections
+        return 2
+    }
 
     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-      // #warning Incomplete implementation, return the number of rows
+        // #warning Incomplete implementation, return the number of rows
         if section == 0 {
             return 2
         } else {
@@ -84,24 +81,22 @@ class AccountSetupController: UITableViewController {
         if section == 1 {
             return "Advanced"
         } else {
-            return nil 
+            return nil
         }
     }
-    
-    
-    
+
     /*
-    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
-        if section == 0 {
-            return nil 
-        } else {
-            let label = UILabel()
-            label.text = "Advanced"
-            return label
-        }
-    }
-    */
-    
+     override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
+     if section == 0 {
+     return nil
+     } else {
+     let label = UILabel()
+     label.text = "Advanced"
+     return label
+     }
+     }
+     */
+
     override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
         if section == 0 {
             return "There are no Delta Chat servers, your data stays on your device!"
@@ -118,20 +113,48 @@ class AccountSetupController: UITableViewController {
             return passwordCell
         }
     }
-    
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        // handle tap on password 
+        print(indexPath)
+    }
+
     @objc func loginButtonPressed() {
-        guard let emailAdress = emailCell.getText() else {
+        guard let emailAddress = emailCell.getText() else {
             return // handle case when either email or pw fields are empty
         }
-        
+
+        let oAuthStared = showOAuthAlertIfNeeded(emailAddress: emailAddress)
+
+        if oAuthStared {
+            return
+        }
+
         let passWord = passwordCell.getText()  ?? "" // empty passwords are ok -> for oauth there is no password needed
-        
-        MRConfig.addr = emailAdress
+
+        MRConfig.addr = emailAddress
         MRConfig.mailPw = passWord
         dc_configure(mailboxPointer)
         hudHandler.showBackupHud("Configuring account")
     }
-    
+
+    // returns true if needed
+    private func showOAuthAlertIfNeeded(emailAddress: String) -> Bool {
+        guard let oAuth2UrlPointer = dc_get_oauth2_url(mailboxPointer, emailAddress, "chat.delta:/auth") else {
+            return false
+        }
+
+        let oAuth2Url = String(cString: oAuth2UrlPointer)
+
+        // TODO: open webView with url
+        if let url = URL.init(string: oAuth2Url) {
+            UIApplication.shared.open(url)
+            return true
+        } else {
+            return false
+        }
+    }
+
     private func addProgressHudEventListener() {
         let nc = NotificationCenter.default
         backupProgressObserver = nc.addObserver(
@@ -147,6 +170,7 @@ class AccountSetupController: UITableViewController {
                     self.hudHandler.setHudDone(callback: nil)
                 } else {
                     self.hudHandler.setHudProgress(ui["progress"] as! Int)
+
                 }
             }
         }
@@ -163,21 +187,19 @@ class AccountSetupController: UITableViewController {
                     self.hudHandler.setHudDone(callback: nil)
                 } else {
                     self.hudHandler.setHudProgress(ui["progress"] as! Int)
+
                 }
             }
         }
     }
 }
 
-
 class InputTableViewCell: UITableViewCell {
-    
     lazy var inputField: UITextField = {
         let textField = UITextField()
         return textField
     }()
-    
-    
+
     init() {
         super.init(style: .default, reuseIdentifier: nil)
         setupView()
@@ -186,128 +208,57 @@ class InputTableViewCell: UITableViewCell {
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
     private func setupView() {
         contentView.addSubview(inputField)
         inputField.translatesAutoresizingMaskIntoConstraints = false
-        
         inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
         inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
         inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
         inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
         inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
     }
-    
     public func getText() -> String? {
-        return inputField.text 
+        return inputField.text
     }
 }
 
 class PasswordInputCell: UITableViewCell {
-    
     lazy var inputField: UITextField = {
         let textField = UITextField()
         textField.isSecureTextEntry = true
         return textField
     }()
-    
+
     // TODO: to add Eye-icon -> uncomment -> add to inputField.rightView
     /*
-    lazy var makeVisibleIcon: UIImageView = {
-       let view = UIImageView(image: )
-        return view
-    }()
-    */
-    
+     lazy var makeVisibleIcon: UIImageView = {
+     let view = UIImageView(image: )
+     return view
+     }()
+     */
     init() {
         super.init(style: .default, reuseIdentifier: nil)
         setupView()
     }
-    
+
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
     private func setupView() {
         contentView.addSubview(inputField)
         inputField.translatesAutoresizingMaskIntoConstraints = false
-        
         inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
         inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
         inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
         inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
         inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
     }
-    
+
     public func getText() -> String? {
-        return inputField.text 
+        return inputField.text
     }
 }
 
 
-class HudHandler {
-    
-    var backupHud: JGProgressHUD?
-    var view:UIView
-    
-    init(parentView: UIView) {
-        self.view = parentView
-    }
-    
-    
-    
-    func setHudProgress(_ progress: Int) {
-        if let hud = self.backupHud {
-            hud.progress = Float(progress) / 1000.0
-            hud.detailTextLabel.text = "\(progress / 10)% Complete"
-        }
-    }
-    
-    func showBackupHud(_ text: String) {
-        DispatchQueue.main.async {
-            let hud = JGProgressHUD(style: .dark)
-            hud.vibrancyEnabled = true
-            hud.indicatorView = JGProgressHUDPieIndicatorView()
-            
-            hud.detailTextLabel.text = "0% Complete"
-            hud.textLabel.text = text
-            hud.show(in: self.view)
-            
-            self.backupHud = hud
-        }
-    }
-
-    
-    func setHudError(_ message: String?) {
-        if let hud = self.backupHud {
-            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
-                UIView.animate(
-                    withDuration: 0.1, animations: {
-                        hud.textLabel.text = message ?? "Error"
-                        hud.detailTextLabel.text = nil
-                        hud.indicatorView = JGProgressHUDErrorIndicatorView()
-                }
-                )
-                
-                hud.dismiss(afterDelay: 5.0)
-            }
-        }
-    }
-    
-    func setHudDone(callback: (()->())?) {
-        if let hud = self.backupHud {
-            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
-                UIView.animate(
-                    withDuration: 0.1, animations: {
-                        hud.textLabel.text = "Success"
-                        hud.detailTextLabel.text = nil
-                        hud.indicatorView = JGProgressHUDSuccessIndicatorView()
-                }
-                )
-                
-                callback?()
-                hud.dismiss(afterDelay: 1.0)
-            }
-        }
-    }
-}

+ 69 - 0
deltachat-ios/HudHandler.swift

@@ -0,0 +1,69 @@
+//
+//  HudHandler.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 02.04.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import UIKit
+import JGProgressHUD
+
+class HudHandler {
+    var backupHud: JGProgressHUD?
+    var view:UIView
+
+    init(parentView: UIView) {
+        self.view = parentView
+    }
+
+    func setHudProgress(_ progress: Int) {
+        if let hud = self.backupHud {
+            hud.progress = Float(progress) / 1000.0
+            hud.detailTextLabel.text = "\(progress / 10)% Complete"
+        }
+    }
+
+    func showBackupHud(_ text: String) {
+        DispatchQueue.main.async {
+            let hud = JGProgressHUD(style: .dark)
+            hud.vibrancyEnabled = true
+            hud.indicatorView = JGProgressHUDPieIndicatorView()
+            hud.detailTextLabel.text = "0% Complete"
+            hud.textLabel.text = text
+            hud.show(in: self.view)
+            self.backupHud = hud
+        }
+    }
+
+    func setHudError(_ message: String?) {
+        if let hud = self.backupHud {
+            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
+                UIView.animate(
+                    withDuration: 0.1, animations: {
+                        hud.textLabel.text = message ?? "Error"
+                        hud.detailTextLabel.text = nil
+                        hud.indicatorView = JGProgressHUDErrorIndicatorView()
+                }
+                )
+                hud.dismiss(afterDelay: 5.0)
+            }
+        }
+    }
+
+    func setHudDone(callback: (() -> Void)?) {
+        if let hud = self.backupHud {
+            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
+                UIView.animate(
+                    withDuration: 0.1, animations: {
+                        hud.textLabel.text = "Success"
+                        hud.detailTextLabel.text = nil
+                        hud.indicatorView = JGProgressHUDSuccessIndicatorView()
+
+                })
+                callback?()
+                hud.dismiss(afterDelay: 1.0)
+            }
+        }
+    }
+}