123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // PagerTabStripNavigateable.swift
- // Mastodon
- //
- // Created by MainasuK on 2022-6-2.
- //
- import UIKit
- import XLPagerTabStrip
- import MastodonLocalization
- typealias PagerTabStripNavigateable = PagerTabStripNavigateableCore & PagerTabStripNavigateableRelay
- protocol PagerTabStripNavigateableCore: AnyObject {
- var navigateablePageViewController: PagerTabStripViewController { get }
- var pagerTabStripNavigateKeyCommands: [UIKeyCommand] { get }
- func pagerTabStripNavigateKeyCommandHandler(_ sender: UIKeyCommand)
- func navigate(direction: PagerTabStripNavigationDirection)
- }
- @objc protocol PagerTabStripNavigateableRelay: AnyObject {
- func pagerTabStripNavigateKeyCommandHandlerRelay(_ sender: UIKeyCommand)
- }
- enum PagerTabStripNavigationDirection: String, CaseIterable {
- case previous
- case next
- var title: String {
- switch self {
- case .previous: return L10n.Common.Controls.Keyboard.SegmentedControl.previousSection
- case .next: return L10n.Common.Controls.Keyboard.SegmentedControl.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 PagerTabStripNavigateableCore where Self: PagerTabStripNavigateableRelay {
- var pagerTabStripNavigateKeyCommands: [UIKeyCommand] {
- PagerTabStripNavigationDirection.allCases.map { direction in
- UIKeyCommand(
- title: direction.title,
- image: nil,
- action: #selector(Self.pagerTabStripNavigateKeyCommandHandlerRelay(_:)),
- input: direction.input,
- modifierFlags: direction.modifierFlags,
- propertyList: direction.propertyList,
- alternates: [],
- discoverabilityTitle: nil,
- attributes: [],
- state: .off
- )
- }
- }
-
- func pagerTabStripNavigateKeyCommandHandler(_ sender: UIKeyCommand) {
- guard let rawValue = sender.propertyList as? String,
- let direction = PagerTabStripNavigationDirection(rawValue: rawValue) else { return }
- navigate(direction: direction)
- }
- }
- extension PagerTabStripNavigateableCore {
- func navigate(direction: PagerTabStripNavigationDirection) {
- let index = navigateablePageViewController.currentIndex
- let targetIndex: Int
-
- switch direction {
- case .previous:
- targetIndex = index - 1
- case .next:
- targetIndex = index + 1
- }
-
- guard targetIndex >= 0,
- !navigateablePageViewController.viewControllers.isEmpty,
- targetIndex < navigateablePageViewController.viewControllers.count,
- navigateablePageViewController.canMoveTo(index: targetIndex)
- else {
- return
- }
-
- navigateablePageViewController.moveToViewController(at: targetIndex)
- }
- }
|