From 5f6edff65dd7f7fe2b8669d2befecf22b376606a Mon Sep 17 00:00:00 2001 From: Ryan ZHAO <> Date: Thu, 28 Mar 2024 13:15:07 +1100 Subject: [PATCH] add error toast when failing to leave group/community --- Session/Home/HomeVC.swift | 9 +++-- Session/Home/HomeViewModel.swift | 4 +- .../UIContextualAction+Utilities.swift | 40 +++++++++++++++---- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Session/Home/HomeVC.swift b/Session/Home/HomeVC.swift index 7975d7dd7..abc18de93 100644 --- a/Session/Home/HomeVC.swift +++ b/Session/Home/HomeVC.swift @@ -668,7 +668,8 @@ final class HomeVC: BaseVC, SessionUtilRespondingViewController, UITableViewData indexPath: indexPath, tableView: tableView, threadViewModel: threadViewModel, - viewController: self + viewController: self, + navigatableStateHolder: viewModel ) ) @@ -689,7 +690,8 @@ final class HomeVC: BaseVC, SessionUtilRespondingViewController, UITableViewData indexPath: indexPath, tableView: tableView, threadViewModel: threadViewModel, - viewController: self + viewController: self, + navigatableStateHolder: viewModel ) ) @@ -735,7 +737,8 @@ final class HomeVC: BaseVC, SessionUtilRespondingViewController, UITableViewData indexPath: indexPath, tableView: tableView, threadViewModel: threadViewModel, - viewController: self + viewController: self, + navigatableStateHolder: viewModel ) ) diff --git a/Session/Home/HomeViewModel.swift b/Session/Home/HomeViewModel.swift index 9ce940a93..0a14098e9 100644 --- a/Session/Home/HomeViewModel.swift +++ b/Session/Home/HomeViewModel.swift @@ -7,7 +7,9 @@ import SignalUtilitiesKit import SessionMessagingKit import SessionUtilitiesKit -public class HomeViewModel { +public class HomeViewModel: NavigatableStateHolder { + public let navigatableState: NavigatableState = NavigatableState() + public typealias SectionModel = ArraySection // MARK: - Section diff --git a/Session/Utilities/UIContextualAction+Utilities.swift b/Session/Utilities/UIContextualAction+Utilities.swift index 778277a2c..0834d09a9 100644 --- a/Session/Utilities/UIContextualAction+Utilities.swift +++ b/Session/Utilities/UIContextualAction+Utilities.swift @@ -48,7 +48,8 @@ public extension UIContextualAction { indexPath: IndexPath, tableView: UITableView, threadViewModel: SessionThreadViewModel, - viewController: UIViewController? + viewController: UIViewController?, + navigatableStateHolder: NavigatableStateHolder? ) -> [UIContextualAction]? { guard !actions.isEmpty else { return nil } @@ -406,13 +407,36 @@ public extension UIContextualAction { dismissOnConfirm: true, onConfirm: { _ in Storage.shared.writeAsync { db in - try SessionThread.deleteOrLeave( - db, - threadId: threadViewModel.threadId, - threadVariant: threadViewModel.threadVariant, - groupLeaveType: .standard, - calledFromConfigHandling: false - ) + do { + try SessionThread.deleteOrLeave( + db, + threadId: threadViewModel.threadId, + threadVariant: threadViewModel.threadVariant, + groupLeaveType: .standard, + calledFromConfigHandling: false + ) + } catch { + DispatchQueue.main.async { + let toastBody: String = { + switch threadViewModel.threadVariant { + case .legacyGroup, .group: + return "groupLeaveErrorFailed" + .put(key: "groupname", value: threadViewModel.displayName) + .localized() + + default: + return "communityLeaveError" + .put(key: "communityname", value: threadViewModel.displayName) + .localized() + } + }() + navigatableStateHolder?.showToast( + text: toastBody, + backgroundColor: .backgroundSecondary + ) + } + } + } viewController?.dismiss(animated: true, completion: nil)