Skip to content

Commit

Permalink
chore: 🚚 split koin modules by feature (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffgiraldez committed May 6, 2018
1 parent 3d3bdf0 commit 096d5cd
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 29 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/es/ffgiraldez/comicsearch/ComicApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ package es.ffgiraldez.comicsearch

import android.app.Application
import com.facebook.stetho.Stetho
import es.ffgiraldez.comicsearch.di.comicContext
import es.ffgiraldez.comicsearch.comics.di.comicModule
import es.ffgiraldez.comicsearch.navigation.di.navigationModule
import es.ffgiraldez.comicsearch.query.search.di.searchModule
import es.ffgiraldez.comicsearch.query.sugestion.di.suggestionModule
import org.koin.android.ext.android.startKoin

class ComicApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin(this, listOf(comicContext))
startKoin(this, listOf(
navigationModule,
comicModule,
searchModule,
suggestionModule
))
Stetho.initializeWithDefaults(this)
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package es.ffgiraldez.comicsearch.di
package es.ffgiraldez.comicsearch.comics.di

import android.arch.persistence.room.Room
import es.ffgiraldez.comicsearch.comics.data.network.ComicVineApi
import es.ffgiraldez.comicsearch.comics.data.storage.ComicDatabase
import es.ffgiraldez.comicsearch.navigation.Navigator
import es.ffgiraldez.comicsearch.query.search.data.SearchLocalDataSource
import es.ffgiraldez.comicsearch.query.search.data.SearchRemoteDataSource
import es.ffgiraldez.comicsearch.query.search.data.SearchRepository
import es.ffgiraldez.comicsearch.query.search.presentation.SearchViewModel
import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionLocalDataSource
import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionRemoteDataSource
import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionRepository
import es.ffgiraldez.comicsearch.query.sugestion.presentation.SuggestionViewModel
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.dsl.context.ParameterProvider
Expand All @@ -23,7 +14,7 @@ import retrofit2.converter.gson.GsonConverterFactory
const val ACTIVITY_PARAM: String = "activity"
const val CONTEXT_PARAM: String = "context"

val comicContext = applicationContext {
val comicModule = applicationContext {
factory {
val okHttp = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
Expand All @@ -39,16 +30,4 @@ val comicContext = applicationContext {
.create(ComicVineApi::class.java)
}
bean { params: ParameterProvider -> Room.databaseBuilder(params[CONTEXT_PARAM], ComicDatabase::class.java, "comics").build() }

factory { SearchLocalDataSource(get({ it.values })) }
factory { SearchRemoteDataSource(get()) }
factory { SearchRepository(get({ it.values }), get()) }
factory { SearchViewModel(get()) }

factory { SuggestionLocalDataSource(get({ it.values })) }
factory { SuggestionRemoteDataSource(get()) }
factory { SuggestionRepository(get({ it.values }), get()) }
factory { SuggestionViewModel(get({ it.values })) }

factory { params -> Navigator(params[ACTIVITY_PARAM]) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package es.ffgiraldez.comicsearch.navigation.di

import es.ffgiraldez.comicsearch.navigation.Navigator
import org.koin.dsl.module.applicationContext

const val ACTIVITY_PARAM: String = "activity"
const val CONTEXT_PARAM: String = "context"

val navigationModule = applicationContext {
factory { params -> Navigator(params[ACTIVITY_PARAM]) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.arch.lifecycle.LiveData
import android.arch.lifecycle.Observer
import io.reactivex.BackpressureStrategy
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.android.MainThreadDisposable

fun <T> LiveData<T>.toFlowable(): Flowable<T> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import es.ffgiraldez.comicsearch.R
import es.ffgiraldez.comicsearch.databinding.QueryActivityBinding
import es.ffgiraldez.comicsearch.di.ACTIVITY_PARAM
import es.ffgiraldez.comicsearch.di.CONTEXT_PARAM
import es.ffgiraldez.comicsearch.navigation.di.ACTIVITY_PARAM
import es.ffgiraldez.comicsearch.navigation.di.CONTEXT_PARAM
import es.ffgiraldez.comicsearch.navigation.Navigator
import es.ffgiraldez.comicsearch.query.search.presentation.SearchViewModel
import es.ffgiraldez.comicsearch.query.sugestion.presentation.SuggestionViewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package es.ffgiraldez.comicsearch.query.search.di

import es.ffgiraldez.comicsearch.query.search.data.SearchLocalDataSource
import es.ffgiraldez.comicsearch.query.search.data.SearchRemoteDataSource
import es.ffgiraldez.comicsearch.query.search.data.SearchRepository
import es.ffgiraldez.comicsearch.query.search.presentation.SearchViewModel
import org.koin.dsl.module.applicationContext

val searchModule = applicationContext {
factory { SearchLocalDataSource(get({ it.values })) }
factory { SearchRemoteDataSource(get()) }
factory { SearchRepository(get({ it.values }), get()) }
factory { SearchViewModel(get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package es.ffgiraldez.comicsearch.query.sugestion.di

import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionLocalDataSource
import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionRemoteDataSource
import es.ffgiraldez.comicsearch.query.sugestion.data.SuggestionRepository
import es.ffgiraldez.comicsearch.query.sugestion.presentation.SuggestionViewModel
import org.koin.dsl.module.applicationContext

val suggestionModule = applicationContext {
factory { SuggestionLocalDataSource(get({ it.values })) }
factory { SuggestionRemoteDataSource(get()) }
factory { SuggestionRepository(get({ it.values }), get()) }
factory { SuggestionViewModel(get({ it.values })) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import android.app.Activity
import android.arch.core.executor.testing.InstantTaskExecutorRule
import android.content.Context
import com.nhaarman.mockito_kotlin.mock
import es.ffgiraldez.comicsearch.comics.di.comicModule
import es.ffgiraldez.comicsearch.navigation.di.ACTIVITY_PARAM
import es.ffgiraldez.comicsearch.navigation.di.CONTEXT_PARAM
import es.ffgiraldez.comicsearch.navigation.di.navigationModule
import es.ffgiraldez.comicsearch.query.search.di.searchModule
import es.ffgiraldez.comicsearch.query.sugestion.di.suggestionModule
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
Expand All @@ -15,10 +21,16 @@ class TestContextResolution : KoinTest {

@get:Rule
var rule: TestRule = InstantTaskExecutorRule()

@Test
fun `dry run`() {
// start Koin
startKoin(listOf(comicContext))
startKoin(listOf(
navigationModule,
comicModule,
searchModule,
suggestionModule
))
// dry run of given module list
dryRun(defaultParameters = {
mapOf(
Expand Down

0 comments on commit 096d5cd

Please sign in to comment.