From f9daeea4d3ad66a5387493b67682c4e27396d2bf Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Mon, 7 Nov 2022 13:32:15 -0500 Subject: [PATCH] =?UTF-8?q?Add=20a=20custom=20action=20for=20=E2=80=9Cswit?= =?UTF-8?q?ch=20accounts=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Localization/app.json | 6 ++-- .../Root/ContentSplitViewController.swift | 1 + .../Root/MainTab/MainTabBarController.swift | 30 ++++++++++++------- .../Root/Sidebar/SidebarViewController.swift | 13 ++++++++ .../Scene/Root/Sidebar/SidebarViewModel.swift | 21 +++++++++++-- 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Localization/app.json b/Localization/app.json index a965b23ae..47d0e63a5 100644 --- a/Localization/app.json +++ b/Localization/app.json @@ -676,9 +676,9 @@ } }, "account_list": { - "tab_bar_hint": "Current selected profile: %s. Double tap then hold to show account switcher", "dismiss_account_switcher": "Dismiss Account Switcher", - "add_account": "Add Account" + "add_account": "Add Account", + "switch_accounts": "Switch Accounts" }, "wizard": { "new_in_mastodon": "New in Mastodon", @@ -686,4 +686,4 @@ "accessibility_hint": "Double tap to dismiss this wizard" } } -} \ No newline at end of file +} diff --git a/Mastodon/Scene/Root/ContentSplitViewController.swift b/Mastodon/Scene/Root/ContentSplitViewController.swift index 3f4758e8e..4ae7204ca 100644 --- a/Mastodon/Scene/Root/ContentSplitViewController.swift +++ b/Mastodon/Scene/Root/ContentSplitViewController.swift @@ -33,6 +33,7 @@ final class ContentSplitViewController: UIViewController, NeedsDependency { sidebarViewController.context = context sidebarViewController.coordinator = coordinator sidebarViewController.viewModel = SidebarViewModel(context: context, authContext: authContext) + sidebarViewController.viewModel.delegate = sidebarViewController sidebarViewController.delegate = self return sidebarViewController }() diff --git a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift index c49dcc1a1..d56a18aed 100644 --- a/Mastodon/Scene/Root/MainTab/MainTabBarController.swift +++ b/Mastodon/Scene/Root/MainTab/MainTabBarController.swift @@ -298,6 +298,7 @@ extension MainTabBarController { } .store(in: &disposeBag) + let profileTabItem = self.tabBar.items?.first { item in item.tag == Tab.me.tag } if let user = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.user { self.avatarURLObserver = user.publisher(for: \.avatar) .sink { [weak self, weak user] _ in @@ -306,15 +307,20 @@ extension MainTabBarController { guard user.managedObjectContext != nil else { return } self.avatarURL = user.avatarImageURL() } - - // a11y - let _profileTabItem = self.tabBar.items?.first { item in item.tag == Tab.me.tag } - guard let profileTabItem = _profileTabItem else { return } - let currentUserDisplayName = user.displayNameWithFallback ?? "no user" - profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName) - + if let profileTabItem { + profileTabItem.accessibilityCustomActions = [ + // TODO: i18n (scene.account_list.switch_accounts) + UIAccessibilityCustomAction(name: "Switch Accounts") { [weak self] _ in + self?.showAccountSwitcher() + return true + } + ] + } } else { self.avatarURLObserver = nil + if let profileTabItem { + profileTabItem.accessibilityCustomActions = [] + } } let tabBarLongPressGestureRecognizer = UILongPressGestureRecognizer() @@ -390,13 +396,17 @@ extension MainTabBarController { switch tab { case .me: - guard let authContext = self.authContext else { return } - let accountListViewModel = AccountListViewModel(context: context, authContext: authContext) - _ = coordinator.present(scene: .accountList(viewModel: accountListViewModel), from: self, transition: .panModal) + showAccountSwitcher() default: break } } + + private func showAccountSwitcher() { + guard let authContext = self.authContext else { return } + let accountListViewModel = AccountListViewModel(context: context, authContext: authContext) + _ = coordinator.present(scene: .accountList(viewModel: accountListViewModel), from: self, transition: .panModal) + } } extension MainTabBarController { diff --git a/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift b/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift index 70e1239b6..28e205047 100644 --- a/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift +++ b/Mastodon/Scene/Root/Sidebar/SidebarViewController.swift @@ -162,6 +162,19 @@ extension SidebarViewController { } +extension SidebarViewController: SidebarViewModelDelegate { + func sidebarViewModelDidSwitchAccounts(_ sidebarViewModel: SidebarViewModel) { + guard let diffableDataSource = viewModel.diffableDataSource else { return } + guard let indexPath = diffableDataSource.indexPath(for: .tab(.me)) else { return } + guard let cell = collectionView.cellForItem(at: indexPath) else { return } + delegate?.sidebarViewController( + self, + didLongPressItem: .tab(.me), + sourceView: cell + ) + } +} + extension SidebarViewController { @objc private func sidebarLongPressGestureRecognizerHandler(_ sender: UILongPressGestureRecognizer) { guard sender.state == .began else { return } diff --git a/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift b/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift index 9f0eb1899..22be9e6e0 100644 --- a/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift +++ b/Mastodon/Scene/Root/Sidebar/SidebarViewModel.swift @@ -15,6 +15,10 @@ import MastodonAsset import MastodonCore import MastodonLocalization +protocol SidebarViewModelDelegate: AnyObject { + func sidebarViewModelDidSwitchAccounts(_ sidebarViewModel: SidebarViewModel) +} + final class SidebarViewModel { var disposeBag = Set() @@ -31,6 +35,8 @@ final class SidebarViewModel { var secondaryDiffableDataSource: UICollectionViewDiffableDataSource? @Published private(set) var isReadyForWizardAvatarButton = false + weak var delegate: SidebarViewModelDelegate? + init(context: AppContext, authContext: AuthContext?) { self.context = context self.authContext = authContext @@ -127,9 +133,18 @@ extension SidebarViewModel { } .store(in: &cell.disposeBag) case .me: - guard let user = self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user else { return } - let currentUserDisplayName = user.displayNameWithFallback - cell.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName) + if self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user != nil { + cell.accessibilityCustomActions = [ + // TODO: i18n (scene.account_list.switch_accounts) + UIAccessibilityCustomAction(name: "Switch Accounts") { [weak self] _ in + guard let self, let delegate = self.delegate else { return false } + delegate.sidebarViewModelDidSwitchAccounts(self) + return true + } + ] + } else { + cell.accessibilityCustomActions = [] + } default: break }