File tree Expand file tree Collapse file tree 6 files changed +124
-56
lines changed
app/src/main/kotlin/com/fernandocejas/sample Expand file tree Collapse file tree 6 files changed +124
-56
lines changed Original file line number Diff line number Diff line change 16
16
package com.fernandocejas.sample
17
17
18
18
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
20
21
import org.koin.android.ext.koin.androidContext
21
22
import org.koin.android.ext.koin.androidLogger
22
23
import org.koin.core.context.GlobalContext.startKoin
@@ -34,7 +35,7 @@ class AndroidApplication : Application() {
34
35
startKoin {
35
36
androidContext(this @AndroidApplication)
36
37
androidLogger()
37
- modules(appModule )
38
+ modules(allFeatures().map { it.diModule() } )
38
39
}
39
40
}
40
41
}
Original file line number Diff line number Diff line change
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
+ )
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments