Skip to content

Commit fb8a504

Browse files
committed
ref: introduce feature abstraction/contract
1 parent 0d2e737 commit fb8a504

File tree

6 files changed

+124
-56
lines changed

6 files changed

+124
-56
lines changed

app/src/main/kotlin/com/fernandocejas/sample/AndroidApplication.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package com.fernandocejas.sample
1717

1818
import android.app.Application
19-
import com.fernandocejas.sample.core.di.appModule
19+
import com.fernandocejas.sample.core.allFeatures
20+
import com.fernandocejas.sample.core.di.coreModule
2021
import org.koin.android.ext.koin.androidContext
2122
import org.koin.android.ext.koin.androidLogger
2223
import org.koin.core.context.GlobalContext.startKoin
@@ -34,7 +35,7 @@ class AndroidApplication : Application() {
3435
startKoin {
3536
androidContext(this@AndroidApplication)
3637
androidLogger()
37-
modules(appModule)
38+
modules(allFeatures().map { it.diModule() })
3839
}
3940
}
4041
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fernandocejas.sample.core
2+
3+
import com.fernandocejas.sample.core.di.coreModule
4+
import com.fernandocejas.sample.features.login.loginFeature
5+
import com.fernandocejas.sample.features.movies.moviesFeature
6+
import org.koin.core.module.Module
7+
8+
/**
9+
* The fundamental piece of modularity.
10+
*
11+
* An Application always contains features,
12+
* and that is represented by this
13+
* contract/abstraction.
14+
*/
15+
interface Feature {
16+
17+
/**
18+
* Convention: The feature name should follow
19+
* the package name where the feature is contained
20+
* using lower case.
21+
*
22+
* Examples:
23+
* - core
24+
* - login
25+
* - movies
26+
*/
27+
fun name(): String
28+
29+
/**
30+
* Koin DI Module that will be included
31+
* when creating the Dependency Graph
32+
* at Application Startup.
33+
*
34+
* In case of scoping this will facilitate
35+
* refactor.
36+
*/
37+
fun diModule(): Module
38+
39+
/**
40+
* LEARNING PURPOSE:
41+
* In order to keep modularity, each feature could
42+
* point to the tables in the database related
43+
* to the feature itself.
44+
*/
45+
// fun databaseTables(): List<Table> = emptyList()
46+
}
47+
48+
private fun coreFeature() = object : Feature {
49+
override fun name() = "core"
50+
override fun diModule() = coreModule
51+
}
52+
53+
fun allFeatures() = listOf(
54+
coreFeature(),
55+
loginFeature(),
56+
moviesFeature(),
57+
)

app/src/main/kotlin/com/fernandocejas/sample/core/di/AppModule.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fernandocejas.sample.core.di
2+
3+
import com.fernandocejas.sample.core.navigation.Navigator
4+
import com.fernandocejas.sample.core.platform.NetworkHandler
5+
import okhttp3.OkHttpClient
6+
import org.koin.core.module.dsl.singleOf
7+
import org.koin.dsl.module
8+
import retrofit2.Retrofit
9+
import retrofit2.converter.gson.GsonConverterFactory
10+
11+
val coreModule = module {
12+
singleOf(::retrofit)
13+
singleOf(::NetworkHandler)
14+
singleOf(::Navigator)
15+
}
16+
17+
private fun retrofit(): Retrofit {
18+
return Retrofit.Builder()
19+
.baseUrl("https://raw.githubusercontent.com/android10/Sample-Data/master/Android-CleanArchitecture-Kotlin/")
20+
.client(OkHttpClient.Builder().build())
21+
.addConverterFactory(GsonConverterFactory.create())
22+
.build()
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.fernandocejas.sample.features.login
2+
3+
import com.fernandocejas.sample.core.Feature
4+
import org.koin.core.module.dsl.singleOf
5+
import org.koin.dsl.module
6+
7+
fun loginFeature() = object : Feature {
8+
9+
override fun name() = "login"
10+
11+
override fun diModule() = module {
12+
singleOf(::Authenticator)
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fernandocejas.sample.features.movies
2+
3+
import com.fernandocejas.sample.core.Feature
4+
import org.koin.androidx.viewmodel.dsl.viewModelOf
5+
import org.koin.core.module.dsl.factoryOf
6+
import org.koin.dsl.bind
7+
import org.koin.dsl.module
8+
9+
fun moviesFeature() = object : Feature {
10+
11+
override fun name() = "movies"
12+
13+
override fun diModule() = module {
14+
// Movies Feature
15+
factoryOf(::MoviesService)
16+
factory { MoviesRepository.Network(get(), get()) } bind MoviesRepository::class
17+
// Movies
18+
viewModelOf(::MoviesViewModel)
19+
factoryOf(::GetMovies)
20+
factoryOf(::MoviesAdapter)
21+
// Movie Details
22+
viewModelOf(::MovieDetailsViewModel)
23+
factoryOf(::GetMovieDetails)
24+
factoryOf(::PlayMovie)
25+
factoryOf(::MovieDetailsAnimator)
26+
}
27+
}

0 commit comments

Comments
 (0)