Multiplatform LiveData
1.0.0
Kotlin-Multiplatform에서 Android Livedatas의 réimplémentation
그것은 안드로이드에 실제 살아있는 실제를 랩핑하고 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 " Livedatas의 Kotlin Remementation을 사용합니다
implementation " com.github.florent37:multiplatform-livedata-ios:1.0.0 " Jetpack의 Livedatas 내부에 사용됩니다
implementation " com.github.florent37:multiplatform-livedata-android:1.0.0 "내부에 저장된 실제 LIVEDATA를 검색 할 수 있습니다.
KLiveData<T>.toLivedata : LiveData<T>
KMutableLiveData<T>.toMutableLiveData : MutableLiveData<T>
KMediatorLiveData<T>.toMediatorLivedata : MediatorLivedata<T>
그리고 Jetpacks 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.