NCAccountRequest.swift 8.8 KB

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