NCAccountRequest.swift 7.0 KB

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