diff --git a/Data Sources/IdentitiesDataSource.swift b/Data Sources/IdentitiesDataSource.swift index 5828939..7520d68 100644 --- a/Data Sources/IdentitiesDataSource.swift +++ b/Data Sources/IdentitiesDataSource.swift @@ -18,13 +18,10 @@ final class IdentitiesDataSource: UITableViewDiffableDataSource() - private let deleteAction: (Identity) -> Void init(tableView: UITableView, publisher: AnyPublisher<[Identity], Never>, - viewModelProvider: @escaping (Identity) -> IdentityViewModel, - deleteAction: @escaping (Identity) -> Void) { - self.deleteAction = deleteAction + viewModelProvider: @escaping (Identity) -> IdentityViewModel) { tableView.register(UITableViewCell.self, forCellReuseIdentifier: String(describing: UITableViewCell.self)) @@ -79,16 +76,6 @@ final class IdentitiesDataSource: UITableViewDiffableDataSource Bool { itemIdentifier(for: indexPath) != .add } - - override func tableView(_ tableView: UITableView, - commit editingStyle: UITableViewCell.EditingStyle, - forRowAt indexPath: IndexPath) { - guard editingStyle == .delete, - case let .identitiy(identity) = itemIdentifier(for: indexPath) - else { return } - - deleteAction(identity) - } } private extension IdentitiesDataSource { diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index 4cb2610..95a12bd 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -147,6 +147,7 @@ "secondary-navigation-button.accessibility-title" = "Account Menu"; "identities.accounts" = "Accounts"; "identities.browsing" = "Browsing"; +"identities.log-out" = "Log out"; "identities.pending" = "Pending"; "lists.new-list-title" = "New List Title"; "load-more" = "Load More"; diff --git a/View Controllers/IdentitiesViewController.swift b/View Controllers/IdentitiesViewController.swift index 586695a..22d9e6a 100644 --- a/View Controllers/IdentitiesViewController.swift +++ b/View Controllers/IdentitiesViewController.swift @@ -10,8 +10,7 @@ final class IdentitiesViewController: UITableViewController { private lazy var dataSource: IdentitiesDataSource = { .init(tableView: tableView, publisher: viewModel.$identities.eraseToAnyPublisher(), - viewModelProvider: viewModel.viewModel(identity:), - deleteAction: { [weak self] in self?.rootViewModel.deleteIdentity(id: $0.id) }) + viewModelProvider: viewModel.viewModel(identity:)) }() init(viewModel: IdentitiesViewModel, rootViewModel: RootViewModel) { @@ -60,4 +59,26 @@ final class IdentitiesViewController: UITableViewController { rootViewModel.identitySelected(id: identityViewModel.id) } } + + override func tableView( + _ tableView: UITableView, + trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + guard dataSource.itemIdentifier(for: indexPath) != .add else { return nil } + + let logOutAction = UIContextualAction( + style: .destructive, + title: NSLocalizedString("identities.log-out", comment: "")) { [weak self] _, _, completionHandler in + guard let self = self, case let .identitiy(identity) = self.dataSource.itemIdentifier(for: indexPath) else { + completionHandler(false) + + return + } + + self.rootViewModel.deleteIdentity(id: identity.id) + + completionHandler(true) + } + + return UISwipeActionsConfiguration(actions: [logOutAction]) + } }