NCAccountRequest.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // NCRenameFile.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. class NCAccountRequest: UIViewController {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var tableView: UITableView!
  28. @IBOutlet weak var okButton: UIButton!
  29. @IBOutlet weak var progressView: UIProgressView!
  30. public var accounts: [tableAccount] = []
  31. private var timer: Timer?
  32. private var time: Float = 0
  33. private let secondsAutoDismiss: Float = 3
  34. // MARK: - Life Cycle
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. titleLabel.text = NSLocalizedString("_account_request_", comment: "")
  38. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  39. okButton.setTitle(NSLocalizedString("_ok_", comment: ""), for: .normal)
  40. okButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  41. okButton.layer.cornerRadius = 15
  42. okButton.layer.masksToBounds = true
  43. okButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  44. progressView.tintColor = NCBrandColor.shared.brandElement
  45. progressView.trackTintColor = .clear
  46. progressView.progress = 1
  47. NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  48. }
  49. override func viewWillAppear(_ animated: Bool) {
  50. super.viewWillAppear(animated)
  51. }
  52. override func viewDidAppear(_ animated: Bool) {
  53. super.viewDidAppear(animated)
  54. }
  55. override func viewWillDisappear(_ animated: Bool) {
  56. super.viewWillDisappear(animated)
  57. timer?.invalidate()
  58. }
  59. // MARK: - Progress
  60. @objc func startTimer() {
  61. time = 0
  62. timer?.invalidate()
  63. timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
  64. }
  65. @objc func updateProgress() {
  66. time += 0.1
  67. if time >= secondsAutoDismiss {
  68. dismiss(animated: true)
  69. } else {
  70. progressView.progress = 1 - (time / secondsAutoDismiss)
  71. }
  72. }
  73. // MARK: - Action
  74. @IBAction func ok(_ sender: Any) {
  75. dismiss(animated: true)
  76. }
  77. }
  78. extension NCAccountRequest: UITableViewDelegate {
  79. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  80. timer?.invalidate()
  81. progressView.progress = 1
  82. }
  83. func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  84. if decelerate {
  85. startTimer()
  86. }
  87. }
  88. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  89. startTimer()
  90. }
  91. }
  92. extension NCAccountRequest: UITableViewDataSource {
  93. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  94. return accounts.count
  95. }
  96. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  97. let account = accounts[indexPath.row]
  98. var avatar = UIImage(named: "avatarCredentials")
  99. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  100. cell.tintColor = NCBrandColor.shared.customer
  101. let avatarImage = cell.viewWithTag(10) as? UIImageView
  102. let accountLabel = cell.viewWithTag(20) as? UILabel
  103. let activeImage = cell.viewWithTag(30) as? UIImageView
  104. avatarImage?.image = UIImage(named: "avatarCredentials")
  105. avatarImage?.image = UIImage(named: "avatarCredentials")
  106. var fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(account.user, urlBase: account.urlBase) + "-" + account.user
  107. fileNamePath = fileNamePath + ".png"
  108. if var userImage = UIImage(contentsOfFile: fileNamePath) {
  109. userImage = userImage.resizeImage(size: CGSize(width: 50, height: 50), isAspectRation: true)!
  110. let userImageView = UIImageView(image: userImage)
  111. userImageView.avatar(roundness: 2, borderWidth: 1, borderColor: NCBrandColor.shared.avatarBorder, backgroundColor: .clear)
  112. UIGraphicsBeginImageContext(userImageView.bounds.size)
  113. userImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
  114. if let newAvatar = UIGraphicsGetImageFromCurrentImageContext() {
  115. avatar = newAvatar
  116. }
  117. UIGraphicsEndImageContext()
  118. }
  119. avatarImage?.image = avatar
  120. accountLabel?.text = account.user + " " + (URL(string: account.urlBase)?.host ?? "")
  121. if account.active {
  122. activeImage?.image = UIImage(named: "check")
  123. } else {
  124. activeImage?.image = nil
  125. }
  126. return cell
  127. }
  128. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  129. dismiss(animated: true)
  130. }
  131. }