Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

01-lifecycle #5

Open
wants to merge 1 commit into
base: 00-start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.weak
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById
Expand All @@ -11,7 +15,7 @@ class EditSuperHeroPresenter(
private val getSuperHeroById: GetSuperHeroById,
private val saveSuperHero: SaveSuperHero,
private val executor: ExecutorService
) : EditSuperHeroListener {
) : EditSuperHeroListener, LifecycleObserver {

private val view: View? by weak(view)
private lateinit var id: String
Expand All @@ -21,11 +25,13 @@ class EditSuperHeroPresenter(
this.id = id
}

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

@OnLifecycleEvent(ON_DESTROY)
fun onDestroy() {
executor.shutdownNow()
}
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.weak
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById
Expand All @@ -9,7 +13,7 @@ class SuperHeroDetailPresenter(
view: View,
private val getSuperHeroById: GetSuperHeroById,
private val executor: ExecutorService
) : SuperHeroDetailListener {
) : SuperHeroDetailListener, LifecycleObserver {

private val view: View? by weak(view)

Expand All @@ -19,11 +23,13 @@ class SuperHeroDetailPresenter(
this.id = id
}

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

@OnLifecycleEvent(ON_DESTROY)
fun onDestroy() {
executor.shutdownNow()
}
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.weak
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroes
Expand All @@ -9,15 +13,17 @@ class SuperHeroesPresenter(
view: View,
private val getSuperHeroes: GetSuperHeroes,
private val executor: ExecutorService
) {
) : LifecycleObserver {

private val view: View? by weak(view)

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

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