From 69a3132f4eec4ff5c5979897f2e92ebb6988840c Mon Sep 17 00:00:00 2001 From: Serchinastico <54cymru@gmail.com> Date: Tue, 29 Jan 2019 16:48:40 +0100 Subject: [PATCH] Add lifecycle events to activity/presenters --- app/build.gradle | 1 + .../ui/presenter/EditSuperHeroPresenter.kt | 8 +++++++- .../ui/presenter/SuperHeroDetailPresenter.kt | 8 +++++++- .../superheroes/ui/presenter/SuperHeroesPresenter.kt | 8 +++++++- .../superheroes/ui/view/EditSuperHeroActivity.kt | 11 +---------- .../jetpack/superheroes/ui/view/MainActivity.kt | 11 +---------- .../superheroes/ui/view/SuperHeroDetailActivity.kt | 11 +---------- 7 files changed, 25 insertions(+), 33 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 42adbf2..a58218b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,7 @@ dependencies { implementation "androidx.appcompat:appcompat:1.0.2" implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha3" implementation "androidx.recyclerview:recyclerview:1.0.0" + implementation "androidx.lifecycle:lifecycle-runtime:2.0.0" /* Coroutines */ implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0" diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/EditSuperHeroPresenter.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/EditSuperHeroPresenter.kt index dd78da1..c12e58b 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/EditSuperHeroPresenter.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/EditSuperHeroPresenter.kt @@ -1,5 +1,9 @@ package com.karumi.jetpack.superheroes.ui.presenter +import androidx.lifecycle.Lifecycle.Event.ON_DESTROY +import androidx.lifecycle.Lifecycle.Event.ON_RESUME +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent import com.karumi.jetpack.superheroes.common.async import com.karumi.jetpack.superheroes.common.weak import com.karumi.jetpack.superheroes.domain.model.SuperHero @@ -14,7 +18,7 @@ class EditSuperHeroPresenter( view: View, private val getSuperHeroById: GetSuperHeroById, private val saveSuperHero: SaveSuperHero -) : CoroutineScope by MainScope() { +) : LifecycleObserver, CoroutineScope by MainScope() { private val view: View? by weak(view) private lateinit var id: String @@ -28,11 +32,13 @@ class EditSuperHeroPresenter( } } + @OnLifecycleEvent(ON_RESUME) fun onResume() { view?.showLoading() refreshSuperHero() } + @OnLifecycleEvent(ON_DESTROY) fun onDestroy() { cancel() } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroDetailPresenter.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroDetailPresenter.kt index 956a096..228df46 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroDetailPresenter.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroDetailPresenter.kt @@ -1,5 +1,9 @@ package com.karumi.jetpack.superheroes.ui.presenter +import androidx.lifecycle.Lifecycle.Event.ON_DESTROY +import androidx.lifecycle.Lifecycle.Event.ON_RESUME +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent import com.karumi.jetpack.superheroes.common.async import com.karumi.jetpack.superheroes.common.weak import com.karumi.jetpack.superheroes.domain.model.SuperHero @@ -12,7 +16,7 @@ import kotlinx.coroutines.launch class SuperHeroDetailPresenter( view: View, private val getSuperHeroById: GetSuperHeroById -) : CoroutineScope by MainScope() { +) : LifecycleObserver, CoroutineScope by MainScope() { private val view: View? by weak(view) @@ -26,11 +30,13 @@ class SuperHeroDetailPresenter( } } + @OnLifecycleEvent(ON_RESUME) fun onResume() { view?.showLoading() refreshSuperHero() } + @OnLifecycleEvent(ON_DESTROY) fun onDestroy() { cancel() } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroesPresenter.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroesPresenter.kt index fb79f8f..284229d 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroesPresenter.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/presenter/SuperHeroesPresenter.kt @@ -1,5 +1,9 @@ package com.karumi.jetpack.superheroes.ui.presenter +import androidx.lifecycle.Lifecycle.Event.ON_DESTROY +import androidx.lifecycle.Lifecycle.Event.ON_RESUME +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent import com.karumi.jetpack.superheroes.common.async import com.karumi.jetpack.superheroes.common.weak import com.karumi.jetpack.superheroes.domain.model.SuperHero @@ -12,17 +16,19 @@ import kotlinx.coroutines.launch class SuperHeroesPresenter( view: View, private val getSuperHeroes: GetSuperHeroes -) : CoroutineScope by MainScope() { +) : LifecycleObserver, CoroutineScope by MainScope() { private val view: View? by weak( view ) + @OnLifecycleEvent(ON_RESUME) fun onResume() { view?.showLoading() refreshSuperHeroes() } + @OnLifecycleEvent(ON_DESTROY) fun onDestroy() { cancel() } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt index 56af60a..177ac2a 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt @@ -37,6 +37,7 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + lifecycle.addObserver(presenter) bt_save_edition.setOnClickListener { presenter.onSaveSuperHeroSelected( name = et_super_hero_name.text?.toString() ?: "", @@ -46,16 +47,6 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { } } - override fun onResume() { - super.onResume() - presenter.onResume() - } - - override fun onDestroy() { - super.onDestroy() - presenter.onDestroy() - } - override fun preparePresenter(intent: Intent?) { title = superHeroId presenter.preparePresenter(superHeroId) diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt index 78a7360..8c909be 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt @@ -25,20 +25,11 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + lifecycle.addObserver(presenter) initializeAdapter() initializeRecyclerView() } - override fun onResume() { - super.onResume() - presenter.onResume() - } - - override fun onDestroy() { - super.onDestroy() - presenter.onDestroy() - } - private fun initializeAdapter() { adapter = SuperHeroesAdapter(presenter) } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt index ffc9246..718ddbe 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt @@ -37,6 +37,7 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + lifecycle.addObserver(presenter) edit_super_hero.setOnClickListener { EditSuperHeroActivity.open( this@SuperHeroDetailActivity, @@ -45,16 +46,6 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { } } - override fun onResume() { - super.onResume() - presenter.onResume() - } - - override fun onDestroy() { - super.onDestroy() - presenter.onDestroy() - } - override fun preparePresenter(intent: Intent?) { title = superHeroId presenter.preparePresenter(superHeroId)