Skip to content

Commit

Permalink
Cleanup (IOS-241)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitschlag committed Jul 24, 2024
1 parent ddb73b7 commit 21b4313
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class NotificationFilteringBannerTableViewCell: UITableViewCell {
subtitleLabel.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 15, weight: .regular))
subtitleLabel.textColor = .secondaryLabel


labelStackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
labelStackView.translatesAutoresizingMaskIntoConstraints = false
labelStackView.alignment = .leading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import MastodonSDK
import MastodonLocalization

protocol AccountNotificationTimelineViewControllerDelegate: AnyObject {
func acceptRequest(_ viewController: AccountNotificationTimelineViewController, request: Mastodon.Entity.NotificationRequest, completion: @escaping (() -> Void))
func dismissRequest(_ viewController: AccountNotificationTimelineViewController, request: Mastodon.Entity.NotificationRequest, completion: @escaping (() -> Void))
func acceptRequest(_ viewController: AccountNotificationTimelineViewController, request: Mastodon.Entity.NotificationRequest)
func dismissRequest(_ viewController: AccountNotificationTimelineViewController, request: Mastodon.Entity.NotificationRequest)
}

class AccountNotificationTimelineViewController: NotificationTimelineViewController {
Expand All @@ -27,25 +27,20 @@ class AccountNotificationTimelineViewController: NotificationTimelineViewControl

// MARK: - Actions

//TODO: Localization
func menu() -> UIMenu {
let menu = UIMenu(children: [
UIAction(title: L10n.Scene.Notification.FilteredNotification.accept, image: UIImage(systemName: "checkmark")) { [weak self] _ in
guard let self else { return }

coordinator.showLoading()
self.delegate?.acceptRequest(self, request: request) {
self.navigationController?.popViewController(animated: true)
}
self.delegate?.acceptRequest(self, request: request)
coordinator.hideLoading()
},
UIAction(title: L10n.Scene.Notification.FilteredNotification.dismiss, image: UIImage(systemName: "speaker.slash")) { [weak self] _ in
guard let self else { return }

coordinator.showLoading()
self.delegate?.dismissRequest(self, request: request) {
self.navigationController?.popViewController(animated: true)
}
self.delegate?.dismissRequest(self, request: request)
coordinator.hideLoading()
}
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,7 @@ extension NotificationRequestsTableViewController: NotificationRequestTableViewC
Task { [weak self] in
guard let self else { return }
do {
_ = try await context.apiService.acceptNotificationRequests(authenticationBox: authContext.mastodonAuthenticationBox,
id: notificationRequest.id)

let requests = try await context.apiService.notificationRequests(authenticationBox: authContext.mastodonAuthenticationBox).value

NotificationCenter.default.post(name: .notificationFilteringChanged, object: nil)

if requests.count > 0 {

await MainActor.run { [weak self] in
guard let self else { return }
self.viewModel.requests = requests
var snapshot = NSDiffableDataSourceSnapshot<NotificationRequestsSection, NotificationRequestItem>()
snapshot.appendSections([.main])
snapshot.appendItems(self.viewModel.requests.compactMap { NotificationRequestItem.item($0) } )

self.dataSource?.apply(snapshot)
}
} else {
await MainActor.run { [weak self] in
_ = self?.navigationController?.popViewController(animated: true)
}
}

try await acceptNotificationRequest(notificationRequest)
} catch {
cell.acceptNotificationRequestActivityIndicatorView.stopAnimating()
cell.acceptNotificationRequestButton.tintColor = .white
Expand All @@ -157,7 +134,33 @@ extension NotificationRequestsTableViewController: NotificationRequestTableViewC
}
}
}


private func acceptNotificationRequest(_ notificationRequest: MastodonSDK.Mastodon.Entity.NotificationRequest) async throws {
_ = try await context.apiService.acceptNotificationRequests(authenticationBox: authContext.mastodonAuthenticationBox,
id: notificationRequest.id)

let requests = try await context.apiService.notificationRequests(authenticationBox: authContext.mastodonAuthenticationBox).value

NotificationCenter.default.post(name: .notificationFilteringChanged, object: nil)

if requests.count > 0 {
await MainActor.run { [weak self] in
guard let self else { return }
self.viewModel.requests = requests
var snapshot = NSDiffableDataSourceSnapshot<NotificationRequestsSection, NotificationRequestItem>()
snapshot.appendSections([.main])
snapshot.appendItems(self.viewModel.requests.compactMap { NotificationRequestItem.item($0) } )

self.dataSource?.apply(snapshot)
}
} else {
await MainActor.run { [weak self] in
_ = self?.navigationController?.popViewController(animated: true)
}
}

}

func rejectNotificationRequest(_ cell: NotificationRequestTableViewCell, notificationRequest: MastodonSDK.Mastodon.Entity.NotificationRequest) {

cell.rejectNotificationRequestActivityIndicatorView.isHidden = false
Expand Down Expand Up @@ -209,17 +212,15 @@ extension NotificationRequestsTableViewController: NotificationRequestTableViewC
}

extension NotificationRequestsTableViewController: AccountNotificationTimelineViewControllerDelegate {
func acceptRequest(_ viewController: AccountNotificationTimelineViewController, request: MastodonSDK.Mastodon.Entity.NotificationRequest, completion: @escaping (() -> Void)) {
func acceptRequest(_ viewController: AccountNotificationTimelineViewController, request: MastodonSDK.Mastodon.Entity.NotificationRequest) {
Task {
try? await rejectNotificationRequest(request)
completion()
try? await acceptNotificationRequest(request)
}
}

func dismissRequest(_ viewController: AccountNotificationTimelineViewController, request: MastodonSDK.Mastodon.Entity.NotificationRequest, completion: @escaping (() -> Void)) {
func dismissRequest(_ viewController: AccountNotificationTimelineViewController, request: MastodonSDK.Mastodon.Entity.NotificationRequest) {
Task {
try? await rejectNotificationRequest(request)
completion()
}
}
}

0 comments on commit 21b4313

Please sign in to comment.