feat: add segment control shortcut for profile scene

This commit is contained in:
CMK 2021-05-21 17:42:14 +08:00
parent 611d65dd30
commit 443c863465
7 changed files with 132 additions and 2 deletions

View File

@ -88,7 +88,9 @@
"common": {
"switch_to_tab": "Switch to %s",
"show_favorites": "Show Favorites",
"open_settings": "Open Settings"
"open_settings": "Open Settings",
"previous_section": "Previous Section",
"next_section": "Next Section"
},
"timeline": {
"previous_status": "Previous Status",

View File

@ -202,6 +202,7 @@
DB1D843026566512000346B3 /* KeyboardPreference.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1D842F26566512000346B3 /* KeyboardPreference.swift */; };
DB1D843426579931000346B3 /* TableViewControllerNavigateable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1D843326579931000346B3 /* TableViewControllerNavigateable.swift */; };
DB1D843626579DB5000346B3 /* StatusProvider+TableViewControllerNavigateable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1D843526579DB5000346B3 /* StatusProvider+TableViewControllerNavigateable.swift */; };
DB1D84382657B275000346B3 /* SegmentedControlNavigateable.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1D84372657B275000346B3 /* SegmentedControlNavigateable.swift */; };
DB1E346825F518E20079D7DF /* CategoryPickerSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1E346725F518E20079D7DF /* CategoryPickerSection.swift */; };
DB1E347825F519300079D7DF /* PickServerItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1E347725F519300079D7DF /* PickServerItem.swift */; };
DB1FD43625F26899004CFCFC /* MastodonPickServerViewModel+LoadIndexedServerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB1FD43525F26899004CFCFC /* MastodonPickServerViewModel+LoadIndexedServerState.swift */; };
@ -762,6 +763,7 @@
DB1D842F26566512000346B3 /* KeyboardPreference.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardPreference.swift; sourceTree = "<group>"; };
DB1D843326579931000346B3 /* TableViewControllerNavigateable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewControllerNavigateable.swift; sourceTree = "<group>"; };
DB1D843526579DB5000346B3 /* StatusProvider+TableViewControllerNavigateable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "StatusProvider+TableViewControllerNavigateable.swift"; sourceTree = "<group>"; };
DB1D84372657B275000346B3 /* SegmentedControlNavigateable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentedControlNavigateable.swift; sourceTree = "<group>"; };
DB1E346725F518E20079D7DF /* CategoryPickerSection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CategoryPickerSection.swift; sourceTree = "<group>"; };
DB1E347725F519300079D7DF /* PickServerItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PickServerItem.swift; sourceTree = "<group>"; };
DB1FD43525F26899004CFCFC /* MastodonPickServerViewModel+LoadIndexedServerState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MastodonPickServerViewModel+LoadIndexedServerState.swift"; sourceTree = "<group>"; };
@ -1353,6 +1355,7 @@
2D38F20725CD491300561493 /* DisposeBagCollectable.swift */,
5DF1058425F88AE500D6C0D4 /* NeedsDependency+AVPlayerViewControllerDelegate.swift */,
DBE3CE12261D7D4200430CC6 /* StatusTableViewControllerAspect.swift */,
DB1D84372657B275000346B3 /* SegmentedControlNavigateable.swift */,
DB1D843326579931000346B3 /* TableViewControllerNavigateable.swift */,
DB1D842D26552C4D000346B3 /* StatusTableViewControllerNavigateable.swift */,
);
@ -3197,6 +3200,7 @@
5B90C460262599800002E742 /* SettingsAppearanceTableViewCell.swift in Sources */,
DB8AF54425C13647002E6C99 /* SceneCoordinator.swift in Sources */,
5DF1058525F88AE500D6C0D4 /* NeedsDependency+AVPlayerViewControllerDelegate.swift in Sources */,
DB1D84382657B275000346B3 /* SegmentedControlNavigateable.swift in Sources */,
DB447697260B439000B66B82 /* CustomEmojiPickerHeaderCollectionReusableView.swift in Sources */,
DB45FAF925CA80A2005A8AC7 /* APIService+CoreData+MastodonAuthentication.swift in Sources */,
2D04F42525C255B9003F936F /* APIService+PublicTimeline.swift in Sources */,

View File

