NCAccountRequest.swift 7.5 KB

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