123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // AccountSetupController.swift
- // deltachat-ios
- //
- // Created by Bastian van de Wetering on 02.04.19.
- // Copyright © 2019 Jonas Reinsch. All rights reserved.
- //
- import UIKit
- 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()
- 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)
- }
- 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 {
- nc.removeObserver(backupProgressObserver)
- }
- if let configureProgressObserver = self.configureProgressObserver {
- nc.removeObserver(configureProgressObserver)
- }
- }
- // MARK: - Table view data source
- 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
- if section == 0 {
- return 2
- } else {
- return 0
- }
- }
-
- override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- if section == 1 {
- return "Advanced"
- } else {
- 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, titleForFooterInSection section: Int) -> String? {
- if section == 0 {
- return "There are no Delta Chat servers, your data stays on your device!"
- } else {
- return nil
- }
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let row = indexPath.row
- if row == 0 {
- return emailCell
- } else {
- return passwordCell
- }
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // handle tap on password
- print(indexPath)
- }
- @objc func loginButtonPressed() {
- 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 = 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(
- forName: dcNotificationBackupProgress,
- object: nil,
- queue: nil
- ) {
- notification in
- if let ui = notification.userInfo {
- if ui["error"] as! Bool {
- self.hudHandler.setHudError(ui["errorMessage"] as? String)
- } else if ui["done"] as! Bool {
- self.hudHandler.setHudDone(callback: nil)
- } else {
- self.hudHandler.setHudProgress(ui["progress"] as! Int)
- }
- }
- }
- configureProgressObserver = nc.addObserver(
- forName: dcNotificationConfigureProgress,
- object: nil,
- queue: nil
- ) {
- notification in
- if let ui = notification.userInfo {
- if ui["error"] as! Bool {
- self.hudHandler.setHudError(ui["errorMessage"] as? String)
- } else if ui["done"] as! Bool {
- 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()
- }
- 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
- }
- }
- 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
- }()
- */
- 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
- }
- }
|