kotlin互多平台的简易反应状态管理和视图模型。没有样板。与Android兼容。
重新生存 - kotlin提供了这些基础,用于构建多平台视图模型和较低级别的逻辑:
CoroutineDispatcher有关更多详细信息,请参见重新分类文档。
Android,JVM,iOS,TVOS,WatchOS,MacOsarm64,MacOSX64,Mingwx64,Linuxx64
将软件包添加到您的build.gradle的dependencies {} :
dependencies {
// Add the BOM using the desired ReactiveState version
api platform( " com.ensody.reactivestate:reactivestate-bom:VERSION " )
// Leave out the version number from now on:
implementation " com.ensody.reactivestate:reactivestate "
// Utils for unit tests that want to use coroutines
implementation " com.ensody.reactivestate:reactivestate-test "
// Note: kotlin-coroutines-test only supports the "jvm" target,
// so reactivestate-test has the same limitation
}另外,请确保您已经集成了Maven Central Repo,例如Root build.gradle :Gradle:
subprojects {
repositories {
// ...
mavenCentral()
// ...
}
}以下两个原则是为您快速了解反应性编程方面。文档中的“指南”部分介绍了如何与更高级的方面一起工作,例如乘法图形模型,生命周期处理等。
注意:虽然讨论是关于StateFlow的,但您也可以使用LiveData ,甚至可以为其他可观察值实施扩展。
想象一下,您有一个带有名字和姓氏的输入表格,并希望同时观察两个StateFlow值:
isFirstNameValid: StateFlow<Boolean>isLastNameValid: StateFlow<Boolean>这就是您通过使用autoRun函数进行操作的方式:
autoRun {
submitButton.isEnabled = get(isFirstNameValid) && get(isLastNameValid)
}使用get(isFirstNameValid)您检索isFirstNameValid.value ,同时告诉autoRun每当更改值时,请重新执行块。该代码类似于编写以下内容:
lifecycleScope.launchWhenStarted {
isFirstNameValid
.combine(isLastNameValid) { firstNameValid, lastNameValid ->
firstNameValid to lastNameValid
}
.conflate()
.collect { (firstNameValid, lastNameValid) ->
try {
submitButton.isEnabled = firstNameValid && lastNameValid
} catch (e : CancellationException ) {
throw e
} catch (e : Throwable ) {
onError(e)
}
}
}相同的原理可用于创建derived反应StateFlow :
val isFormValid : StateFlow < Boolean > = derived {
get(isFirstNameValid) && get(isLastNameValid)
}现在,您可以在代码的其余部分中使用autoRun { submitButton.isEnabled = get(isFormValid) } 。
更进一步的是, isFirstNameValid本身通常也是derived计算的结果。因此,您可以具有多层反应性derived StateFlow s。
反应性的UI框架(例如JetPack)会在StateFlow改变时自动重建UI。因此,在UI层中autoRun通常可以用Composable替换。
但是,在UI下方,您的数据仍然需要反应性。此处重新分类提供了基于其他StateFlow s自动重新计算StateFlow derived 。这种模式在实践中非常有用,并为诸如JetPack组成的框架提供了理想的基础,主要集中在UI方面。 Reactivestate的derived和autoRun为您的数据和业务逻辑提供了相同的反应性。
在JetPack中,您甚至具有与derived非常相似的derivedStateOf 。因此,您可以选择是否要基于官方的Coroutines库( StateFlow / derived )还是JetPack Compose( State / state / derivedStateOf )来构建业务逻辑。但是,Coroutines库的优势是它可用于更多平台,并且完全独立于任何UI框架。最后,大多数开源非UI库可能基于Coroutines,因此基于StateFlow的代码也可能更适合兼容/互操作性。
换句话说,这两种解决方案的组合在一起都会产生完全反应性的乘数代码库 - 改善了代码简单性并避免了许多错误。
此外,当前JetPack组成的构图不提供任何乘法图视图模型支持或任何大规模架构。因此,该库通过为ViewModels提供BaseReactiveState来解决。它还带有生命周期感知事件系统( eventNotifier )和加载状态处理(因此,您可以根据启动的Coroutines跟踪一个或多个不同的加载指标)。
该库基于Reactive_State用于颤动,并适用于Kotlin Multiplatform和Android模式。
Copyright 2024 Ensody GmbH, Waldemar Kornewald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.