NCAccountRequest.swift 6.8 KB

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