datt16 / MONEA-DEVTOOL

MONEAの開発用ツール
0 stars 0 forks source link

ViewModel を使いたい #1

Closed datt16 closed 2 years ago

datt16 commented 2 years ago

ViewModelでデータを扱えるようにしたい

理由:

課題:

datt16 commented 2 years ago

LiveDataを使う場合

https://developer.android.com/topic/libraries/architecture/livedata?hl=ja#create_livedata_objects

MutableLiveDataで定義 .setValue で更新 Observerで監視

val nameObserver = Observer<String> { newName -> UIの処理 }
datt16 commented 2 years ago

Kotlin Flowを使う場合

https://medium.com/firebase-developers/firebase-with-jetpack-compose-cloud-firestore-39d8d139536a

Repository内

Compose上

datt16 commented 2 years ago

ちなみにFactoryってなんだろう

datt16 commented 2 years ago

ViewModelProvider.Factory

https://developer.android.com/codelabs/kotlin-android-training-view-model?authuser=19&hl=ja#7

A factory method is a method that returns an instance of the same class.

ViewModelをインスタンス化して返すメソッドらしい