NCAppConfigView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // NCAppConfigView.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/09/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. @objc protocol NCAppConfigViewDelegate: class {
  25. func loginSuccess(_: NSInteger)
  26. @objc optional func appConfigViewDismiss()
  27. }
  28. class NCAppConfigView: UIViewController {
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. @objc var serverUrl: String?
  31. @objc var username: String?
  32. @objc var password: String?
  33. @objc weak var delegate: NCAppConfigViewDelegate?
  34. @IBOutlet weak var logoImage: UIImageView!
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. self.view.backgroundColor = NCBrandColor.sharedInstance.brand
  38. let serverConfig = UserDefaults.standard.dictionary(forKey: NCBrandConfiguration.sharedInstance.configuration_key)
  39. serverUrl = serverConfig?[NCBrandConfiguration.sharedInstance.configuration_serverUrl] as? String
  40. username = serverConfig?[NCBrandConfiguration.sharedInstance.configuration_username] as? String
  41. password = serverConfig?[NCBrandConfiguration.sharedInstance.configuration_password] as? String
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. // Stop timer error network
  46. appDelegate.timerErrorNetworking.invalidate()
  47. guard let serverUrl = self.serverUrl else {
  48. appDelegate.messageNotification("_error_", description: "User Default, serverUrl not found", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  49. return
  50. }
  51. guard let username = self.username else {
  52. appDelegate.messageNotification("_error_", description: "User Default, username not found", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  53. return
  54. }
  55. guard let password = self.password else {
  56. appDelegate.messageNotification("_error_", description: "User Default, password not found", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  57. return
  58. }
  59. OCNetworking.sharedManager()?.getAppPassword(serverUrl, username: username, password: password, completion: { (token, message, errorCode) in
  60. if errorCode == 0 {
  61. let account: String = "\(username) \(serverUrl)"
  62. // NO account found, clear
  63. if NCManageDatabase.sharedInstance.getAccounts() == nil {
  64. NCUtility.sharedInstance.removeAllSettings()
  65. }
  66. // Add new account
  67. NCManageDatabase.sharedInstance.deleteAccount(account)
  68. NCManageDatabase.sharedInstance.addAccount(account, url: serverUrl, user: username, password: password, loginFlow: true)
  69. guard let tableAccount = NCManageDatabase.sharedInstance.setAccountActive(account) else {
  70. self.appDelegate.messageNotification("_error_", description: "setAccountActive error", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: 0)
  71. self.dismiss(animated: true, completion: nil)
  72. return
  73. }
  74. self.appDelegate.settingActiveAccount(account, activeUrl: serverUrl, activeUser: username, activeUserID: tableAccount.userID, activePassword: password)
  75. self.dismiss(animated: true) {
  76. self.delegate?.loginSuccess(NSInteger(k_login_Add))
  77. }
  78. } else {
  79. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  80. }
  81. })
  82. }
  83. override func viewDidDisappear(_ animated: Bool) {
  84. super.viewDidDisappear(animated)
  85. // Start timer error network
  86. appDelegate.startTimerErrorNetworking()
  87. }
  88. }