@ -199,8 +199,12 @@ internal enum L10n {
}
internal enum Keyboard {
internal enum Common {
/// Next Section
internal static let nextSection = L10n.tr("Localizable", "Common.Controls.Keyboard.Common.NextSection")
/// Open Settings
internal static let openSettings = L10n.tr("Localizable", "Common.Controls.Keyboard.Common.OpenSettings")
/// Previous Section
internal static let previousSection = L10n.tr("Localizable", "Common.Controls.Keyboard.Common.PreviousSection")
/// Show Favorites
internal static let showFavorites = L10n.tr("Localizable", "Common.Controls.Keyboard.Common.ShowFavorites")
/// Switch to %@

View File

@ -0,0 +1,95 @@
//
// SegmentedControlNavigateable.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-5-21.
//
import UIKit
typealias SegmentedControlNavigateable = SegmentedControlNavigateableCore & SegmentedControlNavigateableRelay
protocol SegmentedControlNavigateableCore: AnyObject {
var navigateableSegmentedControl: UISegmentedControl { get }
var segmentedControlNavigateKeyCommands: [UIKeyCommand] { get }
func segmentedControlNavigateKeyCommandHandler(_ sender: UIKeyCommand)
func navigate(direction: SegmentedControlNavigationDirection)
}
@objc protocol SegmentedControlNavigateableRelay: AnyObject {
func segmentedControlNavigateKeyCommandHandlerRelay(_ sender: UIKeyCommand)
}
enum SegmentedControlNavigationDirection: String, CaseIterable {
case previous
case next
var title: String {
switch self {
case .previous: return L10n.Common.Controls.Keyboard.Common.previousSection
case .next: return L10n.Common.Controls.Keyboard.Common.nextSection
}
}
// UIKeyCommand input
var input: String {
switch self {
case .previous: return "["
case .next: return "]"
}
}
var modifierFlags: UIKeyModifierFlags {
switch self {
case .previous: return [.shift, .command]
case .next: return [.shift, .command]
}
}
var propertyList: Any {
return rawValue
}
}
extension SegmentedControlNavigateableCore where Self: SegmentedControlNavigateableRelay {
var segmentedControlNavigateKeyCommands: [UIKeyCommand] {
SegmentedControlNavigationDirection.allCases.map { direction in
UIKeyCommand(
title: direction.title,
image: nil,
action: #selector(Self.segmentedControlNavigateKeyCommandHandlerRelay(_:)),
input: direction.input,
modifierFlags: direction.modifierFlags,
propertyList: direction.propertyList,
alternates: [],
discoverabilityTitle: nil,
attributes: [],
state: .off
)
}
}
func segmentedControlNavigateKeyCommandHandler(_ sender: UIKeyCommand) {
guard let rawValue = sender.propertyList as? String,
let direction = SegmentedControlNavigationDirection(rawValue: rawValue) else { return }
navigate(direction: direction)
}
}
extension SegmentedControlNavigateableCore {
func navigate(direction: SegmentedControlNavigationDirection) {
let index: Int = {
let selectedIndex = navigateableSegmentedControl.selectedSegmentIndex
switch direction {
case .previous: return selectedIndex - 1
case .next: return selectedIndex + 1
}
}()
guard 0..<navigateableSegmentedControl.numberOfSegments ~= index else { return }
navigateableSegmentedControl.selectedSegmentIndex = index
navigateableSegmentedControl.sendActions(for: .valueChanged)
}
}

View File

@ -68,7 +68,9 @@ Please check your internet connection.";
"Common.Controls.Firendship.UnblockUser" = "Unblock %@";
"Common.Controls.Firendship.Unmute" = "Unmute";
"Common.Controls.Firendship.UnmuteUser" = "Unmute %@";
"Common.Controls.Keyboard.Common.NextSection" = "Next Section";
"Common.Controls.Keyboard.Common.OpenSettings" = "Open Settings";
"Common.Controls.Keyboard.Common.PreviousSection" = "Previous Section";
"Common.Controls.Keyboard.Common.ShowFavorites" = "Show Favorites";
"Common.Controls.Keyboard.Common.SwitchToTab" = "Switch to %@";
"Common.Controls.Keyboard.Timeline.NextStatus" = "Next Status";

View File

@ -68,7 +68,9 @@ Please check your internet connection.";
"Common.Controls.Firendship.UnblockUser" = "Unblock %@";
"Common.Controls.Firendship.Unmute" = "Unmute";
"Common.Controls.Firendship.UnmuteUser" = "Unmute %@";
"Common.Controls.Keyboard.Common.NextSection" = "Next Section";
"Common.Controls.Keyboard.Common.OpenSettings" = "Open Settings";
"Common.Controls.Keyboard.Common.PreviousSection" = "Previous Section";
"Common.Controls.Keyboard.Common.ShowFavorites" = "Show Favorites";
"Common.Controls.Keyboard.Common.SwitchToTab" = "Switch to %@";
"Common.Controls.Keyboard.Timeline.NextStatus" = "Next Status";

View File

@ -859,7 +859,6 @@ extension ProfileViewController: ProfileHeaderViewDelegate {
}
func profileHeaderView(_ profileHeaderView: ProfileHeaderView, profileStatusDashboardView: ProfileStatusDashboardView, postDashboardMeterViewDidPressed dashboardMeterView: ProfileStatusDashboardMeterView) {
}
func profileHeaderView(_ profileHeaderView: ProfileHeaderView, profileStatusDashboardView: ProfileStatusDashboardView, followingDashboardMeterViewDidPressed dwingDashboardMeterView: ProfileStatusDashboardMeterView) {
@ -876,3 +875,25 @@ extension ProfileViewController: ScrollViewContainer {
var scrollView: UIScrollView { return overlayScrollView }
}
extension ProfileViewController {
override var keyCommands: [UIKeyCommand]? {
if !viewModel.isEditing.value {
return segmentedControlNavigateKeyCommands
}
return nil
}
}
// MARK: - SegmentedControlNavigateable
extension ProfileViewController: SegmentedControlNavigateable {
var navigateableSegmentedControl: UISegmentedControl {
profileHeaderViewController.pageSegmentedControl
}
@objc func segmentedControlNavigateKeyCommandHandlerRelay(_ sender: UIKeyCommand) {
segmentedControlNavigateKeyCommandHandler(sender)
}
}