Multiplatform LiveData
1.0.0
kotlin-multiplatform上的Android Livedatas的重新召集
它包裹在Android上的Livedatas,並在iOS上使用可觀察的模式
它的工作原理與Android Livedatas完全相同:https://developer.android.com/topic/libraries/architecture/livedata
KLiveData<T>僅讀取可觀察的KMutableLiveData<T>讀 /寫觀察的KMediatorLiveData<T>讀 /寫可觀察的,能夠聽KLiveData
也可以使用map和switchmap之類的轉換
class MainViewModel ( val premiumManager : PremiumManager ) {
private val _viewState = KMediatorLiveData < ViewState >()
val viewState = KLiveData < ViewState >()
get() = _viewState
init {
_viewState .value = ViewState ( " not premium " )
_viewState .addSource(premiumManager.premium()) {
if (it) {
_viewState .value = ViewState ( " premium " )
} else {
_viewState .value = ViewState ( " not premium " )
}
}
}
} class ViewState (
val userStatus : String
) class PremiumManager {
private val premium = KMutableLiveData < Boolean >()
fun premium () : KLiveData < Boolean > {
return premium
}
fun becomePremium () {
premium.value = true
}
init {
// default value
premium.value = false
}
}添加存儲庫
repositories {
maven { url " https://dl.bintray.com/florent37/maven " }
}implementation " com.github.florent37:multiplatform-livedata:1.0.0 " 使用Kotlin重新實現Livedatas
implementation " com.github.florent37:multiplatform-livedata-ios:1.0.0 " 使用噴氣背包的Livedatas
implementation " com.github.florent37:multiplatform-livedata-android:1.0.0 "您可以檢索內部存儲的真實壽命:
KLiveData<T>.toLivedata : LiveData<T>
KMutableLiveData<T>.toMutableLiveData : MutableLiveData<T>
KMediatorLiveData<T>.toMediatorLivedata : MediatorLivedata<T>
並從噴氣背包livedatas創建klivedatas
LiveData<T>.toKLivedata: KLiveData<T>
MutableLiveData<T>.toKMutableLiveData: KMutableLiveData<T>
MediatorLiveData<T>.toKMediatorLivedata: KMediatorLiveData<T>
Copyright 2018 Florent37
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.