From 8bf564b0b105139a502306aa1c801fc50027ff64 Mon Sep 17 00:00:00 2001 From: Serchinastico <54cymru@gmail.com> Date: Thu, 31 Jan 2019 15:12:46 +0100 Subject: [PATCH] Remove unused lib and simplify code to make the livedata-viewmodel part easier to write --- app/build.gradle | 6 ------ .../superheroes/SuperHeroesApplication.kt | 21 +++++++++---------- .../jetpack/superheroes/common/kodein.kt | 16 ++++++++++++++ .../superheroes/ui/view/BaseActivity.kt | 4 ++-- .../ui/view/EditSuperHeroActivity.kt | 20 ++++++++---------- .../superheroes/ui/view/MainActivity.kt | 13 ++++++------ .../ui/view/SuperHeroDetailActivity.kt | 18 +++++++--------- 7 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/karumi/jetpack/superheroes/common/kodein.kt diff --git a/app/build.gradle b/app/build.gradle index cba598c..8aaef8b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,12 +63,6 @@ dependencies { exclude group: "com.android.support" } androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2" - - androidTestImplementation("com.facebook.testing.screenshot:core:0.8.0") { - exclude group: "com.crittercism.dexmaker", module: "dexmaker" - exclude group: "com.crittercism.dexmaker", module: "dexmaker-dx" - } - androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.1.0" } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt b/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt index b21bd36..0e636a3 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt @@ -2,6 +2,7 @@ package com.karumi.jetpack.superheroes import android.app.Application import android.content.Context +import com.karumi.jetpack.superheroes.common.module import com.karumi.jetpack.superheroes.data.repository.LocalSuperHeroDataSource import com.karumi.jetpack.superheroes.data.repository.RemoteSuperHeroDataSource import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository @@ -27,17 +28,15 @@ class SuperHeroesApplication : Application(), KodeinAware { } } - private fun appDependencies(): Kodein.Module { - return Kodein.Module("Application dependencies", allowSilentOverride = true) { - bind() with provider { - SuperHeroRepository(instance(), instance()) - } - bind() with singleton { - LocalSuperHeroDataSource() - } - bind() with provider { - RemoteSuperHeroDataSource() - } + private fun appDependencies(): Kodein.Module = module { + bind() with provider { + SuperHeroRepository(instance(), instance()) + } + bind() with singleton { + LocalSuperHeroDataSource() + } + bind() with provider { + RemoteSuperHeroDataSource() } } } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/common/kodein.kt b/app/src/main/java/com/karumi/jetpack/superheroes/common/kodein.kt new file mode 100644 index 0000000..b6ffb41 --- /dev/null +++ b/app/src/main/java/com/karumi/jetpack/superheroes/common/kodein.kt @@ -0,0 +1,16 @@ +package com.karumi.jetpack.superheroes.common + +import org.kodein.di.Kodein +import org.kodein.di.KodeinAware + +inline fun T.module( + allowSilentOverride: Boolean = false, + prefix: String = "", + noinline init: Kodein.Builder.() -> Unit +) where T : KodeinAware = + Kodein.Module( + "${T::class.java.simpleName} dependencies", + allowSilentOverride, + prefix, + init + ) 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 5e0adf6..ef0fc78 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 @@ -23,8 +23,8 @@ abstract class BaseActivity : AppCompatActivity(), KodeinAware { super.onCreate(savedInstanceState) setContentView(layoutId) setSupportActionBar(toolbarView) - preparePresenter(intent) + prepare(intent) } - open fun preparePresenter(intent: Intent?) {} + 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 56af60a..2d3b446 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 @@ -6,13 +6,13 @@ 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.domain.model.SuperHero import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById import com.karumi.jetpack.superheroes.domain.usecase.SaveSuperHero import com.karumi.jetpack.superheroes.ui.presenter.EditSuperHeroPresenter import com.karumi.jetpack.superheroes.ui.utils.setImageBackground import kotlinx.android.synthetic.main.edit_super_hero_activity.* -import org.kodein.di.Kodein import org.kodein.di.erased.bind import org.kodein.di.erased.instance import org.kodein.di.erased.provider @@ -32,8 +32,7 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { override val layoutId = R.layout.edit_super_hero_activity override val toolbarView: Toolbar get() = toolbar - private val superHeroId: String - get() = intent?.extras?.getString(SUPER_HERO_ID_KEY) ?: "" + private val superHeroId: String by lazy { intent?.extras?.getString(SUPER_HERO_ID_KEY) ?: "" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -56,7 +55,7 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { presenter.onDestroy() } - override fun preparePresenter(intent: Intent?) { + override fun prepare(intent: Intent?) { title = superHeroId presenter.preparePresenter(superHeroId) } @@ -86,12 +85,11 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View { cb_is_avenger.isChecked = superHero.isAvenger } - override val activityModules = - Kodein.Module("EditSuperHeroActivity dependencies", allowSilentOverride = true) { - bind() with provider { - EditSuperHeroPresenter(this@EditSuperHeroActivity, instance(), instance()) - } - bind() with provider { GetSuperHeroById(instance()) } - bind() with provider { SaveSuperHero(instance()) } + override val activityModules = module { + bind() with provider { + EditSuperHeroPresenter(this@EditSuperHeroActivity, instance(), instance()) } + bind() with provider { GetSuperHeroById(instance()) } + bind() with provider { SaveSuperHero(instance()) } + } } \ No newline at end of file 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..de4349a 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 @@ -5,12 +5,12 @@ 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.domain.model.SuperHero import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroes import com.karumi.jetpack.superheroes.ui.presenter.SuperHeroesPresenter import com.karumi.jetpack.superheroes.ui.view.adapter.SuperHeroesAdapter import kotlinx.android.synthetic.main.main_activity.* -import org.kodein.di.Kodein import org.kodein.di.erased.bind import org.kodein.di.erased.instance import org.kodein.di.erased.provider @@ -74,11 +74,10 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View { ) } - override val activityModules = - Kodein.Module("MainActivity dependencies", allowSilentOverride = true) { - bind() with provider { - SuperHeroesPresenter(this@MainActivity, instance()) - } - bind() with provider { GetSuperHeroes(instance()) } + override val activityModules = module { + bind() with provider { + SuperHeroesPresenter(this@MainActivity, instance()) } + bind() with provider { GetSuperHeroes(instance()) } + } } 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 a55ced0..91aa05c 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 @@ -6,12 +6,12 @@ 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.domain.model.SuperHero import com.karumi.jetpack.superheroes.domain.usecase.GetSuperHeroById import com.karumi.jetpack.superheroes.ui.presenter.SuperHeroDetailPresenter import com.karumi.jetpack.superheroes.ui.utils.setImageBackground import kotlinx.android.synthetic.main.super_hero_detail_activity.* -import org.kodein.di.Kodein import org.kodein.di.erased.bind import org.kodein.di.erased.instance import org.kodein.di.erased.provider @@ -32,8 +32,7 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { override val layoutId: Int = R.layout.super_hero_detail_activity override val toolbarView: Toolbar get() = toolbar - private val superHeroId: String - get() = intent?.extras?.getString(SUPER_HERO_ID_KEY) ?: "" + private val superHeroId: String by lazy { intent?.extras?.getString(SUPER_HERO_ID_KEY) ?: "" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -50,7 +49,7 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { presenter.onDestroy() } - override fun preparePresenter(intent: Intent?) { + override fun prepare(intent: Intent?) { title = superHeroId presenter.preparePresenter(superHeroId) } @@ -80,11 +79,10 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View { EditSuperHeroActivity.open(this, superHeroId) } - override val activityModules = - Kodein.Module("SuperHeroDetailActivity dependencies", allowSilentOverride = true) { - bind() with provider { - SuperHeroDetailPresenter(this@SuperHeroDetailActivity, instance()) - } - bind() with provider { GetSuperHeroById(instance()) } + override val activityModules = module { + bind() with provider { + SuperHeroDetailPresenter(this@SuperHeroDetailActivity, instance()) } + bind() with provider { GetSuperHeroById(instance()) } + } } \ No newline at end of file