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.