NCAccountRequest.swift 8.3 KB

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