FederationInvitationTableViewController.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. class FederationInvitationTableViewController: UITableViewController, FederationInvitationCellDelegate {
  6. private let federationInvitationCellIdentifier = "FederationInvitationCell"
  7. private var pendingInvitations: [FederationInvitation] = []
  8. var backgroundView: PlaceholderView = PlaceholderView(for: .grouped)
  9. var modifyingViewIndicator = UIActivityIndicatorView()
  10. init() {
  11. super.init(style: .insetGrouped)
  12. }
  13. required init?(coder: NSCoder) {
  14. fatalError("init(coder:) has not been implemented")
  15. }
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.navigationController?.navigationBar.isTranslucent = false
  19. self.navigationItem.title = NSLocalizedString("Pending invitations", comment: "")
  20. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCAppBranding.themeTextColor()]
  21. self.navigationController?.navigationBar.tintColor = NCAppBranding.themeTextColor()
  22. self.navigationController?.navigationBar.barTintColor = NCAppBranding.themeColor()
  23. self.tabBarController?.tabBar.tintColor = NCAppBranding.themeColor()
  24. let themeColor: UIColor = NCAppBranding.themeColor()
  25. let appearance = UINavigationBarAppearance()
  26. appearance.configureWithOpaqueBackground()
  27. appearance.backgroundColor = themeColor
  28. appearance.titleTextAttributes = [.foregroundColor: NCAppBranding.themeTextColor()]
  29. self.navigationItem.standardAppearance = appearance
  30. self.navigationItem.compactAppearance = appearance
  31. self.navigationItem.scrollEdgeAppearance = appearance
  32. let barButtonItem = UIBarButtonItem(title: nil, style: .plain, target: nil, action: nil)
  33. barButtonItem.primaryAction = UIAction(title: NSLocalizedString("Close", comment: ""), handler: { [unowned self] _ in
  34. self.dismiss(animated: true)
  35. })
  36. self.navigationItem.rightBarButtonItems = [barButtonItem]
  37. self.tableView.register(UINib(nibName: federationInvitationCellIdentifier, bundle: nil), forCellReuseIdentifier: federationInvitationCellIdentifier)
  38. self.tableView.backgroundView = backgroundView
  39. self.backgroundView.placeholderView.isHidden = true
  40. self.backgroundView.loadingView.startAnimating()
  41. self.modifyingViewIndicator.color = NCAppBranding.themeTextColor()
  42. }
  43. override func viewWillAppear(_ animated: Bool) {
  44. self.getData()
  45. }
  46. func getData() {
  47. let activeAccount = NCDatabaseManager.sharedInstance().activeAccount()
  48. NCAPIController.sharedInstance().getFederationInvitations(for: activeAccount.accountId) { [weak self] invitations in
  49. guard let self else { return }
  50. if let invitations {
  51. // TODO: For now the invitation endpoint also returns accepted invitations, we only want to have pending
  52. self.pendingInvitations = invitations.filter { $0.invitationState != .accepted }
  53. NCDatabaseManager.sharedInstance().setPendingFederationInvitationForAccountId(activeAccount.accountId, with: self.pendingInvitations.count)
  54. if self.pendingInvitations.isEmpty {
  55. self.dismiss(animated: true)
  56. return
  57. }
  58. } else {
  59. self.pendingInvitations = []
  60. }
  61. self.backgroundView.loadingView.stopAnimating()
  62. self.backgroundView.loadingView.isHidden = true
  63. self.tableView.reloadData()
  64. self.hideActivityIndicator()
  65. }
  66. }
  67. func showActivityIndicator() {
  68. self.modifyingViewIndicator.startAnimating()
  69. self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: modifyingViewIndicator)
  70. }
  71. func hideActivityIndicator() {
  72. self.modifyingViewIndicator.stopAnimating()
  73. self.navigationItem.leftBarButtonItem = nil
  74. }
  75. // MARK: - Table view data source
  76. override func numberOfSections(in tableView: UITableView) -> Int {
  77. return 1
  78. }
  79. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  80. return pendingInvitations.count
  81. }
  82. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  83. guard let cell = self.tableView.dequeueReusableCell(withIdentifier: federationInvitationCellIdentifier, for: indexPath) as? FederationInvitationCell
  84. else { return UITableViewCell() }
  85. let pendingInvitation = self.pendingInvitations[indexPath.row]
  86. cell.setupForInvitation(invitation: pendingInvitation)
  87. cell.delegate = self
  88. return cell
  89. }
  90. func federationInvitationCellAccept(_ cell: FederationInvitationCell, invitation: FederationInvitation) {
  91. self.showActivityIndicator()
  92. cell.setDisabledState()
  93. NCAPIController.sharedInstance().acceptFederationInvitation(for: invitation.accountId, with: invitation.invitationId) { [weak self] success in
  94. if !success {
  95. NotificationPresenter.shared().present(text: NSLocalizedString("Failed to accept invitation", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error)
  96. }
  97. self?.getData()
  98. }
  99. }
  100. func federationInvitationCellReject(_ cell: FederationInvitationCell, invitation: FederationInvitation) {
  101. self.showActivityIndicator()
  102. cell.setDisabledState()
  103. NCAPIController.sharedInstance().rejectFederationInvitation(for: invitation.accountId, with: invitation.invitationId) { [weak self] success in
  104. if !success {
  105. NotificationPresenter.shared().present(text: NSLocalizedString("Failed to reject invitation", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error)
  106. }
  107. self?.getData()
  108. }
  109. }
  110. }