NCShareAccounts.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 viewWillAppear(_ animated: Bool) {
  50. super.viewWillAppear(animated)
  51. }
  52. override func viewDidAppear(_ animated: Bool) {
  53. super.viewDidAppear(animated)
  54. let visibleCells = tableView.visibleCells
  55. if visibleCells.count == accounts.count {
  56. tableView.isScrollEnabled = false
  57. }
  58. }
  59. }
  60. extension NCShareAccounts: UITableViewDelegate {
  61. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  62. return heightCell
  63. }
  64. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  65. dismiss(animated: true) {
  66. let account = self.accounts[indexPath.row]
  67. self.delegate?.selected(url: account.url, user: account.user)
  68. }
  69. }
  70. }
  71. extension NCShareAccounts: UITableViewDataSource {
  72. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  73. return accounts.count
  74. }
  75. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  76. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
  77. cell.backgroundColor = tableView.backgroundColor
  78. let avatarImage = cell.viewWithTag(10) as? UIImageView
  79. let userLabel = cell.viewWithTag(20) as? UILabel
  80. let urlLabel = cell.viewWithTag(30) as? UILabel
  81. userLabel?.text = ""
  82. urlLabel?.text = ""
  83. let account = accounts[indexPath.row]
  84. if let image = account.image {
  85. avatarImage?.image = image
  86. }
  87. if let name = account.name, !name.isEmpty {
  88. userLabel?.text = name.uppercased() + " (\(account.user))"
  89. } else {
  90. userLabel?.text = account.user.uppercased()
  91. }
  92. urlLabel?.text = (URL(string: account.url)?.host ?? "")
  93. return cell
  94. }
  95. }