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
530
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
460
Android の静的解析における SARIF ファイルの活用
hkusu
0
5.7k
CI_でライブラリのバージョンの変化をレポートする.pdf
hkusu
0
430
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
610
GitHub Actions でユニットテストの結果をレポートする
hkusu
0
3.9k
Other Decks in Programming
See All in Programming
Press start. Python's next generation.
willingc
PRO
3
300
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
53
34k
typoなんかねぇよ
raspython3
0
660
Go 1.27からのGODEBUG / Go 1.27 リリースパーティ #go127party
mazrean
0
290
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
130
Deep dive into the select statement (GopherCon UK)
jespino
0
170
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
350
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
2
1.5k
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
310
Building an Out-of-Order CPU
latte72
0
620
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
810
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
970
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
The SEO identity crisis: Don't let AI make you average
varn
0
550
How to train your dragon (web standard)
notwaldorf
97
6.8k
Designing Experiences People Love
moore
143
24k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
240
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
680
Rebuilding a faster, lazier Slack
samanthasiow
85
9.6k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
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_