-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSubscriptionsViewModel.swift
More file actions
65 lines (58 loc) · 1.94 KB
/
Copy pathSubscriptionsViewModel.swift
File metadata and controls
65 lines (58 loc) · 1.94 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
59
60
61
62
63
64
65
//
// Flare
// Copyright © 2024 Space Code. All rights reserved.
//
import Flare
import Foundation
// MARK: - SubscriptionsViewModel
/// A view model for managing subscriptions.
@available(watchOS, unavailable)
struct SubscriptionsViewModel: IModel {
/// The state of the view model.
enum State: Equatable {
/// Loading state.
case loading
/// Loaded products state.
case products([SubscriptionView.ViewModel])
/// Error state.
case error(IAPError)
}
/// The current state of the view model.
let state: State
/// The selected product ID.
let selectedProductID: String?
/// The presenter for the subscriptions.
let presenter: ISubscriptionsPresenter
/// The number of products in the loaded state.
var numberOfProducts: Int {
if case let .products(array) = state {
return array.count
}
return 0
}
}
@available(watchOS, unavailable)
extension SubscriptionsViewModel {
/// Sets the state of the view model and returns a new instance with the updated state.
///
/// - Parameter state: The new state of the view model.
/// - Returns: A new `SubscriptionsViewModel` instance with the updated state.
func setState(_ state: State) -> SubscriptionsViewModel {
SubscriptionsViewModel(
state: state,
selectedProductID: selectedProductID,
presenter: presenter
)
}
/// Sets the selected product ID and returns a new instance with the updated selected product ID.
///
/// - Parameter selectedProductID: The selected product ID.
/// - Returns: A new `SubscriptionsViewModel` instance with the updated selected product ID.
func setSelectedProductID(_ selectedProductID: String?) -> SubscriptionsViewModel {
SubscriptionsViewModel(
state: state,
selectedProductID: selectedProductID,
presenter: presenter
)
}
}