NCAccountRequest.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 tableView: UITableView!
  37. @IBOutlet weak var progressView: UIProgressView!
  38. public var accounts: [tableAccount] = []
  39. public let heightCell: CGFloat = 80
  40. public var enableTimerProgress: Bool = true
  41. public var enableAddAccount: Bool = false
  42. public var dismissDidEnterBackground: Bool = false
  43. public var delegate: NCAccountRequestDelegate?
  44. private var timer: Timer?
  45. private var time: Float = 0
  46. private let secondsAutoDismiss: Float = 3
  47. // MARK: - Life Cycle
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. titleLabel.text = NSLocalizedString("_accounts_", comment: "")
  51. view.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  52. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  53. tableView.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  54. progressView.tintColor = NCBrandColor.shared.brandElement
  55. progressView.trackTintColor = .clear
  56. progressView.progress = 1
  57. if enableTimerProgress {
  58. progressView.isHidden = false
  59. } else {
  60. progressView.isHidden = true
  61. }
  62. NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  63. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  64. }
  65. override func viewWillAppear(_ animated: Bool) {
  66. super.viewWillAppear(animated)
  67. }
  68. override func viewDidAppear(_ animated: Bool) {
  69. super.viewDidAppear(animated)
  70. }
  71. override func viewWillDisappear(_ animated: Bool) {
  72. super.viewWillDisappear(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 scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  107. if decelerate {
  108. // startTimer()
  109. }
  110. }
  111. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  112. // startTimer()
  113. }
  114. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  115. return heightCell
  116. }
  117. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  118. if indexPath.row == accounts.count {
  119. dismiss(animated: true)
  120. delegate?.accountRequestAddAccount()
  121. } else {
  122. let account = accounts[indexPath.row]
  123. let activeAccount = NCManageDatabase.shared.getActiveAccount()
  124. if account.account != activeAccount?.account {
  125. dismiss(animated: true) {
  126. self.delegate?.changeAccountRequestAddAccount(account: account.account)
  127. }
  128. } else {
  129. dismiss(animated: true)
  130. }
  131. }
  132. }
  133. }
  134. extension NCAccountRequest: UITableViewDataSource {
  135. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  136. if enableAddAccount {
  137. return accounts.count + 1
  138. } else {
  139. return accounts.count
  140. }
  141. }
  142. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  143. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  144. cell.backgroundColor = NCBrandColor.shared.secondarySystemBackground
  145. let avatarImage = cell.viewWithTag(10) as? UIImageView
  146. let userLabel = cell.viewWithTag(20) as? UILabel
  147. let urlLabel = cell.viewWithTag(30) as? UILabel
  148. let activeImage = cell.viewWithTag(40) as? UIImageView
  149. userLabel?.text = ""
  150. urlLabel?.text = ""
  151. if indexPath.row == accounts.count {
  152. avatarImage?.image = NCUtility.shared.loadImage(named: "plus").image(color: .systemBlue, size: 25)
  153. avatarImage?.contentMode = .center
  154. userLabel?.text = NSLocalizedString("_add_account_", comment: "")
  155. userLabel?.textColor = .systemBlue
  156. } else {
  157. let account = accounts[indexPath.row]
  158. avatarImage?.image = NCUtility.shared.loadImage(named: "person.crop.circle")
  159. let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(account.user, urlBase: account.urlBase)) + "-" + account.user + ".png"
  160. if let image = UIImage(contentsOfFile: fileNamePath) {
  161. avatarImage?.image = NCUtility.shared.createAvatar(image: image, size: 40)
  162. }
  163. if account.alias != "" {
  164. userLabel?.text = account.alias.uppercased()
  165. } else {
  166. userLabel?.text = account.user.uppercased()
  167. urlLabel?.text = (URL(string: account.urlBase)?.host ?? "")
  168. }
  169. if account.active {
  170. activeImage?.image = NCUtility.shared.loadImage(named: "checkmark").image(color: .systemBlue, size: 30)
  171. } else {
  172. activeImage?.image = nil
  173. }
  174. }
  175. return cell
  176. }
  177. }