NCTalkAccounts.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // NCTalkAccounts.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/11/22.
  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 NextcloudKit
  25. public protocol NCTalkAccountsDelegate: AnyObject {
  26. func selected(url: String, user: String)
  27. }
  28. // optional func
  29. public extension NCTalkAccountsDelegate {
  30. func selected(url: String, user: String) {}
  31. }
  32. class NCTalkAccounts: UIViewController {
  33. @IBOutlet weak var titleLabel: UILabel!
  34. @IBOutlet weak var closeButton: UIButton!
  35. @IBOutlet weak var tableView: UITableView!
  36. @IBOutlet weak var progressView: UIProgressView!
  37. public var accounts: [NKDataAccountFile] = []
  38. public let heightCell: CGFloat = 60
  39. public var enableTimerProgress: Bool = true
  40. public var dismissDidEnterBackground: Bool = true
  41. public weak var delegate: NCTalkAccountsDelegate?
  42. private var timer: Timer?
  43. private var time: Float = 0
  44. private let secondsAutoDismiss: Float = 3
  45. // MARK: - View Life Cycle
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. titleLabel.text = NSLocalizedString("_account_select_to_add_", comment: "")
  49. closeButton.setImage(NCUtility.shared.loadImage(named: "xmark", color: .label), for: .normal)
  50. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  51. // tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  52. view.backgroundColor = .secondarySystemBackground
  53. tableView.backgroundColor = .secondarySystemBackground
  54. progressView.trackTintColor = .clear
  55. progressView.progress = 1
  56. if enableTimerProgress {
  57. progressView.isHidden = false
  58. } else {
  59. progressView.isHidden = true
  60. }
  61. NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
  62. NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
  63. }
  64. override func viewWillAppear(_ animated: Bool) {
  65. super.viewWillAppear(animated)
  66. }
  67. override func viewDidAppear(_ animated: Bool) {
  68. super.viewDidAppear(animated)
  69. let visibleCells = tableView.visibleCells
  70. if visibleCells.count == accounts.count {
  71. tableView.isScrollEnabled = false
  72. }
  73. }
  74. override func viewWillDisappear(_ animated: Bool) {
  75. super.viewWillDisappear(animated)
  76. timer?.invalidate()
  77. }
  78. // MARK: - Action
  79. @IBAction func actionClose(_ sender: UIButton) {
  80. dismiss(animated: true)
  81. }
  82. // MARK: - NotificationCenter
  83. @objc func applicationDidEnterBackground() {
  84. if dismissDidEnterBackground {
  85. dismiss(animated: false)
  86. }
  87. }
  88. // MARK: - Progress
  89. @objc func startTimer() {
  90. if enableTimerProgress {
  91. time = 0
  92. timer?.invalidate()
  93. timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
  94. progressView.isHidden = false
  95. } else {
  96. progressView.isHidden = true
  97. }
  98. }
  99. @objc func updateProgress() {
  100. time += 0.1
  101. if time >= secondsAutoDismiss {
  102. dismiss(animated: true)
  103. } else {
  104. progressView.progress = 1 - (time / secondsAutoDismiss)
  105. }
  106. }
  107. }
  108. extension NCTalkAccounts: UITableViewDelegate {
  109. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  110. timer?.invalidate()
  111. progressView.progress = 0
  112. }
  113. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  114. return heightCell
  115. }
  116. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  117. dismiss(animated: true) {
  118. let account = self.accounts[indexPath.row]
  119. self.delegate?.selected(url: account.url, user: account.user)
  120. }
  121. }
  122. }
  123. extension NCTalkAccounts: UITableViewDataSource {
  124. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  125. return accounts.count
  126. }
  127. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  128. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  129. cell.backgroundColor = tableView.backgroundColor
  130. let avatarImage = cell.viewWithTag(10) as? UIImageView
  131. let userLabel = cell.viewWithTag(20) as? UILabel
  132. let urlLabel = cell.viewWithTag(30) as? UILabel
  133. userLabel?.text = ""
  134. urlLabel?.text = ""
  135. let account = accounts[indexPath.row]
  136. if let avatarPath = account.avatar, !avatarPath.isEmpty, let avatarImage = avatarImage {
  137. do {
  138. let data = try Data(contentsOf: URL(fileURLWithPath: avatarPath))
  139. if let image = UIImage(data: data) {
  140. avatarImage.image = image
  141. }
  142. } catch { print("Error: \(error)") }
  143. }
  144. if let alias = account.alias, !alias.isEmpty {
  145. userLabel?.text = alias.uppercased() + " (\(account.user))"
  146. } else {
  147. userLabel?.text = account.user.uppercased()
  148. }
  149. urlLabel?.text = (URL(string: account.url)?.host ?? "")
  150. return cell
  151. }
  152. }