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