From 6d082593e3cfe8bea5e5249098db1f36b849ba31 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 +++++++- .../jetpack/superheroes/ui/view/BaseActivity.kt | 8 ++++++++ .../superheroes/ui/view/EditSuperHeroActivity.kt | 12 +----------- .../jetpack/superheroes/ui/view/MainActivity.kt | 12 +----------- .../superheroes/ui/view/SuperHeroDetailActivity.kt | 12 +----------- 8 files changed, 33 insertions(+), 36 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 8aaef8b..9dea4d2 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 d81d5d2..6030992 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 b75f5db..a40246a 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 de71f35..d7d80e7 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/BaseActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt index ef0fc78..261b95a 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.lifecycle.LifecycleObserver import org.kodein.di.Kodein import org.kodein.di.KodeinAware import org.kodein.di.android.closestKodein @@ -15,16 +16,23 @@ abstract class BaseActivity : AppCompatActivity(), KodeinAware { extend(appKodein) import(activityModules) } + abstract val presenter: LifecycleObserver abstract val layoutId: Int abstract val toolbarView: Toolbar abstract val activityModules: Kodein.Module override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + lifecycle.addObserver(presenter) setContentView(layoutId) setSupportActionBar(toolbarView) prepare(intent) } + override fun onDestroy() { + super.onDestroy() + lifecycle.removeObserver(presenter) + } + open fun prepare(intent: Intent?) {} } \ No newline at end of file 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 2d3b446..5545d73 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 @@ -28,7 +28,7 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { } } - private val presenter: EditSuperHeroPresenter by instance() + override val presenter: EditSuperHeroPresenter by instance() override val layoutId = R.layout.edit_super_hero_activity override val toolbarView: Toolbar get() = toolbar @@ -45,16 +45,6 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { } } - override fun onResume() { - super.onResume() - presenter.onResume() - } - - override fun onDestroy() { - super.onDestroy() - presenter.onDestroy() - } - override fun prepare(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 de4349a..6e21142 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 @@ -17,7 +17,7 @@ import org.kodein.di.erased.provider class MainActivity : BaseActivity(), SuperHeroesPresenter.View { - private val presenter: SuperHeroesPresenter by instance() + override val presenter: SuperHeroesPresenter by instance() private lateinit var adapter: SuperHeroesAdapter override val layoutId: Int = R.layout.main_activity override val toolbarView: Toolbar @@ -29,16 +29,6 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View { 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 91aa05c..e07dc48 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 @@ -28,7 +28,7 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { } } - private val presenter: SuperHeroDetailPresenter by instance() + override val presenter: SuperHeroDetailPresenter by instance() override val layoutId: Int = R.layout.super_hero_detail_activity override val toolbarView: Toolbar get() = toolbar @@ -39,16 +39,6 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { edit_super_hero.setOnClickListener { presenter.onEditSelected() } } - override fun onResume() { - super.onResume() - presenter.onResume() - } - - override fun onDestroy() { - super.onDestroy() - presenter.onDestroy() - } - override fun prepare(intent: Intent?) { title = superHeroId presenter.preparePresenter(superHeroId)