NCAccountRequest.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // NCAccountRequest.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/02/21.
  6. // Copyright © 2021 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. import NCCommunication
  25. class NCAccountRequest: UIViewController {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var tableView: UITableView!
  28. @IBOutlet weak var progressView: UIProgressView!
  29. public var accounts: [tableAccount] = []
  30. public let heightCell: CGFloat = 80
  31. public var enableTimerProgress: Bool = true
  32. public var enableAddAccount: Bool = false
  33. public var viewController: UIViewController?
  34. public var dismissDidEnterBackground: Bool = false
  35. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. private var timer: Timer?
  37. private var time: Float = 0
  38. private let secondsAutoDismiss: Float = 3
  39. // MARK: - Life Cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. titleLabel.text = NSLocalizedString("_accounts_", comment: "")
  43. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  44. progressView.tintColor = NCBrandColor.shared.brandElement
  45. progressView.trackTintColor = .clear
  46. progressView.progress = 1
  47. if enableTimerProgress {
  48. progressView.isHidden = false
  49. } else {
  50. progressView.isHidden = true
  51. }
  52. NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  53. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil)
  54. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  55. changeTheming()
  56. }
  57. override func viewWillAppear(_ animated: Bool) {
  58. super.viewWillAppear(animated)
  59. }
  60. override func viewDidAppear(_ animated: Bool) {
  61. super.viewDidAppear(animated)
  62. }
  63. override func viewWillDisappear(_ animated: Bool) {
  64. super.viewWillDisappear(animated)
  65. timer?.invalidate()
  66. }
  67. // MARK: - NotificationCenter
  68. @objc func changeTheming() {
  69. view.backgroundColor = NCBrandColor.shared.backgroundForm
  70. tableView.backgroundColor = NCBrandColor.shared.backgroundForm
  71. tableView.reloadData()
  72. }
  73. @objc func applicationDidEnterBackground() {
  74. if dismissDidEnterBackground {
  75. dismiss(animated: false)
  76. }
  77. }
  78. // MARK: - Progress
  79. @objc func startTimer() {
  80. if enableTimerProgress {
  81. time = 0
  82. timer?.invalidate()
  83. timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
  84. progressView.isHidden = false
  85. } else {
  86. progressView.isHidden = true
  87. }
  88. }
  89. @objc func updateProgress() {
  90. time += 0.1
  91. if time >= secondsAutoDismiss {
  92. dismiss(animated: true)
  93. } else {
  94. progressView.progress = 1 - (time / secondsAutoDismiss)
  95. }
  96. }
  97. }
  98. extension NCAccountRequest: UITableViewDelegate {
  99. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  100. timer?.invalidate()
  101. progressView.progress = 0
  102. }
  103. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  104. if decelerate {
  105. // startTimer()
  106. }
  107. }
  108. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  109. // startTimer()
  110. }
  111. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  112. return heightCell
  113. }
  114. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  115. if indexPath.row == accounts.count {
  116. dismiss(animated: true)
  117. appDelegate.openLogin(viewController: viewController, selector: NCGlobal.shared.introLogin, openLoginWeb: false)
  118. } else {
  119. let account = accounts[indexPath.row]
  120. if account.account != appDelegate.account {
  121. NCManageDatabase.shared.setAccountActive(account.account)
  122. dismiss(animated: true) {
  123. NCOperationQueue.shared.cancelAllQueue()
  124. NCNetworking.shared.cancelAllTask()
  125. self.appDelegate.settingAccount(account.account, urlBase: account.urlBase, user: account.user, userId: account.userId, password: CCUtility.getPassword(account.account))
  126. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterInitializeMain)
  127. }
  128. } else {
  129. dismiss(animated: true)
  130. }
  131. }
  132. }
  133. }
  134. extension NCAccountRequest: UITableViewDataSource {
  135. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  136. if enableAddAccount {
  137. return accounts.count + 1
  138. } else {
  139. return accounts.count
  140. }
  141. }
  142. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  143. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  144. cell.backgroundColor = NCBrandColor.shared.backgroundForm
  145. let avatarImage = cell.viewWithTag(10) as? UIImageView
  146. let userLabel = cell.viewWithTag(20) as? UILabel
  147. let urlLabel = cell.viewWithTag(30) as? UILabel
  148. let activeImage = cell.viewWithTag(40) as? UIImageView
  149. userLabel?.text = ""
  150. urlLabel?.text = ""
  151. if indexPath.row == accounts.count {
  152. avatarImage?.image = NCUtility.shared.loadImage(named: "plus").image(color: .systemBlue, size: 25)
  153. avatarImage?.contentMode = .center
  154. userLabel?.text = NSLocalizedString("_add_account_", comment: "")
  155. userLabel?.textColor = .systemBlue
  156. } else {
  157. let account = accounts[indexPath.row]
  158. avatarImage?.image = NCUtility.shared.loadImage(named: "person.crop.circle")
  159. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(account.user, urlBase: account.urlBase)) + "-" + account.user + ".png"
  160. if let image = UIImage(contentsOfFile: fileNamePath) {
  161. avatarImage?.image = NCUtility.shared.createAvatar(image: image, size: 40)
  162. }
  163. if account.alias != "" {
  164. userLabel?.text = account.alias.uppercased()
  165. } else {
  166. userLabel?.text = account.user.uppercased()
  167. urlLabel?.text = (URL(string: account.urlBase)?.host ?? "")
  168. }
  169. if account.active {
  170. activeImage?.image = NCUtility.shared.loadImage(named: "checkmark").image(color: .systemBlue, size: 30)
  171. } else {
  172. activeImage?.image = nil
  173. }
  174. }
  175. return cell
  176. }
  177. }