Add selection to menu (IOS-235)

also refactor it into a function as we need to re-generate it for an updated version
This commit is contained in:
Nathan Mattes 2024-03-28 13:31:19 +01:00
parent ceca4f6887
commit 4ae69c9036
1 changed files with 57 additions and 45 deletions

View File

@ -46,42 +46,8 @@ final class HomeTimelineViewController: UIViewController, NeedsDependency, Media
let titleView = HomeTimelineNavigationBarTitleView() let titleView = HomeTimelineNavigationBarTitleView()
lazy var timelineSelectorBarButtonItem: UIBarButtonItem = { lazy var timelineSelectorButton = {
let button = UIButton(type: .custom) let button = UIButton(type: .custom)
let actions = [
UIAction(title: "Following", image: .init(systemName: "house")) { [weak self] action in
guard let self, let viewModel = self.viewModel else { return }
viewModel.timelineContext = .following
viewModel.dataController.records = []
viewModel.loadLatestStateMachine.enter(HomeTimelineViewModel.LoadLatestState.LoadingManually.self)
button.setAttributedTitle(
.init(string: "Following", attributes: [
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
]),
for: .normal)
button.sizeToFit()
},
UIAction(title: "Local", image: .init(systemName: "building.2")) { [weak self] _ in
guard let self, let viewModel = self.viewModel else { return }
viewModel.timelineContext = .community
viewModel.dataController.records = []
viewModel.loadLatestStateMachine.enter(HomeTimelineViewModel.LoadLatestState.LoadingManually.self)
button.setAttributedTitle(
.init(string: "Local", attributes: [
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
]),
for: .normal)
button.sizeToFit()
}
]
let menu = UIMenu(children: actions)
button.setAttributedTitle( button.setAttributedTitle(
.init(string: "Following", attributes: [ .init(string: "Following", attributes: [
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold)) .font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
@ -105,10 +71,8 @@ final class HomeTimelineViewController: UIViewController, NeedsDependency, Media
.forceLeftToRight : .forceLeftToRight :
.forceRightToLeft .forceRightToLeft
button.showsMenuAsPrimaryAction = true button.showsMenuAsPrimaryAction = true
button.menu = menu button.menu = generateTimeSelectorMenu()
return button
let barButtonItem = UIBarButtonItem(customView: button)
return barButtonItem
}() }()
let settingBarButtonItem: UIBarButtonItem = { let settingBarButtonItem: UIBarButtonItem = {
@ -137,6 +101,54 @@ final class HomeTimelineViewController: UIViewController, NeedsDependency, Media
}() }()
let refreshControl = RefreshControl() let refreshControl = RefreshControl()
private func generateTimeSelectorMenu() -> UIMenu {
let showFollowingAction = UIAction(title: "Following", image: .init(systemName: "house")) { [weak self] _ in
guard let self, let viewModel = self.viewModel else { return }
viewModel.timelineContext = .following
viewModel.dataController.records = []
viewModel.loadLatestStateMachine.enter(HomeTimelineViewModel.LoadLatestState.LoadingManually.self)
timelineSelectorButton.setAttributedTitle(
.init(string: "Following", attributes: [
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
]),
for: .normal)
timelineSelectorButton.sizeToFit()
timelineSelectorButton.menu = generateTimeSelectorMenu()
}
let showLocalTimelineAction = UIAction(title: "Local", image: .init(systemName: "building.2")) { [weak self] action in
guard let self, let viewModel = self.viewModel else { return }
viewModel.timelineContext = .community
viewModel.dataController.records = []
viewModel.loadLatestStateMachine.enter(HomeTimelineViewModel.LoadLatestState.LoadingManually.self)
timelineSelectorButton.setAttributedTitle(
.init(string: "Local", attributes: [
.font: UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 20, weight: .semibold))
]),
for: .normal)
timelineSelectorButton.sizeToFit()
timelineSelectorButton.menu = generateTimeSelectorMenu()
}
if let viewModel {
switch viewModel.timelineContext {
case .community:
showLocalTimelineAction.state = .on
showFollowingAction.state = .off
case .following:
showLocalTimelineAction.state = .off
showFollowingAction.state = .on
}
}
return UIMenu(children: [showFollowingAction, showLocalTimelineAction])
}
} }
extension HomeTimelineViewController { extension HomeTimelineViewController {
@ -159,7 +171,7 @@ extension HomeTimelineViewController {
settingBarButtonItem.target = self settingBarButtonItem.target = self
settingBarButtonItem.action = #selector(HomeTimelineViewController.settingBarButtonItemPressed(_:)) settingBarButtonItem.action = #selector(HomeTimelineViewController.settingBarButtonItemPressed(_:))
self.navigationItem.leftBarButtonItem = self.timelineSelectorBarButtonItem self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: timelineSelectorButton)
// navigationItem.titleView = titleView // navigationItem.titleView = titleView
// titleView.delegate = self // titleView.delegate = self