Skip to content

Commit

Permalink
Remove unused lib and simplify code to make the livedata-viewmodel pa…
Browse files Browse the repository at this point in the history
…rt easier to write
  • Loading branch information
Serchinastico committed Jan 31, 2019
1 parent a973b91 commit 8bf564b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 47 deletions.
6 changes: 0 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,17 +28,15 @@ class SuperHeroesApplication : Application(), KodeinAware {
}
}

private fun appDependencies(): Kodein.Module {
return Kodein.Module("Application dependencies", allowSilentOverride = true) {
bind<SuperHeroRepository>() with provider {
SuperHeroRepository(instance(), instance())
}
bind<LocalSuperHeroDataSource>() with singleton {
LocalSuperHeroDataSource()
}
bind<RemoteSuperHeroDataSource>() with provider {
RemoteSuperHeroDataSource()
}
private fun appDependencies(): Kodein.Module = module {
bind<SuperHeroRepository>() with provider {
SuperHeroRepository(instance(), instance())
}
bind<LocalSuperHeroDataSource>() with singleton {
LocalSuperHeroDataSource()
}
bind<RemoteSuperHeroDataSource>() with provider {
RemoteSuperHeroDataSource()
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/karumi/jetpack/superheroes/common/kodein.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.karumi.jetpack.superheroes.common

import org.kodein.di.Kodein
import org.kodein.di.KodeinAware

inline fun <reified T> 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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<EditSuperHeroPresenter>() with provider {
EditSuperHeroPresenter(this@EditSuperHeroActivity, instance(), instance())
}
bind<GetSuperHeroById>() with provider { GetSuperHeroById(instance()) }
bind<SaveSuperHero>() with provider { SaveSuperHero(instance()) }
override val activityModules = module {
bind<EditSuperHeroPresenter>() with provider {
EditSuperHeroPresenter(this@EditSuperHeroActivity, instance(), instance())
}
bind<GetSuperHeroById>() with provider { GetSuperHeroById(instance()) }
bind<SaveSuperHero>() with provider { SaveSuperHero(instance()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,11 +74,10 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View {
)
}

override val activityModules =
Kodein.Module("MainActivity dependencies", allowSilentOverride = true) {
bind<SuperHeroesPresenter>() with provider {
SuperHeroesPresenter(this@MainActivity, instance())
}
bind<GetSuperHeroes>() with provider { GetSuperHeroes(instance()) }
override val activityModules = module {
bind<SuperHeroesPresenter>() with provider {
SuperHeroesPresenter(this@MainActivity, instance())
}
bind<GetSuperHeroes>() with provider { GetSuperHeroes(instance()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -80,11 +79,10 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {
EditSuperHeroActivity.open(this, superHeroId)
}

override val activityModules =
Kodein.Module("SuperHeroDetailActivity dependencies", allowSilentOverride = true) {
bind<SuperHeroDetailPresenter>() with provider {
SuperHeroDetailPresenter(this@SuperHeroDetailActivity, instance())
}
bind<GetSuperHeroById>() with provider { GetSuperHeroById(instance()) }
override val activityModules = module {
bind<SuperHeroDetailPresenter>() with provider {
SuperHeroDetailPresenter(this@SuperHeroDetailActivity, instance())
}
bind<GetSuperHeroById>() with provider { GetSuperHeroById(instance()) }
}
}

0 comments on commit 8bf564b

Please sign in to comment.