Add a custom action for “switch accounts”

This commit is contained in:
Jed Fox 2022-11-07 13:32:15 -05:00
parent fd31e08089
commit f9daeea4d3
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
5 changed files with 55 additions and 16 deletions

View File

@ -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"
}
}
}
}

View File

@ -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
}()

View File

@ -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 {

View File

@ -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 }

View File

@ -15,6 +15,10 @@ import MastodonAsset
import MastodonCore
import MastodonLocalization
protocol SidebarViewModelDelegate: AnyObject {
func sidebarViewModelDidSwitchAccounts(_ sidebarViewModel: SidebarViewModel)
}
final class SidebarViewModel {
var disposeBag = Set<AnyCancellable>()
@ -31,6 +35,8 @@ final class SidebarViewModel {
var secondaryDiffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
@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
}