CocoaPods
168 |
169 |
170 | If you like you can also use CocoaPods instead of SPM:
171 | ```ruby
172 | pod 'KMPObservableViewModelSwiftUI', '1.0.0-BETA-14'
173 | ```
174 |
175 |
176 |
177 | Create a `KMPObservableViewModel.swift` file with the following contents:
178 | ```swift
179 | import KMPObservableViewModelCore
180 | import shared // This should be your shared KMP module
181 |
182 | extension Kmp_observableviewmodel_coreViewModel: ViewModel { }
183 | ```
184 |
185 | After that you can use your view model almost as if it were an `ObservableObject`.
186 | Just use the view model specific property wrappers and functions:
187 |
188 | | `ObservableObject` | `ViewModel` |
189 | |-------------------------|----------------------------|
190 | | `@StateObject` | `@StateViewModel` |
191 | | `@ObservedObject` | `@ObservedViewModel` |
192 | | `@EnvironmentObject` | `@EnvironmentViewModel` |
193 | | `environmentObject(_:)` | `environmentViewModel(_:)` |
194 |
195 | E.g. to use the `TimeTravelViewModel` as a `StateObject`:
196 | ```swift
197 | import SwiftUI
198 | import KMPObservableViewModelSwiftUI
199 | import shared // This should be your shared KMP module
200 |
201 | struct ContentView: View {
202 | @StateViewModel var viewModel = TimeTravelViewModel()
203 | }
204 | ```
205 |
206 | It's also possible to subclass your view model in Swift:
207 | ```swift
208 | import Combine
209 | import shared // This should be your shared KMP module
210 |
211 | class TimeTravelViewModel: shared.TimeTravelViewModel {
212 | @Published var isResetDisabled: Bool = false
213 | }
214 | ```
215 |
216 | ### Child view models
217 |
218 | You'll need some additional logic if your `ViewModel`s expose child view models.
219 |
220 | First make sure to use the `NativeCoroutinesRefinedState` annotation instead of the `NativeCoroutinesState` annotation:
221 | ```kotlin
222 | class MyParentViewModel: ViewModel() {
223 | @NativeCoroutinesRefinedState
224 | val myChildViewModel: StateFlow