-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathViewModelState.swift
90 lines (77 loc) · 2.41 KB
/
ViewModelState.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// CFlowExt.swift
// mokoMvvmFlowSwiftUI (iOS)
//
// Created by Aleksey Mikhailov on 29.04.2022.
//
import MultiPlatformLibrary
import SwiftUI
import Combine
public extension ObservableObject where Self: ViewModel {
func state<T, R>(
_ flowKey: KeyPath<Self, CStateFlow<T>>,
equals: @escaping (T?, T?) -> Bool,
mapper: @escaping (T) -> R
) -> R {
let stateFlow: CStateFlow<T> = self[keyPath: flowKey]
var lastValue: T? = stateFlow.value
var disposable: DisposableHandle? = nil
disposable = stateFlow.subscribe(onCollect: { value in
if !equals(lastValue, value) {
lastValue = value
self.objectWillChange.send()
disposable?.dispose()
}
})
return mapper(stateFlow.value!)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<KotlinBoolean>>) -> Bool {
return state(
flowKey,
equals: { $0?.boolValue == $1?.boolValue },
mapper: { $0.boolValue }
)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<KotlinDouble>>) -> Double {
return state(
flowKey,
equals: { $0?.doubleValue == $1?.doubleValue },
mapper: { $0.doubleValue }
)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<KotlinFloat>>) -> Float {
return state(
flowKey,
equals: { $0?.floatValue == $1?.floatValue },
mapper: { $0.floatValue }
)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<KotlinInt>>) -> Int {
return state(
flowKey,
equals: { $0?.intValue == $1?.intValue },
mapper: { $0.intValue }
)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<KotlinLong>>) -> Int64 {
return state(
flowKey,
equals: { $0?.int64Value == $1?.int64Value },
mapper: { $0.int64Value }
)
}
func state(_ flowKey: KeyPath<Self, CStateFlow<NSString>>) -> String {
return state(
flowKey,
equals: { $0 == $1 },
mapper: { $0 as String }
)
}
func state<T>(_ flowKey: KeyPath<Self, CStateFlow<NSArray>>) -> Array<T> {
return state(
flowKey,
equals: { $0 === $1 },
mapper: { $0 as! Array<T> }
)
}
}