NCAccountRequest.swift 6.5 KB

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