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

03a-data-binding #9

Open
wants to merge 1 commit into
base: 02b-room-migrations
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class EditSuperHeroPresenter(

interface View {
fun close()
fun showLoading()
fun hideLoading()
fun showLoading()
fun showSuperHero(superHero: SuperHero)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class SuperHeroDetailPresenter(
}

interface View {
fun showLoading()
fun hideLoading()
fun showLoading()
fun showSuperHero(superHero: SuperHero)
fun openEditSuperHero(superHeroId: String)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleObserver
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.android.closestKodein

abstract class BaseActivity : AppCompatActivity(), KodeinAware {
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity(), KodeinAware {

private val appKodein by closestKodein()
override val kodein: Kodein = Kodein.lazy {
Expand All @@ -20,11 +22,13 @@ abstract class BaseActivity : AppCompatActivity(), KodeinAware {
abstract val layoutId: Int
abstract val toolbarView: Toolbar
abstract val activityModules: Kodein.Module
protected lateinit var binding: T

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(presenter)
setContentView(layoutId)
binding = DataBindingUtil.setContentView(this, layoutId)
configureBinding(binding)
setSupportActionBar(toolbarView)
prepare(intent)
}
Expand All @@ -34,5 +38,6 @@ abstract class BaseActivity : AppCompatActivity(), KodeinAware {
lifecycle.removeObserver(presenter)
}

abstract fun configureBinding(binding: T)
open fun prepare(intent: Intent?) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.karumi.jetpack.superheroes.ui.view
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.karumi.jetpack.superheroes.R
import com.karumi.jetpack.superheroes.common.module
import com.karumi.jetpack.superheroes.databinding.EditSuperHeroActivityBinding
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById
import com.karumi.jetpack.superheroes.domain.usecase.SaveSuperHero
Expand All @@ -17,7 +17,10 @@ import org.kodein.di.erased.bind
import org.kodein.di.erased.instance
import org.kodein.di.erased.provider

class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View {
class EditSuperHeroActivity :
BaseActivity<EditSuperHeroActivityBinding>(),
EditSuperHeroPresenter.View {

companion object {
private const val SUPER_HERO_ID_KEY = "super_hero_id_key"

Expand Down Expand Up @@ -45,6 +48,11 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View {
}
}

override fun configureBinding(binding: EditSuperHeroActivityBinding) {
binding.listener = presenter
binding.isLoading = false
}

override fun prepare(intent: Intent?) {
title = superHeroId
presenter.preparePresenter(superHeroId)
Expand All @@ -55,24 +63,16 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View {
}

override fun showLoading() = runOnUiThread {
et_super_hero_name.isEnabled = false
et_super_hero_description.isEnabled = false
bt_save_edition.isEnabled = false
progress_bar.visibility = View.VISIBLE
binding.isLoading = true
}

override fun hideLoading() = runOnUiThread {
et_super_hero_name.isEnabled = true
et_super_hero_description.isEnabled = true
bt_save_edition.isEnabled = true
progress_bar.visibility = View.GONE
binding.isLoading = false
}

override fun showSuperHero(superHero: SuperHero) = runOnUiThread {
et_super_hero_name.setText(superHero.name)
et_super_hero_description.setText(superHero.description)
binding.superHero = superHero
iv_super_hero_photo.setImageBackground(superHero.photo)
cb_is_avenger.isChecked = superHero.isAvenger
}

override val activityModules = module {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.karumi.jetpack.superheroes.ui.view

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.recyclerview.widget.LinearLayoutManager
import com.karumi.jetpack.superheroes.R
import com.karumi.jetpack.superheroes.common.module
import com.karumi.jetpack.superheroes.databinding.MainActivityBinding
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroes
import com.karumi.jetpack.superheroes.ui.presenter.SuperHeroesPresenter
Expand All @@ -15,7 +15,7 @@ import org.kodein.di.erased.bind
import org.kodein.di.erased.instance
import org.kodein.di.erased.provider

class MainActivity : BaseActivity(), SuperHeroesPresenter.View {
class MainActivity : BaseActivity<MainActivityBinding>(), SuperHeroesPresenter.View {

override val presenter: SuperHeroesPresenter by instance()
private lateinit var adapter: SuperHeroesAdapter
Expand All @@ -29,6 +29,11 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View {
initializeRecyclerView()
}

override fun configureBinding(binding: MainActivityBinding) {
binding.isLoading = false
binding.isShowingEmptyCase = false
}

private fun initializeAdapter() {
adapter = SuperHeroesAdapter(presenter)
}
Expand All @@ -40,15 +45,15 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View {
}

override fun showLoading() = runOnUiThread {
progress_bar.visibility = View.VISIBLE
binding.isLoading = true
}

override fun hideLoading() = runOnUiThread {
progress_bar.visibility = View.GONE
binding.isLoading = false
}

override fun showEmptyCase() = runOnUiThread {
tv_empty_case.visibility = View.VISIBLE
binding.isShowingEmptyCase = true
}

override fun showSuperHeroes(superHeroes: List<SuperHero>) = runOnUiThread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package com.karumi.jetpack.superheroes.ui.view

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.karumi.jetpack.superheroes.R
import com.karumi.jetpack.superheroes.common.module
import com.karumi.jetpack.superheroes.databinding.SuperHeroDetailActivityBinding
import com.karumi.jetpack.superheroes.domain.model.SuperHero
import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById
import com.karumi.jetpack.superheroes.ui.presenter.SuperHeroDetailPresenter
Expand All @@ -16,8 +15,9 @@ import org.kodein.di.erased.bind
import org.kodein.di.erased.instance
import org.kodein.di.erased.provider

class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {

class SuperHeroDetailActivity :
BaseActivity<SuperHeroDetailActivityBinding>(),
SuperHeroDetailPresenter.View {
companion object {
private const val SUPER_HERO_ID_KEY = "super_hero_id_key"

Expand All @@ -34,9 +34,9 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {
get() = toolbar
private val superHeroId: String by lazy { intent?.extras?.getString(SUPER_HERO_ID_KEY) ?: "" }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
edit_super_hero.setOnClickListener { presenter.onEditSelected() }
override fun configureBinding(binding: SuperHeroDetailActivityBinding) {
binding.listener = presenter
binding.isLoading = false
}

override fun prepare(intent: Intent?) {
Expand All @@ -45,22 +45,17 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {
}

override fun showLoading() = runOnUiThread {
progress_bar.visibility = View.VISIBLE
binding.isLoading = true
}

override fun hideLoading() = runOnUiThread {
progress_bar.visibility = View.GONE
binding.isLoading = false
}

override fun showSuperHero(superHero: SuperHero) = runOnUiThread {
title = superHero.name
tv_super_hero_name.text = superHero.name
tv_super_hero_description.text = superHero.description
iv_avengers_badge.visibility =
if (superHero.isAvenger) View.VISIBLE else View.GONE
binding.superHero = superHero
iv_super_hero_photo.setImageBackground(superHero.photo)
edit_super_hero.visibility = View.VISIBLE
super_hero_background.visibility = View.VISIBLE
}

override fun openEditSuperHero(superHeroId: String) = runOnUiThread {
Expand Down
Loading