-
Notifications
You must be signed in to change notification settings - Fork 31
/
ViewModelWithLocalState.swift
56 lines (48 loc) · 1.55 KB
/
ViewModelWithLocalState.swift
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
//
// ViewModelWithLocalState.swift
// Tempura
//
// Copyright © 2021 Bending Spoons.
// Distributed under the MIT License.
// See the LICENSE file for more information.
import Foundation
import Katana
/// A special case of `ViewModel` used to select part of the Katana app state and `ViewControllerWithLocalState`'s `LocalState`
/// that is of interest for the View.
/// ```swift
/// struct CounterState: State {
/// var counter: Int = 0
/// }
/// ```
/// ```swift
/// struct ScreenLocalState: LocalState {
/// var isCounting: Bool = false
/// }
/// ```
/// ```swift
/// struct CounterViewModel: ViewModelWithState {
/// var countDescription: String
///
/// init(state: CounterState?, localState: ScreenLocalState) {
/// if let state = state, localState.isCounting {
/// self.countDescription = "the counter is at \(state.counter)"
/// } else {
/// self.countDescription = "we are not counting yet"
/// }
/// }
/// }
/// ```
public protocol ViewModelWithLocalState: ViewModelWithState {
/// The type of the LocalState for this ViewModel
// associatedtype S: State
associatedtype SS where S == SS
associatedtype LS: LocalState
/// Instantiate a ViewModelWithLocalState given the Katana app state and the `LocalState`.
init?(state: SS?, localState: LS)
}
extension ViewModelWithLocalState {
/// Do not use this, use the `ViewModelWithLocalState.init(state:localState:)` instead.
public init?(state _: SS) {
fatalError("use `init(state: S, localState: LS)` instead")
}
}