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

Inside Jetpack Architecture Components

kobito-kaba
September 26, 2018

Inside Jetpack Architecture Components

How LifecycleObservers, LiveData, ViewModels work

kobito-kaba

September 26, 2018
Tweet

More Decks by kobito-kaba

Other Decks in Programming

Transcript

  1. -JGFDZDMFT -JWF%BUB 7JFX.PEFM 3PPN 1BHJOH %BUB#JOEJOH /BWJHBUJPO 8PSL.BOBHFS %PXOMPBE.BOBHFS .FEJB1MBZCBDL

    1FSNJTTJPOT /PUJDBUJPOT 4IBSJOH 4MJDFT "OJNBUJPO5SBOTJUJPOT "VUP 578FBS &NPKJ 'SBHNFOU -BZPVU 1BMFUUF "QQ$PNQBU "OESPJE,59 .VMUJEFY 5FTU
  2. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat DBTFGFUDIJOHBUFWFSZPO$SFBUF     
  3. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ       
  4. The Japanese Bobtail is a breed of domestic cat with

    an unusual "bobbed" tail more closely resembling the tail of a rabbit than that of other cats. The variety is native to Japan and Southeast Asia, though it is now found throughout the world. The breed has been known in Japan for centuries, and it frequently appears in traditional folklore and art. Short Hair • Brown • White • Cute Caption Japanese Bobtail Cute Cat  DBTFEJTQPTJOHJOXSPOHXBZ        
  5. apply plugin : 'kotlin-kapt' dependencies { … def lifecycle_version =

    "2.0.0-rc01" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" } -JGFDZDMF0CTFSWFS
  6. dependencies { ... implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.22.5" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.22.5" implementation "com.squareup.okhttp3:okhttp:3.10.0" implementation

    "com.squareup.okhttp3:logging-interceptor:3.10.0" implementation "com.squareup.retrofit2:retrofit:2.4.0" implementation "com.squareup.retrofit2:converter-gson:2.4.0" implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0' implementation 'com.google.code.gson:gson:2.8.2' implementation 'androidx.recyclerview:recyclerview:1.0.0-rc02' } kotlin { experimental { coroutines "enable" } }
  7. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  8. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  9. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  10. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { val repos = github.repositories() _repos.postValue(repos) } return _repos } }
  11. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { // Coroutine way launch(CommonPool) { val repos = github.repositories().await() _repos.postValue(repos) } } return _repos } }
  12. class YourViewModel : ViewModel() { private val _repos = MutableLiveData<List<Repository>>()

    val repositories : LiveData<List<Repository>> get() { if (_repos.value == null) { // Rx way github.repositories() .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe { repositories.postValue(it) } } return _repos } }
  13. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... val viewModel = ViewModelProviders.of(this)[YourViewModel::class.java] } }
  14. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... val viewModel = ViewModelProviders.of(this)[YourViewModel::class.java] viewModel.repositories.observe(this, Observer { // update Views }) } }