Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Extension functions for LiveData
Search
Hiroyuki Kusu
August 21, 2020
Programming
520
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Extension functions for LiveData
Yumemi.apk #1 (
https://yumemi.connpass.com/event/180842/
) の資料
Hiroyuki Kusu
August 21, 2020
More Decks by Hiroyuki Kusu
See All by Hiroyuki Kusu
モノレポのプルリクエストに最近、導入したもの
hkusu
2
600
GitHub composite actions
hkusu
2
450
Android の静的解析における SARIF ファイルの活用
hkusu
0
5.7k
CI_でライブラリのバージョンの変化をレポートする.pdf
hkusu
0
420
Maestro を GitHub Actions で動かす 〜Android編〜
hkusu
1
1.8k
Android の CI(GitHub Actions)の改善で、最近やったこと
hkusu
0
740
Tauri Mobile で生成される Android のコードを見てみる
hkusu
0
1.6k
Custom GitHub Actions を作って Organization 内で共有する
hkusu
1
600
GitHub Actions でユニットテストの結果をレポートする
hkusu
0
3.9k
Other Decks in Programming
See All in Programming
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
1
250
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
180
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
530
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
210
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.7k
VibeCodingからAgenticWorkflowへ
starfish719
0
310
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
440
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
370
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
【やさしく解説 設計編・中級 #4】ルールの寿命と、システムの年輪
panda728
PRO
2
180
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
180
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
580
Featured
See All Featured
Test your architecture with Archunit
thirion
1
2.3k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
3
410
AI: The stuff that nobody shows you
jnunemaker
PRO
9
890
Designing for Timeless Needs
cassininazir
1
430
WCS-LA-2024
lcolladotor
0
790
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
360
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
How to Think Like a Performance Engineer
csswizardry
28
2.7k
How to build a perfect <img>
jonoalderson
1
5.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Transcript
Extension functions for LiveData 2020.08.21 YUMEMI.apk #1 Hiroyuki Kusu (
@hkusu_ )
About me
https://speakerdeck.com/hkusu/unit-test-for-viewmodel-and-livedata ࠓճ ↑ ʹ࣌ؒతʹؚΊΔ͜ͱ͕ग़དྷͳ͔ͬͨ
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" • σʔλͷؔ࿈ੑ͕એݴతʹఆٛ͞Ε͍ͯΔͱ͔Γ͍͢ • ͬͱศརʹ͍ͨ͠..! • ࣗલͰ࡞֦ͨ͠ுؔΛɺ͍͔ͭ͘հ͠·͢
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 } ར༻Πϝʔδ ؔͷఆٛ
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͢Δ)
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 ར༻Πϝʔδ ؔͷఆٛ
val liveData2: LiveData<Int> = liveData1.map { // ... }.coMap(viewModelScope) {
// ... }.map { // ... }.coMap(viewModelScope) { // ... }.filter { // ... } ͭͳ͛ͯॻ͚Δ
͋ΔLiveDataͷมߋΛܖػʹͳʹ͔͠ΒͷॲཧΛ͍͕ͨ͠.. ( LiveData ͷͭͳ͕Γ͚ͩͰ্ख͘ઃܭͰ͖ͳ͍έʔε ) val liveData2: LiveData<Boolean> = liveData1.map
{ } ແ༻ͳ LiveData2 Λఆٛͯ͠ɺLiveData2 ͷ Observer ͕ډͳ͍߹ ͜ͷϒϩοΫ࣮ߦ͞Εͳ͍
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 Λར༻ ͜ͷϒϩοΫͰҙͷॲཧ ؔͷఆٛ ར༻Πϝʔδ
https://gist.github.com/hkusu/b258c4e4ef488ccd8b213586417ffbec ࠓճɺհ֦ͨ͠ுؔgistʹஔ͖·ͨ͠
Thank you ! @hkusu_