BannedActorTableViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. @objcMembers class BannedActorTableViewController: UITableViewController, BannedActorCellDelegate {
  6. private let bannedActorCellIdentifier = "BannedActorCell"
  7. private var bannedActors: [BannedActor] = []
  8. var room: NCRoom
  9. var backgroundView: PlaceholderView = PlaceholderView(for: .grouped)
  10. var modifyingViewIndicator = UIActivityIndicatorView()
  11. init(room: NCRoom) {
  12. self.room = room
  13. super.init(style: .insetGrouped)
  14. }
  15. required init?(coder: NSCoder) {
  16. fatalError("init(coder:) has not been implemented")
  17. }
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. self.navigationController?.navigationBar.isTranslucent = false
  21. self.navigationItem.title = NSLocalizedString("Banned users and guests", comment: "")
  22. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCAppBranding.themeTextColor()]
  23. self.navigationController?.navigationBar.tintColor = NCAppBranding.themeTextColor()
  24. self.navigationController?.navigationBar.barTintColor = NCAppBranding.themeColor()
  25. self.tabBarController?.tabBar.tintColor = NCAppBranding.themeColor()
  26. let themeColor: UIColor = NCAppBranding.themeColor()
  27. let appearance = UINavigationBarAppearance()
  28. appearance.configureWithOpaqueBackground()
  29. appearance.backgroundColor = themeColor
  30. appearance.titleTextAttributes = [.foregroundColor: NCAppBranding.themeTextColor()]
  31. self.navigationItem.standardAppearance = appearance
  32. self.navigationItem.compactAppearance = appearance
  33. self.navigationItem.scrollEdgeAppearance = appearance
  34. self.tableView.register(UINib(nibName: bannedActorCellIdentifier, bundle: nil), forCellReuseIdentifier: bannedActorCellIdentifier)
  35. self.tableView.backgroundView = backgroundView
  36. self.backgroundView.placeholderView.isHidden = true
  37. self.backgroundView.placeholderTextView.text = NSLocalizedString("No banned users or guests", comment: "")
  38. self.backgroundView.setImage(UIImage(systemName: "person.badge.minus"))
  39. self.backgroundView.loadingView.startAnimating()
  40. self.modifyingViewIndicator.color = NCAppBranding.themeTextColor()
  41. }
  42. override func viewWillAppear(_ animated: Bool) {
  43. self.getData()
  44. }
  45. func getData() {
  46. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  47. NCAPIController.sharedInstance().listBans(for: activeAccount.accountId, in: room.token) { [weak self] bannedActors in
  48. guard let self else { return }
  49. self.bannedActors = bannedActors ?? []
  50. self.backgroundView.loadingView.stopAnimating()
  51. self.backgroundView.loadingView.isHidden = true
  52. self.backgroundView.placeholderView.isHidden = !self.bannedActors.isEmpty
  53. self.tableView.reloadData()
  54. self.hideActivityIndicator()
  55. }
  56. }
  57. func showActivityIndicator() {
  58. self.modifyingViewIndicator.startAnimating()
  59. self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: modifyingViewIndicator)
  60. }
  61. func hideActivityIndicator() {
  62. self.modifyingViewIndicator.stopAnimating()
  63. self.navigationItem.leftBarButtonItem = nil
  64. }
  65. // MARK: - Table view data source
  66. override func numberOfSections(in tableView: UITableView) -> Int {
  67. return 1
  68. }
  69. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  70. return bannedActors.count
  71. }
  72. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  73. guard let cell = self.tableView.dequeueReusableCell(withIdentifier: bannedActorCellIdentifier, for: indexPath) as? BannedActorCell
  74. else { return UITableViewCell() }
  75. let bannedActor = self.bannedActors[indexPath.row]
  76. cell.setupFor(bannedActor: bannedActor)
  77. cell.delegate = self
  78. return cell
  79. }
  80. func bannedActorCellUnbanActor(_ cell: BannedActorCell, bannedActor: BannedActor) {
  81. self.showActivityIndicator()
  82. cell.setDisabledState()
  83. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  84. NCAPIController.sharedInstance().unbanActor(for: activeAccount.accountId, in: self.room.token, with: bannedActor.banId) { [weak self] success in
  85. if !success {
  86. NotificationPresenter.shared().present(text: NSLocalizedString("Failed to unban selected entry", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error)
  87. }
  88. self?.getData()
  89. }
  90. }
  91. }