Add a custom action for “switch accounts”
This commit is contained in:
parent
fd31e08089
commit
f9daeea4d3
|
@ -676,9 +676,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"account_list": {
|
"account_list": {
|
||||||
"tab_bar_hint": "Current selected profile: %s. Double tap then hold to show account switcher",
|
|
||||||
"dismiss_account_switcher": "Dismiss Account Switcher",
|
"dismiss_account_switcher": "Dismiss Account Switcher",
|
||||||
"add_account": "Add Account"
|
"add_account": "Add Account",
|
||||||
|
"switch_accounts": "Switch Accounts"
|
||||||
},
|
},
|
||||||
"wizard": {
|
"wizard": {
|
||||||
"new_in_mastodon": "New in Mastodon",
|
"new_in_mastodon": "New in Mastodon",
|
||||||
|
@ -686,4 +686,4 @@
|
||||||
"accessibility_hint": "Double tap to dismiss this wizard"
|
"accessibility_hint": "Double tap to dismiss this wizard"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ final class ContentSplitViewController: UIViewController, NeedsDependency {
|
||||||
sidebarViewController.context = context
|
sidebarViewController.context = context
|
||||||
sidebarViewController.coordinator = coordinator
|
sidebarViewController.coordinator = coordinator
|
||||||
sidebarViewController.viewModel = SidebarViewModel(context: context, authContext: authContext)
|
sidebarViewController.viewModel = SidebarViewModel(context: context, authContext: authContext)
|
||||||
|
sidebarViewController.viewModel.delegate = sidebarViewController
|
||||||
sidebarViewController.delegate = self
|
sidebarViewController.delegate = self
|
||||||
return sidebarViewController
|
return sidebarViewController
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -298,6 +298,7 @@ extension MainTabBarController {
|
||||||
}
|
}
|
||||||
.store(in: &disposeBag)
|
.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 {
|
if let user = authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: context.managedObjectContext)?.user {
|
||||||
self.avatarURLObserver = user.publisher(for: \.avatar)
|
self.avatarURLObserver = user.publisher(for: \.avatar)
|
||||||
.sink { [weak self, weak user] _ in
|
.sink { [weak self, weak user] _ in
|
||||||
|
@ -306,15 +307,20 @@ extension MainTabBarController {
|
||||||
guard user.managedObjectContext != nil else { return }
|
guard user.managedObjectContext != nil else { return }
|
||||||
self.avatarURL = user.avatarImageURL()
|
self.avatarURL = user.avatarImageURL()
|
||||||
}
|
}
|
||||||
|
if let profileTabItem {
|
||||||
// a11y
|
profileTabItem.accessibilityCustomActions = [
|
||||||
let _profileTabItem = self.tabBar.items?.first { item in item.tag == Tab.me.tag }
|
// TODO: i18n (scene.account_list.switch_accounts)
|
||||||
guard let profileTabItem = _profileTabItem else { return }
|
UIAccessibilityCustomAction(name: "Switch Accounts") { [weak self] _ in
|
||||||
let currentUserDisplayName = user.displayNameWithFallback ?? "no user"
|
self?.showAccountSwitcher()
|
||||||
profileTabItem.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName)
|
return true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.avatarURLObserver = nil
|
self.avatarURLObserver = nil
|
||||||
|
if let profileTabItem {
|
||||||
|
profileTabItem.accessibilityCustomActions = []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let tabBarLongPressGestureRecognizer = UILongPressGestureRecognizer()
|
let tabBarLongPressGestureRecognizer = UILongPressGestureRecognizer()
|
||||||
|
@ -390,13 +396,17 @@ extension MainTabBarController {
|
||||||
|
|
||||||
switch tab {
|
switch tab {
|
||||||
case .me:
|
case .me:
|
||||||
guard let authContext = self.authContext else { return }
|
showAccountSwitcher()
|
||||||
let accountListViewModel = AccountListViewModel(context: context, authContext: authContext)
|
|
||||||
_ = coordinator.present(scene: .accountList(viewModel: accountListViewModel), from: self, transition: .panModal)
|
|
||||||
default:
|
default:
|
||||||
break
|
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 {
|
extension MainTabBarController {
|
||||||
|
|
|
@ -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 {
|
extension SidebarViewController {
|
||||||
@objc private func sidebarLongPressGestureRecognizerHandler(_ sender: UILongPressGestureRecognizer) {
|
@objc private func sidebarLongPressGestureRecognizerHandler(_ sender: UILongPressGestureRecognizer) {
|
||||||
guard sender.state == .began else { return }
|
guard sender.state == .began else { return }
|
||||||
|
|
|
@ -15,6 +15,10 @@ import MastodonAsset
|
||||||
import MastodonCore
|
import MastodonCore
|
||||||
import MastodonLocalization
|
import MastodonLocalization
|
||||||
|
|
||||||
|
protocol SidebarViewModelDelegate: AnyObject {
|
||||||
|
func sidebarViewModelDidSwitchAccounts(_ sidebarViewModel: SidebarViewModel)
|
||||||
|
}
|
||||||
|
|
||||||
final class SidebarViewModel {
|
final class SidebarViewModel {
|
||||||
|
|
||||||
var disposeBag = Set<AnyCancellable>()
|
var disposeBag = Set<AnyCancellable>()
|
||||||
|
@ -31,6 +35,8 @@ final class SidebarViewModel {
|
||||||
var secondaryDiffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
|
var secondaryDiffableDataSource: UICollectionViewDiffableDataSource<Section, Item>?
|
||||||
@Published private(set) var isReadyForWizardAvatarButton = false
|
@Published private(set) var isReadyForWizardAvatarButton = false
|
||||||
|
|
||||||
|
weak var delegate: SidebarViewModelDelegate?
|
||||||
|
|
||||||
init(context: AppContext, authContext: AuthContext?) {
|
init(context: AppContext, authContext: AuthContext?) {
|
||||||
self.context = context
|
self.context = context
|
||||||
self.authContext = authContext
|
self.authContext = authContext
|
||||||
|
@ -127,9 +133,18 @@ extension SidebarViewModel {
|
||||||
}
|
}
|
||||||
.store(in: &cell.disposeBag)
|
.store(in: &cell.disposeBag)
|
||||||
case .me:
|
case .me:
|
||||||
guard let user = self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user else { return }
|
if self.authContext?.mastodonAuthenticationBox.authenticationRecord.object(in: self.context.managedObjectContext)?.user != nil {
|
||||||
let currentUserDisplayName = user.displayNameWithFallback
|
cell.accessibilityCustomActions = [
|
||||||
cell.accessibilityHint = L10n.Scene.AccountList.tabBarHint(currentUserDisplayName)
|
// 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:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue