Skip to content

Commit

Permalink
Add lifecycle events to activity/presenters
Browse files Browse the repository at this point in the history
  • Loading branch information
Serchinastico committed Jan 29, 2019
1 parent 36b3c67 commit 4a245ab
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -28,11 +32,13 @@ class EditSuperHeroPresenter(
}
}

@OnLifecycleEvent(ON_RESUME)
fun onResume() {
view?.showLoading()
refreshSuperHero()
}

@OnLifecycleEvent(ON_DESTROY)
fun onDestroy() {
cancel()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -26,11 +30,13 @@ class SuperHeroDetailPresenter(
}
}

@OnLifecycleEvent(ON_RESUME)
fun onResume() {
view?.showLoading()
refreshSuperHero()
}

@OnLifecycleEvent(ON_DESTROY)
fun onDestroy() {
cancel()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() ?: "",
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 4a245ab

Please sign in to comment.