NCAppConfigView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 UIKit
  24. import NCCommunication
  25. class NCAppConfigView: UIViewController {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. private var serverUrl: String?
  28. private var username: String?
  29. private var password: String?
  30. @IBOutlet weak var logoImage: UIImageView!
  31. @IBOutlet weak var titleLabel: UILabel!
  32. // MARK: - View Life Cycle
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. self.view.backgroundColor = NCBrandColor.shared.brandElement
  36. titleLabel.textColor = NCBrandColor.shared.brandText
  37. titleLabel.text = NSLocalizedString("_appconfig_view_title_", comment: "")
  38. if let serverConfig = UserDefaults.standard.dictionary(forKey: NCBrandConfiguration.shared.configuration_bundleId) {
  39. serverUrl = serverConfig[NCBrandConfiguration.shared.configuration_serverUrl] as? String
  40. username = serverConfig[NCBrandConfiguration.shared.configuration_username] as? String
  41. password = serverConfig[NCBrandConfiguration.shared.configuration_password] as? String
  42. } else {
  43. serverUrl = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_serverUrl)
  44. username = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_username)
  45. password = UserDefaults.standard.string(forKey: NCBrandConfiguration.shared.configuration_password)
  46. }
  47. }
  48. override func viewDidAppear(_ animated: Bool) {
  49. super.viewDidAppear(animated)
  50. // Stop timer error network
  51. appDelegate.timerErrorNetworking?.invalidate()
  52. guard let serverUrl = self.serverUrl else {
  53. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, serverUrl not found", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, forced: true)
  54. return
  55. }
  56. guard let username = self.username else {
  57. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, username not found", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, forced: true)
  58. return
  59. }
  60. guard let password = self.password else {
  61. NCContentPresenter.shared.messageNotification("_error_", description: "User Default, password not found", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, forced: true)
  62. return
  63. }
  64. NCCommunication.shared.getAppPassword(serverUrl: serverUrl, username: username, password: password, userAgent: nil) { (token, errorCode, errorDescription) in
  65. DispatchQueue.main.async {
  66. if errorCode == 0 && token != nil {
  67. let account: String = "\(username) \(serverUrl)"
  68. // NO account found, clear
  69. if NCManageDatabase.shared.getAccounts() == nil { NCUtility.shared.removeAllSettings() }
  70. // Add new account
  71. NCManageDatabase.shared.deleteAccount(account)
  72. NCManageDatabase.shared.addAccount(account, urlBase: serverUrl, user: username, password: token!)
  73. guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else {
  74. NCContentPresenter.shared.messageNotification("_error_", description: "setAccountActive error", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, forced: true)
  75. self.dismiss(animated: true, completion: nil)
  76. return
  77. }
  78. self.appDelegate.settingAccount(account, urlBase: serverUrl, user: username, userId: tableAccount.userId, password: token!)
  79. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterInitializeMain)
  80. self.dismiss(animated: true) {}
  81. } else {
  82. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  83. }
  84. }
  85. }
  86. }
  87. override func viewDidDisappear(_ animated: Bool) {
  88. super.viewDidDisappear(animated)
  89. // Start timer error network
  90. appDelegate.startTimerErrorNetworking()
  91. }
  92. }