NCShareAccounts.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // NCShareAccounts.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 NCShareAccountsDelegate: AnyObject {
  26. func selected(url: String, user: String)
  27. }
  28. // optional func
  29. public extension NCShareAccountsDelegate {
  30. func selected(url: String, user: String) {}
  31. }
  32. class NCShareAccounts: UIViewController {
  33. @IBOutlet weak var titleLabel: UILabel!
  34. @IBOutlet weak var tableView: UITableView!
  35. public var accounts: [NKShareAccounts.DataAccounts] = []
  36. public let heightCell: CGFloat = 60
  37. public var enableTimerProgress: Bool = true
  38. public var dismissDidEnterBackground: Bool = true
  39. public weak var delegate: NCShareAccountsDelegate?
  40. // MARK: - View Life Cycle
  41. override func viewDidLoad() {
  42. super.viewDidLoad()
  43. titleLabel.text = NSLocalizedString("_account_select_to_add_", comment: "")
  44. tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
  45. tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  46. view.backgroundColor = .secondarySystemBackground
  47. tableView.backgroundColor = .secondarySystemBackground
  48. }
  49. override func viewDidAppear(_ animated: Bool) {
  50. super.viewDidAppear(animated)
  51. let visibleCells = tableView.visibleCells
  52. if visibleCells.count == accounts.count {
  53. tableView.isScrollEnabled = false
  54. }
  55. }
  56. }
  57. extension NCShareAccounts: UITableViewDelegate {
  58. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  59. return heightCell
  60. }
  61. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  62. dismiss(animated: true) {
  63. let account = self.accounts[indexPath.row]
  64. self.delegate?.selected(url: account.url, user: account.user)
  65. }
  66. }
  67. }
  68. extension NCShareAccounts: UITableViewDataSource {
  69. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  70. return accounts.count
  71. }
  72. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  73. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  74. cell.backgroundColor = tableView.backgroundColor
  75. let avatarImage = cell.viewWithTag(10) as? UIImageView
  76. let userLabel = cell.viewWithTag(20) as? UILabel
  77. let urlLabel = cell.viewWithTag(30) as? UILabel
  78. userLabel?.text = ""
  79. urlLabel?.text = ""
  80. let account = accounts[indexPath.row]
  81. if let image = account.image {
  82. avatarImage?.image = image
  83. }
  84. if let name = account.name, !name.isEmpty {
  85. userLabel?.text = name.uppercased() + " (\(account.user))"
  86. } else {
  87. userLabel?.text = account.user.uppercased()
  88. }
  89. urlLabel?.text = (URL(string: account.url)?.host ?? "")
  90. return cell
  91. }
  92. }