NCAccountRequest.swift 6.7 KB

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