Repository navigation
Expand file tree
/
Copy pathCarouselViewModel.swift
More file actions
58 lines (50 loc) · 2.39 KB
/
Copy pathCarouselViewModel.swift
File metadata and controls
58 lines (50 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Foundation
import JamitFoundation
import UIKit
/// The state view model for `CarouselView`.
public struct CarouselViewModel<Content: ViewModelProtocol>: ViewModelProtocol {
/// A closure type definition for the page index change callback.
public typealias PageIndexChangeCallback = (Int) -> Void
/// The content view models for the carousel content views.
public let pages: [Content]
/// The insets applied to the current active page frame.
public let pageInset: UIEdgeInsets
/// The insets applied to the neighboring page frames of the current active page.
public let neighboringPageInset: UIEdgeInsets
/// The scale applied to the neighboring page frames of the current active page.
public let neighboringPageScaleFactor: CGFloat
/// The callback for page index changes.
public let onPageIndexChanged: PageIndexChangeCallback
/// The default initializer of `CarouselViewModel`.
///
/// - Parameter pages: The carousel content view models.
/// - Parameter pageInset: The insets applied to the current active page frame.
/// - Parameter neighboringPageInset: The insets applied to the neighboring page frames of the current active page.
/// - Parameter neighboringPageScaleFactor: The scale applied to the neighboring page frames of the current active page.
/// - Parameter onPageIndexChanged: The content view models for the carousel content views.
public init(
pages: [Content] = Self.default.pages,
pageInset: UIEdgeInsets = Self.default.pageInset,
neighboringPageInset: UIEdgeInsets = Self.default.neighboringPageInset,
neighboringPageScaleFactor: CGFloat = Self.default.neighboringPageScaleFactor,
onPageIndexChanged: @escaping PageIndexChangeCallback = Self.default.onPageIndexChanged
) {
self.pages = pages
self.pageInset = pageInset
self.neighboringPageInset = neighboringPageInset
self.neighboringPageScaleFactor = neighboringPageScaleFactor
self.onPageIndexChanged = onPageIndexChanged
}
}
extension CarouselViewModel {
/// The default state of `CarouselViewModel`.
public static var `default`: CarouselViewModel<Content> {
return .init(
pages: [],
pageInset: .zero,
neighboringPageInset: .zero,
neighboringPageScaleFactor: 1.0,
onPageIndexChanged: { _ in }
)
}
}