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
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
560
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
900
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
400
これって Effect でできたのでは? / TSKaigi Mashup Kansai #2
susisu
0
220
源内ハンズオン概要編
hideg
0
170
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
150
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
830
What's New in Android 2026
veronikapj
0
260
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
190
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
160
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
170
VibeCodingからAgenticWorkflowへ
starfish719
0
420
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
530
The Language of Interfaces
destraynor
162
27k
RailsConf 2023
tenderlove
30
1.5k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
A better future with KSS
kneath
240
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.7k
Documentation Writing (for coders)
carmenintech
77
5.4k
Mind Mapping
helmedeiros
PRO
1
310
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
610
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_