Skip to content

Commit a4aeb8f

Browse files
committed
Feature Three Module dagger implementation and shared dependency
1 parent f30630c commit a4aeb8f

File tree

23 files changed

+266
-3
lines changed

23 files changed

+266
-3
lines changed

app/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies {
3232
implementation project(":base")
3333
implementation project(":feature-one")
3434
implementation project(":feature-two")
35+
implementation project(":feature-three")
3536

3637
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3738
implementation 'androidx.appcompat:appcompat:1.1.0'

app/src/main/java/com/ysfcyln/daggermultimodule/di/AppComponent.kt

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Application
44
import com.ysfcyln.base.BaseModule
55
import com.ysfcyln.daggermultimodule.di.main.MainComponent
66
import com.ysfcyln.feature_one.di.FeatureOneComponent
7+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
78
import com.ysfcyln.feature_two.di.FeatureTwoComponent
89
import dagger.BindsInstance
910
import dagger.Component
@@ -35,4 +36,7 @@ interface AppComponent {
3536
// Save the reference of factories in the app component for creating sub components
3637
fun featureTwoComponent() : FeatureTwoComponent.Factory
3738

39+
// Save the reference of factories in the app component for creating sub components
40+
fun featureThreeComponent() : FeatureThreeComponent.Factory
41+
3842
}

app/src/main/java/com/ysfcyln/daggermultimodule/di/SubComponents.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import com.ysfcyln.daggermultimodule.di.main.MainComponent
44
import com.ysfcyln.daggermultimodule.di.main.MainComponentProvider
55
import com.ysfcyln.feature_one.di.FeatureOneComponent
66
import com.ysfcyln.feature_one.di.FeatureOneComponentProvider
7+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
8+
import com.ysfcyln.feature_three.di.FeatureThreeComponentProvider
79
import com.ysfcyln.feature_two.di.FeatureTwoComponent
810
import com.ysfcyln.feature_two.di.FeatureTwoComponentProvider
911

10-
interface SubComponents: MainComponentProvider, FeatureOneComponentProvider, FeatureTwoComponentProvider {
12+
interface SubComponents: MainComponentProvider, FeatureOneComponentProvider, FeatureTwoComponentProvider,
13+
FeatureThreeComponentProvider {
1114

1215
override fun provideMainComponent(): MainComponent {
1316
return DiProvider.appComponent().mainComponent().create()
@@ -20,4 +23,8 @@ interface SubComponents: MainComponentProvider, FeatureOneComponentProvider, Fea
2023
override fun provideFeatureTwoComponent(): FeatureTwoComponent {
2124
return DiProvider.appComponent().featureTwoComponent().create()
2225
}
26+
27+
override fun provideFeatureThreeComponent(): FeatureThreeComponent {
28+
return DiProvider.appComponent().featureThreeComponent().create()
29+
}
2330
}

app/src/main/java/com/ysfcyln/daggermultimodule/di/SubComponentsModule.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.ysfcyln.daggermultimodule.di
22

33
import com.ysfcyln.daggermultimodule.di.main.MainComponent
44
import com.ysfcyln.feature_one.di.FeatureOneComponent
5+
import com.ysfcyln.feature_three.di.FeatureThreeComponent
56
import com.ysfcyln.feature_two.di.FeatureTwoComponent
67
import dagger.Module
78

@@ -12,7 +13,8 @@ import dagger.Module
1213
subcomponents = [
1314
MainComponent::class,
1415
FeatureOneComponent::class,
15-
FeatureTwoComponent::class
16+
FeatureTwoComponent::class,
17+
FeatureThreeComponent::class
1618
]
1719
)
1820
class SubComponentsModule {

feature-three/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

feature-three/build.gradle

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apply plugin: 'com.android.library'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
apply plugin: 'kotlin-kapt'
5+
6+
android {
7+
compileSdkVersion 29
8+
buildToolsVersion "29.0.2"
9+
10+
defaultConfig {
11+
minSdkVersion 21
12+
targetSdkVersion 29
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
consumerProguardFiles 'consumer-rules.pro'
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
27+
}
28+
29+
dependencies {
30+
implementation fileTree(dir: 'libs', include: ['*.jar'])
31+
32+
implementation project(":base")
33+
34+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
35+
implementation 'androidx.appcompat:appcompat:1.1.0'
36+
implementation 'androidx.core:core-ktx:1.2.0'
37+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
38+
testImplementation 'junit:junit:4.12'
39+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
40+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
41+
42+
// Dagger dependencies
43+
implementation "com.google.dagger:dagger:$daggerVersion"
44+
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
45+
}

feature-three/consumer-rules.pro

Whitespace-only changes.

feature-three/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.ysfcyln.feature_three
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.ysfcyln.feature_three.test", appContext.packageName)
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.ysfcyln.feature_three">
4+
5+
<application>
6+
<activity android:name=".FeatureThreeActivity"></activity>
7+
</application>
8+
9+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.ysfcyln.feature_three
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import com.ysfcyln.base.DatabaseService
7+
import com.ysfcyln.base.NetworkService
8+
import com.ysfcyln.feature_three.di.FeatureThreeComponentProvider
9+
import javax.inject.Inject
10+
11+
class FeatureThreeActivity : AppCompatActivity() {
12+
13+
@Inject
14+
lateinit var databaseService: DatabaseService
15+
16+
@Inject
17+
lateinit var networkService: NetworkService
18+
19+
@Inject
20+
lateinit var featureThreeObject: FeatureThreeObject
21+
22+
/**
23+
* Create component and inject
24+
*/
25+
private fun inject() {
26+
// When rotation happens component and its dependencies recreated :(
27+
val featureThreeComponent = (application as FeatureThreeComponentProvider).provideFeatureThreeComponent()
28+
featureThreeComponent.inject(this)
29+
}
30+
31+
override fun onCreate(savedInstanceState: Bundle?) {
32+
inject()
33+
super.onCreate(savedInstanceState)
34+
setContentView(R.layout.activity_feature_three)
35+
Log.d("FeatureThreeActivity", databaseService.toString())
36+
Log.d("FeatureThreeActivity", networkService.toString())
37+
Log.d("FeatureThreeActivity", featureThreeObject.toString())
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.ysfcyln.feature_three
2+
3+
class FeatureThreeObject {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.ysfcyln.feature_three.di
2+
3+
import com.ysfcyln.feature_three.FeatureThreeActivity
4+
import dagger.Subcomponent
5+
6+
@Subcomponent(
7+
modules = [
8+
// Bounded feature three activity necessary modules comes here
9+
FeatureThreeModule::class
10+
]
11+
)
12+
interface FeatureThreeComponent {
13+
14+
@Subcomponent.Factory
15+
interface Factory {
16+
fun create() : FeatureThreeComponent
17+
}
18+
19+
fun inject(featureThreeActivity: FeatureThreeActivity) // Add feature three activity to Dagger graph
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.ysfcyln.feature_three.di
2+
3+
interface FeatureThreeComponentProvider {
4+
5+
fun provideFeatureThreeComponent() : FeatureThreeComponent
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ysfcyln.feature_three.di
2+
3+
import com.ysfcyln.feature_three.FeatureThreeObject
4+
import dagger.Module
5+
import dagger.Provides
6+
7+
@Module
8+
class FeatureThreeModule {
9+
10+
@Provides
11+
fun provideFeatureThreeObject() = FeatureThreeObject()
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".FeatureThreeActivity">
8+
9+
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<resources></resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.ysfcyln.feature_three
2+
3+
import org.junit.Test
4+
5+
import org.junit.Assert.*
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* See [testing documentation](http://d.android.com/tools/testing).
11+
*/
12+
class ExampleUnitTest {
13+
@Test
14+
fun addition_isCorrect() {
15+
assertEquals(4, 2 + 2)
16+
}
17+
}

feature-two/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
implementation fileTree(dir: 'libs', include: ['*.jar'])
3131

3232
implementation project(":base")
33+
implementation project(":feature-three")
3334

3435
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
3536
implementation 'androidx.appcompat:appcompat:1.1.0'

feature-two/src/main/java/com/ysfcyln/feature_two/FeatureTwoActivity.kt

+19
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package com.ysfcyln.feature_two
22

3+
import android.content.Intent
34
import androidx.appcompat.app.AppCompatActivity
45
import android.os.Bundle
56
import android.util.Log
67
import com.ysfcyln.base.DatabaseService
78
import com.ysfcyln.base.NetworkService
9+
import com.ysfcyln.feature_three.FeatureThreeActivity
10+
import com.ysfcyln.feature_three.FeatureThreeObject
811
import com.ysfcyln.feature_two.di.FeatureTwoComponentProvider
12+
import kotlinx.android.synthetic.main.activity_feature_two.*
913
import javax.inject.Inject
1014

1115
class FeatureTwoActivity : AppCompatActivity() {
@@ -19,6 +23,9 @@ class FeatureTwoActivity : AppCompatActivity() {
1923
@Inject
2024
lateinit var featureTwoObject: FeatureTwoObject
2125

26+
@Inject
27+
lateinit var featureThreeObject: FeatureThreeObject // Shared dependency
28+
2229
/**
2330
* Create component and inject
2431
*/
@@ -35,5 +42,17 @@ class FeatureTwoActivity : AppCompatActivity() {
3542
Log.d("FeatureTwoActivity", databaseService.toString())
3643
Log.d("FeatureTwoActivity", networkService.toString())
3744
Log.d("FeatureTwoActivity", featureTwoObject.toString())
45+
Log.d("FeatureTwoActivity", featureThreeObject.toString())
46+
47+
clickListeners()
48+
}
49+
50+
/**
51+
* View click listeners
52+
*/
53+
private fun clickListeners() {
54+
btnFeatureThree.setOnClickListener {
55+
startActivity(Intent(this, FeatureThreeActivity::class.java))
56+
}
3857
}
3958
}

feature-two/src/main/java/com/ysfcyln/feature_two/di/FeatureTwoModule.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package com.ysfcyln.feature_two.di
22

3+
import com.ysfcyln.feature_three.di.FeatureThreeModule
34
import com.ysfcyln.feature_two.FeatureTwoObject
45
import dagger.Module
56
import dagger.Provides
67

7-
@Module
8+
@Module(
9+
includes = [
10+
FeatureThreeModule::class
11+
]
12+
)
813
class FeatureTwoModule {
914

1015
@Provides

feature-two/src/main/res/layout/activity_feature_two.xml

+12
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@
66
android:layout_height="match_parent"
77
tools:context=".FeatureTwoActivity">
88

9+
<Button
10+
android:id="@+id/btnFeatureThree"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="Open Feature Three!"
14+
app:layout_constraintBottom_toBottomOf="parent"
15+
app:layout_constraintLeft_toLeftOf="parent"
16+
app:layout_constraintRight_toRightOf="parent"
17+
app:layout_constraintTop_toTopOf="parent"
18+
android:textSize="14sp"
19+
android:textColor="@android:color/black"/>
20+
921
</androidx.constraintlayout.widget.ConstraintLayout>

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ include ':app'
33
include ':base'
44
include ':feature-one'
55
include ':feature-two'
6+
include ':feature-three'

0 commit comments

Comments
 (0)