|
| 1 | +// |
| 2 | +// Pager.swift |
| 3 | +// SwiftUIPager |
| 4 | +// |
| 5 | +// Created by Fernando Moya de Rivas on 19/01/2020. |
| 6 | +// Copyright © 2020 Fernando Moya de Rivas. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | +import Combine |
| 11 | + |
| 12 | +/// Encapsulates `Pager` state. |
| 13 | +/// |
| 14 | +/// Initialize with one of its convenience methods: |
| 15 | +/// - `firstPage()` |
| 16 | +/// - `withIndex(_:)` |
| 17 | +@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) |
| 18 | +public class Page: ObservableObject { |
| 19 | + |
| 20 | + // Needed for `iOS 13` or else it won't trigger an update |
| 21 | + public var objectWillChange = PassthroughSubject<Void, Never>() |
| 22 | + var _index: Int |
| 23 | + |
| 24 | + /// Current page index. |
| 25 | + /// - Note: Modifying its value won't trigger a `SwiftUI` update, use `update(_:)` method instead. |
| 26 | + public var index: Int { |
| 27 | + get { _index } |
| 28 | + set { |
| 29 | + guard isInfinite else { |
| 30 | + return _index = min(totalPages - 1, max(0, newValue)) |
| 31 | + } |
| 32 | + _index = (newValue + totalPages) % totalPages |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Total number of pages |
| 37 | + var totalPages: Int = Int.max |
| 38 | + |
| 39 | + #if !os(tvOS) |
| 40 | + |
| 41 | + /// `swipeGesture` translation on the X-Axis |
| 42 | + var draggingOffset: CGFloat = 0 |
| 43 | + |
| 44 | + /// `swipeGesture` last translation on the X-Axis |
| 45 | + var lastDraggingValue: DragGesture.Value? |
| 46 | + |
| 47 | + /// `swipeGesture` velocity on the X-Axis |
| 48 | + var draggingVelocity: Double = 0 |
| 49 | + |
| 50 | + #endif |
| 51 | + |
| 52 | + /// Increment resulting from the last swipe |
| 53 | + var pageIncrement = 0 |
| 54 | + |
| 55 | + var isInfinite = false |
| 56 | + |
| 57 | + /// Initializes a new instance |
| 58 | + /// |
| 59 | + /// - Parameter page: Current page index |
| 60 | + private init(page: Int) { |
| 61 | + self._index = page |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +// MARK: Public |
| 67 | + |
| 68 | +@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) |
| 69 | +extension Page { |
| 70 | + |
| 71 | + /// An update to perform on a `Page` index |
| 72 | + public enum Update { |
| 73 | + |
| 74 | + /// Will increase the `index` by `1` |
| 75 | + case next |
| 76 | + |
| 77 | + /// Will decrease the `index` by `1` |
| 78 | + case previous |
| 79 | + |
| 80 | + /// Will move to the first page |
| 81 | + case moveToFirst |
| 82 | + |
| 83 | + /// Will move to the last page |
| 84 | + case moveToLast |
| 85 | + |
| 86 | + /// Will set the `index` to the new value |
| 87 | + case new(index: Int) |
| 88 | + } |
| 89 | + |
| 90 | + /// Convenience method to initialize a new `Page` |
| 91 | + /// |
| 92 | + /// - Parameter index: Current page index |
| 93 | + public static func withIndex(_ index: Int) -> Page { |
| 94 | + Page(page: index) |
| 95 | + } |
| 96 | + |
| 97 | + /// Convenience method to initialize a new `Page` |
| 98 | + /// |
| 99 | + /// - Parameter index: Current page index |
| 100 | + public static func first() -> Page { |
| 101 | + withIndex(0) |
| 102 | + } |
| 103 | + |
| 104 | + /// Will update `Page` accordingly and trigger `objectWillChange` |
| 105 | + /// |
| 106 | + /// - Parameter update: update to perform |
| 107 | + /// |
| 108 | + /// If you do not wish to trigger an update because you want to take control of the update, set `index` direclty |
| 109 | + public func update(_ update: Update) { |
| 110 | + switch update { |
| 111 | + case .next: |
| 112 | + index += 1 |
| 113 | + case .previous: |
| 114 | + index -= 1 |
| 115 | + case .moveToFirst: |
| 116 | + index = 0 |
| 117 | + case .moveToLast: |
| 118 | + index = totalPages - 1 |
| 119 | + case .new(let newIndex): |
| 120 | + index = newIndex |
| 121 | + } |
| 122 | + |
| 123 | + objectWillChange.send() |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments