KMM-ViewModel
一个允许您在 Android 和 iOS 之间共享 ViewModels 的库。
兼容性
库的最新版本使用 Kotlin 版本 1.9.10
。
还提供对旧版和/或预览版 Kotlin 版本的兼容性版本。
版本 | 版本后缀 | Kotlin | 协程 | AndroidX 生命周期 |
---|---|---|---|---|
最新版本 | 无后缀 | 1.9.10 | 1.7.3 | 2.6.1 |
1.0.0-ALPHA-13 | 无后缀 | 1.9.0 | 1.7.3 | 2.6.1 |
1.0.0-ALPHA-10 | 无后缀 | 1.8.22 | 1.7.2 | 2.6.1 |
1.0.0-ALPHA-9 | 无后缀 | 1.8.21 | 1.7.1 | 2.5.1 |
1.0.0-ALPHA-8 | 无后缀 | 1.8.21 | 1.7.0 | 2.5.1 |
1.0.0-ALPHA-7 | 无后缀 | 1.8.21 | 1.6.4 | 2.5.1 |
1.0.0-ALPHA-6 | 无后缀 | 1.8.20 | 1.6.4 | 2.5.1 |
1.0.0-ALPHA-4 | 无后缀 | 1.8.10 | 1.6.4 | 2.5.1 |
1.0.0-ALPHA-3 | 无后缀 | 1.8.0 | 1.6.4 | 2.5.1 |
Kotlin
将库添加到您的共享 Kotlin 模块中
dependencies {
api("com.rickclephas.kmm:kmm-viewmodel-core:1.0.0-ALPHA-14")
}
并创建您的 ViewModels
import com.rickclephas.kmm.viewmodel.KMMViewModel
import com.rickclephas.kmm.viewmodel.MutableStateFlow
import com.rickclephas.kmm.viewmodel.stateIn
open class TimeTravelViewModel: KMMViewModel() {
private val clockTime = Clock.time
/**
* A [StateFlow] that emits the actual time.
*/
val actualTime = clockTime.map { formatTime(it) }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), "N/A")
private val _travelEffect = MutableStateFlow<TravelEffect?>(viewModelScope, null)
/**
* A [StateFlow] that emits the applied [TravelEffect].
*/
val travelEffect = _travelEffect.asStateFlow()
}
如您可能已注意到,这与 Android ViewModel 没有太大区别。
最明显的区别是 KMMViewModel
的父类
- import androidx.lifecycle.ViewModel
+ import com.rickclephas.kmm.viewmodel.KMMViewModel
- open class TimeTravelViewModel: ViewModel() {
+ open class TimeTravelViewModel: KMMViewModel() {
除此之外,只有两个小的区别。
第一个是针对 stateIn
的不同导入方式
- import kotlinx.coroutines.flow.stateIn
+ import com.rickclephas.kmm.viewmodel.stateIn
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), "N/A")
第二个是不同的 MutableStateFlow
构造函数。
- import kotlinx.coroutines.flow.MutableStateFlow
+ import com.rickclephas.kmm.viewmodel.MutableStateFlow
- private val _travelEffect = MutableStateFlow<TravelEffect?>(null)
+ private val _travelEffect = MutableStateFlow<TravelEffect?>(viewModelScope, null)
这些细微的差别可以确保任何状态更改都会传播到iOS。
注意:
viewModelScope
是围绕实际的CoroutineScope
的一个包装器,可以通过ViewModelScope.coroutineScope
属性访问。
KMP-NativeCoroutines
强烈推荐您使用来自 KMP-NativeCoroutines 的 @NativeCoroutinesState
注解将您的 StateFlow
转换为Swift中的属性。
@NativeCoroutinesState
val travelEffect = _travelEffect.asStateFlow()
有关更多信息以及安装说明,请查看 KMP-NativeCoroutines 的 README。
替代方案
或者,您可以自己创建iOS源集中的扩展属性。
val TimeTravelViewModel.travelEffectValue: TravelEffect?
get() = travelEffect.value
Android
像使用任何其他Android视图模型一样使用视图模型。
class TimeTravelFragment: Fragment(R.layout.fragment_time_travel) {
private val viewModel: TimeTravelViewModel by viewModels()
}
注意:即将推出对 Jetpack Compose 的改进支持。
Swift
将Swift包添加到您的 Package.swift
文件中。
dependencies: [
.package(url: "https://github.com/rickclephas/KMM-ViewModel.git", from: "1.0.0-ALPHA-14")
]
或者,在Xcode中通过转到 文件
> 添加包...
并提供URL: https://github.com/rickclephas/KMM-ViewModel.git
来添加它。
CocoaPods
如果您愿意,也可以使用CocoaPods而不是SPM。
pod 'KMMViewModelSwiftUI', '1.0.0-ALPHA-14'
创建一个包含以下内容的 KMMViewModel.swift
文件
import KMMViewModelCore
import shared // This should be your shared KMM module
extension Kmm_viewmodel_coreKMMViewModel: KMMViewModel { }
之后,您几乎可以像使用 ObservableObject
一样使用您的视图模型。
只需使用视图模型特定的属性包装器和函数
ObservableObject |
KMMViewModel |
---|---|
@StateObject |
@StateViewModel |
@ObservedObject |
@ObservedViewModel |
@EnvironmentObject |
@EnvironmentViewModel |
environmentObject(_:) |
environmentViewModel(_:) |
例如,要将 TimeTravelViewModel
作为 StateObject
使用
import SwiftUI
import KMMViewModelSwiftUI
import shared // This should be your shared KMM module
struct ContentView: View {
@StateViewModel var viewModel = TimeTravelViewModel()
}
也可以在Swift中继承您的视图模型
import Combine
import shared // This should be your shared KMM module
class TimeTravelViewModel: shared.TimeTravelViewModel {
@Published var isResetDisabled: Bool = false
}
子视图模型
如果你的 KMMViewModel
公开子视图模型,您将需要一些额外的逻辑。
首先,请确保使用NativeCoroutinesRefinedState
注解而不是NativeCoroutinesState
注解。
class MyParentViewModel: KMMViewModel() {
@NativeCoroutinesRefinedState
val myChildViewModel: StateFlow<MyChildViewModel?> = MutableStateFlow(null)
}
之后,您应使用childViewModel(_, at:)
函数创建一个Swift扩展属性。
extension MyParentViewModel {
var myChildViewModel: MyChildViewModel? {
childViewModel(__myChildViewModel, at: \.__myChildViewModel)
}
}
这将防止您的Swift视图模型过早卸载。
注意:对于包含视图模型的列表、集合和字典,有
childViewModels(_, at:)
。