PagerTabStripNavigateable.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // PagerTabStripNavigateable.swift
  3. // Mastodon
  4. //
  5. // Created by MainasuK on 2022-6-2.
  6. //
  7. import UIKit
  8. import XLPagerTabStrip
  9. import MastodonLocalization
  10. typealias PagerTabStripNavigateable = PagerTabStripNavigateableCore & PagerTabStripNavigateableRelay
  11. protocol PagerTabStripNavigateableCore: AnyObject {
  12. var navigateablePageViewController: PagerTabStripViewController { get }
  13. var pagerTabStripNavigateKeyCommands: [UIKeyCommand] { get }
  14. func pagerTabStripNavigateKeyCommandHandler(_ sender: UIKeyCommand)
  15. func navigate(direction: PagerTabStripNavigationDirection)
  16. }
  17. @objc protocol PagerTabStripNavigateableRelay: AnyObject {
  18. func pagerTabStripNavigateKeyCommandHandlerRelay(_ sender: UIKeyCommand)
  19. }
  20. enum PagerTabStripNavigationDirection: String, CaseIterable {
  21. case previous
  22. case next
  23. var title: String {
  24. switch self {
  25. case .previous: return L10n.Common.Controls.Keyboard.SegmentedControl.previousSection
  26. case .next: return L10n.Common.Controls.Keyboard.SegmentedControl.nextSection
  27. }
  28. }
  29. // UIKeyCommand input
  30. var input: String {
  31. switch self {
  32. case .previous: return "["
  33. case .next: return "]"
  34. }
  35. }
  36. var modifierFlags: UIKeyModifierFlags {
  37. switch self {
  38. case .previous: return [.shift, .command]
  39. case .next: return [.shift, .command]
  40. }
  41. }
  42. var propertyList: Any {
  43. return rawValue
  44. }
  45. }
  46. extension PagerTabStripNavigateableCore where Self: PagerTabStripNavigateableRelay {
  47. var pagerTabStripNavigateKeyCommands: [UIKeyCommand] {
  48. PagerTabStripNavigationDirection.allCases.map { direction in
  49. UIKeyCommand(
  50. title: direction.title,
  51. image: nil,
  52. action: #selector(Self.pagerTabStripNavigateKeyCommandHandlerRelay(_:)),
  53. input: direction.input,
  54. modifierFlags: direction.modifierFlags,
  55. propertyList: direction.propertyList,
  56. alternates: [],
  57. discoverabilityTitle: nil,
  58. attributes: [],
  59. state: .off
  60. )
  61. }
  62. }
  63. func pagerTabStripNavigateKeyCommandHandler(_ sender: UIKeyCommand) {
  64. guard let rawValue = sender.propertyList as? String,
  65. let direction = PagerTabStripNavigationDirection(rawValue: rawValue) else { return }
  66. navigate(direction: direction)
  67. }
  68. }
  69. extension PagerTabStripNavigateableCore {
  70. func navigate(direction: PagerTabStripNavigationDirection) {
  71. let index = navigateablePageViewController.currentIndex
  72. let targetIndex: Int
  73. switch direction {
  74. case .previous:
  75. targetIndex = index - 1
  76. case .next:
  77. targetIndex = index + 1
  78. }
  79. guard targetIndex >= 0,
  80. !navigateablePageViewController.viewControllers.isEmpty,
  81. targetIndex < navigateablePageViewController.viewControllers.count,
  82. navigateablePageViewController.canMoveTo(index: targetIndex)
  83. else {
  84. return
  85. }
  86. navigateablePageViewController.moveToViewController(at: targetIndex)
  87. }
  88. }