NCAppConfigView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 NextcloudKit
  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. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "User Default, serverUrl not found")
  54. NCContentPresenter.shared.showError(error: error)
  55. return
  56. }
  57. guard let username = self.username else {
  58. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "User Default, username not found")
  59. NCContentPresenter.shared.showError(error: error)
  60. return
  61. }
  62. guard let password = self.password else {
  63. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "User Default, password not found")
  64. NCContentPresenter.shared.showError(error: error)
  65. return
  66. }
  67. NextcloudKit.shared.getAppPassword(serverUrl: serverUrl, username: username, password: password, userAgent: nil) { token, data, error in
  68. DispatchQueue.main.async {
  69. if error == .success && token != nil {
  70. let account: String = "\(username) \(serverUrl)"
  71. // NO account found, clear
  72. if NCManageDatabase.shared.getAccounts() == nil { NCUtility.shared.removeAllSettings() }
  73. // Add new account
  74. NCManageDatabase.shared.deleteAccount(account)
  75. NCManageDatabase.shared.addAccount(account, urlBase: serverUrl, user: username, password: token!)
  76. guard let tableAccount = NCManageDatabase.shared.setAccountActive(account) else {
  77. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "setAccountActive error")
  78. NCContentPresenter.shared.showError(error: error)
  79. self.dismiss(animated: true, completion: nil)
  80. return
  81. }
  82. self.appDelegate.settingAccount(account, urlBase: serverUrl, user: username, userId: tableAccount.userId, password: token!)
  83. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterInitialize)
  84. self.dismiss(animated: true) {}
  85. } else {
  86. NCContentPresenter.shared.showError(error: error)
  87. }
  88. }
  89. }
  90. }
  91. override func viewDidDisappear(_ animated: Bool) {
  92. super.viewDidDisappear(animated)
  93. // Start timer error network
  94. appDelegate.startTimerErrorNetworking()
  95. }
  96. }