NCAccountRequest.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 UIKit
  24. import NextcloudKit
  25. public protocol NCAccountRequestDelegate: AnyObject {
  26. func accountRequestAddAccount()
  27. func accountRequestChangeAccount(account: String)
  28. }
  29. // optional func
  30. public extension NCAccountRequestDelegate {
  31. func accountRequestAddAccount() {}
  32. func accountRequestChangeAccount(account: String) {}
  33. }
  34. class NCAccountRequest: UIViewController {
  35. @IBOutlet weak var titleLabel: UILabel!
  36. @IBOutlet weak var tableView: UITableView!
  37. @IBOutlet weak var progressView: UIProgressView!
  38. public var accounts: [tableAccount] = []
  39. public var activeAccount: tableAccount?
  40. public let heightCell: CGFloat = 60
  41. public var enableTimerProgress: Bool = true
  42. public var enableAddAccount: Bool = false
  43. public var dismissDidEnterBackground: Bool = false
  44. public weak var delegate: NCAccountRequestDelegate?
  45. private var timer: Timer?
  46. private var time: Float = 0
  47. private let secondsAutoDismiss: Float = 3
  48. // MARK: - View Life Cycle
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. titleLabel.text = NSLocalizedString("_account_select_", comment: "")
  52. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  53. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  54. view.backgroundColor = .secondarySystemBackground
  55. tableView.backgroundColor = .secondarySystemBackground
  56. progressView.trackTintColor = .clear
  57. progressView.progress = 1
  58. if enableTimerProgress {
  59. progressView.isHidden = false
  60. } else {
  61. progressView.isHidden = true
  62. }
  63. NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  64. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  65. }
  66. override func viewWillAppear(_ animated: Bool) {
  67. super.viewWillAppear(animated)
  68. }
  69. override func viewDidAppear(_ animated: Bool) {
  70. super.viewDidAppear(animated)
  71. let visibleCells = tableView.visibleCells
  72. var numAccounts = accounts.count
  73. if enableAddAccount { numAccounts += 1 }
  74. if visibleCells.count == numAccounts {
  75. tableView.isScrollEnabled = false
  76. }
  77. }
  78. override func viewWillDisappear(_ animated: Bool) {
  79. super.viewWillDisappear(animated)
  80. timer?.invalidate()
  81. }
  82. // MARK: - NotificationCenter
  83. @objc func applicationDidEnterBackground() {
  84. if dismissDidEnterBackground {
  85. dismiss(animated: false)
  86. }
  87. }
  88. // MARK: - Progress
  89. @objc func startTimer() {
  90. if enableTimerProgress {
  91. time = 0
  92. timer?.invalidate()
  93. timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
  94. progressView.isHidden = false
  95. } else {
  96. progressView.isHidden = true
  97. }
  98. }
  99. @objc func updateProgress() {
  100. time += 0.1
  101. if time >= secondsAutoDismiss {
  102. dismiss(animated: true)
  103. } else {
  104. progressView.progress = 1 - (time / secondsAutoDismiss)
  105. }
  106. }
  107. }
  108. extension NCAccountRequest: UITableViewDelegate {
  109. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  110. timer?.invalidate()
  111. progressView.progress = 0
  112. }
  113. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  114. if decelerate {
  115. // startTimer()
  116. }
  117. }
  118. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  119. // startTimer()
  120. }
  121. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  122. return heightCell
  123. }
  124. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  125. if indexPath.row == accounts.count {
  126. dismiss(animated: true)
  127. delegate?.accountRequestAddAccount()
  128. } else {
  129. let account = accounts[indexPath.row]
  130. if account.account != activeAccount?.account {
  131. dismiss(animated: true) {
  132. self.delegate?.accountRequestChangeAccount(account: account.account)
  133. }
  134. } else {
  135. dismiss(animated: true)
  136. }
  137. }
  138. }
  139. }
  140. extension NCAccountRequest: UITableViewDataSource {
  141. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  142. if enableAddAccount {
  143. return accounts.count + 1
  144. } else {
  145. return accounts.count
  146. }
  147. }
  148. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  149. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  150. cell.backgroundColor = tableView.backgroundColor
  151. let avatarImage = cell.viewWithTag(10) as? UIImageView
  152. let userLabel = cell.viewWithTag(20) as? UILabel
  153. let urlLabel = cell.viewWithTag(30) as? UILabel
  154. let activeImage = cell.viewWithTag(40) as? UIImageView
  155. userLabel?.text = ""
  156. urlLabel?.text = ""
  157. if indexPath.row == accounts.count {
  158. avatarImage?.image = NCUtility.shared.loadImage(named: "plus").image(color: .systemBlue, size: 15)
  159. avatarImage?.contentMode = .center
  160. userLabel?.text = NSLocalizedString("_add_account_", comment: "")
  161. userLabel?.textColor = .systemBlue
  162. userLabel?.font = UIFont.systemFont(ofSize: 15)
  163. } else {
  164. let account = accounts[indexPath.row]
  165. avatarImage?.image = NCUtility.shared.loadUserImage(
  166. for: account.user,
  167. displayName: account.displayName,
  168. userBaseUrl: account)
  169. if account.alias.isEmpty {
  170. userLabel?.text = account.user.uppercased()
  171. urlLabel?.text = (URL(string: account.urlBase)?.host ?? "")
  172. } else {
  173. userLabel?.text = account.alias.uppercased()
  174. }
  175. if account.active {
  176. activeImage?.image = NCUtility.shared.loadImage(named: "checkmark").image(color: .systemBlue, size: 30)
  177. } else {
  178. activeImage?.image = nil
  179. }
  180. }
  181. return cell
  182. }
  183. }