Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Extension functions for LiveData

Extension functions for LiveData

Yumemi.apk #1 ( https://yumemi.connpass.com/event/180842/ ) の資料

D8281434c0409ba2051cd3f7590e4c2f?s=128

Hiroyuki Kusu

August 21, 2020
Tweet

Transcript

  1. Extension functions for LiveData 2020.08.21 YUMEMI.apk #1 Hiroyuki Kusu (

    @hkusu_ )
  2. About me

  3. https://speakerdeck.com/hkusu/unit-test-for-viewmodel-and-livedata ࠓճ͸ ↑ ʹ࣌ؒతʹؚΊΔ͜ͱ͕ग़དྷͳ͔ͬͨ࿩୊

  4. ViewModel தͷ LiveData ͷهड़ private val _liveData1: MutableLiveData<Int> = MutableLiveData()

    val liveData1: LiveData<Int> = _liveData1 val liveData2: LiveData<Boolean> = liveData1.map { // ... } "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0" • σʔλͷؔ࿈ੑ͕એݴతʹఆٛ͞Ε͍ͯΔͱ෼͔Γ΍͍͢ • ΋ͬͱศརʹ͍ͨ͠..! • ࣗલͰ࡞੒֦ͨ͠ுؔ਺Λɺ͍͔ͭ͘঺հ͠·͢
  5. fun <T, U> LiveData<T>.coMap( coroutineScope: CoroutineScope, coroutineContext: CoroutineContext = coroutineScope.coroutineContext,

    block: suspend (T) -> U ): LiveData<U> { val result = MediatorLiveData<U>() result.addSource(this) { coroutineScope.launch(coroutineContext) { result.postValue(block.invoke(it)) } } return result } ※ switchMap() ͱ livedata {} Λ૊Έ߹Θͤͯ΋ಉ͜͡ͱ͕Ͱ͖·͢ ※ ݱঢ়Ͱ͸ inline fun ʹ͢ΔͱམͪΔ https://github.com/Kotlin/kotlinx.coroutines/issues/ coMap val liveData2: LiveData<List<SampleItem>> = liveData1.coMap(viewModelScope) { it: Int delay(3_000) sampleRepository.getItemList(it) // suspending function } ར༻Πϝʔδ ؔ਺ͷఆٛ
  6. val liveData3: LiveData<String> = merge(liveData1, liveData2) { i: Int?, b:

    Boolean? -> // ... } inline fun <T1, T2, U> merge( liveData1: LiveData<T1>, liveData2: LiveData<T2>, crossinline block: (T1?, T2?) -> U? ): LiveData<U> { val result = MediatorLiveData<U>() result.addSource(liveData1) { t1 -> block.invoke(t1, liveData2.value)?.takeUnless { it is Unit }?.let { result.value = it } } result.addSource(liveData2) { t2 -> block.invoke(liveData1.value, t2)?.takeUnless { it is Unit }?.let { result.value = it } } return result } merge ར༻Πϝʔδ ؔ਺ͷఆٛ (֦ுؔ਺Ͱͳ͘ී௨ͷؔ਺) block தͰ LiveDataͷߋ৽ΛΩϟϯηϧͰ͖ΔΑ͏ʹ͢Δҝͷॲஔ (Ωϟϯηϧ͍ͨ͠৔߹͸null΋͘͠͸໭Γ஋ͳ͠Ͱreturn͢Δ)
  7. val liveData2: LiveData<Int> = liveData1.filter { 100 < it }

    inline fun <T> LiveData<T>.filter( crossinline block: (T) -> Boolean ): LiveData<T> { val result = MediatorLiveData<T>() result.addSource(this) { if (block.invoke(it)) { result.value = it } } return result } filter ར༻Πϝʔδ ؔ਺ͷఆٛ
  8. val liveData2: LiveData<Int> = liveData1.map { // ... }.coMap(viewModelScope) {

    // ... }.map { // ... }.coMap(viewModelScope) { // ... }.filter { // ... } ͭͳ͛ͯॻ͚Δ
  9. ͋ΔLiveDataͷมߋΛܖػʹͳʹ͔͠ΒͷॲཧΛ͍͕ͨ͠.. ( LiveData ͷͭͳ͕Γ͚ͩͰ্ख͘ઃܭͰ͖ͳ͍έʔε ) val liveData2: LiveData<Boolean> = liveData1.map

    { } ແ༻ͳ LiveData2 Λఆٛͯ͠΋ɺLiveData2 ͷ Observer ͕ډͳ͍৔߹ ͜ͷϒϩοΫ͸࣮ߦ͞Εͳ͍
  10. val observe = liveData1.map { }.observe() // ... override fun

    onCleared() { observe.cancel() } class Observe<T>(private val liveData: LiveData<T>) { private val observer = Observer<T> {} fun start() { liveData.observeForever(observer) } fun cancel() { liveData.removeObserver(observer) } } fun <T> LiveData<T>.observe(): Observe<T> = Observe(this).apply { start() } observe ViewModel ͷഁغ࣌ʹ͸؍ଌΛऴྃͤ͞Δඞཁ observeForever Λར༻ ͜ͷϒϩοΫͰ೚ҙͷॲཧ ؔ਺ͷఆٛ ར༻Πϝʔδ
  11. https://gist.github.com/hkusu/b258c4e4ef488ccd8b213586417ffbec ࠓճɺ঺հ֦ͨ͠ுؔ਺͸gistʹஔ͖·ͨ͠

  12. Thank you ! @hkusu_