NCTalkAccounts.swift 5.9 KB

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