Skip to content

Commit b9e9b84

Browse files
authored
Fix theMr17#10: Add Dagger Hilt for DI and Setup ApiModule (theMr17#13)
<!-- This will be automatically replaced by a summary generated by CodeRabbitAI --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced dependency injection support using Dagger Hilt across the app. - Added centralized HTTP client provisioning for network operations. - **Chores** - Updated build configuration and dependencies to support Dagger Hilt and Kotlin Symbol Processing (KSP). - Configured the app to use a custom Application class for improved lifecycle management. <!-- end of auto-generated comment: release notes by coderabbit.ai --> ## Issue Reference * Fixes theMr17#10 ## Essential Checklist <!-- Please tick the relevant boxes by putting an "x" in them (and remove additional spaces). --> * [x] The PR title starts with "Fix #bugnum: " (If this PR fixes part of an issue, prefix the title with "Fix part of #bugnum: ..."). * [x] The PR does not contain any unnecessary code changes from Android Studio. * [x] The PR is made from a branch that is **not** called "main" and is up-to-date with "main".
1 parent 4399282 commit b9e9b84

File tree

7 files changed

+40
-0
lines changed

7 files changed

+40
-0
lines changed

app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ plugins {
33
alias(libs.plugins.kotlin.android)
44
alias(libs.plugins.kotlin.compose)
55
alias(libs.plugins.kotlin.serialization)
6+
alias(libs.plugins.ksp)
7+
alias(libs.plugins.dagger.hilt)
68
}
79

810
android {
@@ -58,6 +60,8 @@ dependencies {
5860
implementation(libs.androidx.ui.tooling.preview)
5961
implementation(libs.androidx.material3)
6062
implementation(libs.bundles.ktor)
63+
implementation(libs.dagger.hilt)
64+
ksp(libs.dagger.hilt.compiler)
6165

6266
testImplementation(libs.junit)
6367
testImplementation(libs.truth)

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools">
44

55
<application
6+
android:name=".GithubNotifierApp"
67
android:icon="@mipmap/ic_launcher"
78
android:label="@string/app_name"
89
android:roundIcon="@mipmap/ic_launcher_round"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.notifier.app
2+
3+
import android.app.Application
4+
import dagger.hilt.android.HiltAndroidApp
5+
6+
@HiltAndroidApp
7+
class GithubNotifierApp : Application()

app/src/main/java/com/notifier/app/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import androidx.compose.runtime.Composable
1212
import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.tooling.preview.Preview
1414
import com.notifier.app.ui.theme.GitHubNotifierTheme
15+
import dagger.hilt.android.AndroidEntryPoint
1516

17+
@AndroidEntryPoint
1618
class MainActivity : ComponentActivity() {
1719
override fun onCreate(savedInstanceState: Bundle?) {
1820
super.onCreate(savedInstanceState)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.notifier.app.di
2+
3+
import com.notifier.app.core.data.networking.HttpClientFactory
4+
import dagger.Module
5+
import dagger.Provides
6+
import dagger.hilt.InstallIn
7+
import dagger.hilt.components.SingletonComponent
8+
import io.ktor.client.HttpClient
9+
import io.ktor.client.engine.cio.CIO
10+
11+
@Module
12+
@InstallIn(SingletonComponent::class)
13+
object ApiModule {
14+
@Provides
15+
fun provideHttpClient(): HttpClient {
16+
return HttpClientFactory.create(CIO.create())
17+
}
18+
}

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ plugins {
33
alias(libs.plugins.android.application) apply false
44
alias(libs.plugins.kotlin.android) apply false
55
alias(libs.plugins.kotlin.compose) apply false
6+
alias(libs.plugins.ksp) apply false
7+
alias(libs.plugins.dagger.hilt) apply false
68
}

gradle/libs.versions.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ktor = "3.1.2"
1212
truth = "1.1.3"
1313
kotlinxCoroutinesTest = "1.10.1"
1414
mockk = "1.13.8"
15+
ksp = "2.0.21-1.0.27"
16+
dagger-hilt = "2.56.1"
1517

1618
[libraries]
1719
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -37,12 +39,16 @@ ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
3739
truth = { group = "com.google.truth", name = "truth", version.ref = "truth" }
3840
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinxCoroutinesTest" }
3941
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
42+
dagger-hilt = { group = "com.google.dagger", name = "hilt-android", version.ref = "dagger-hilt" }
43+
dagger-hilt-compiler = { group = "com.google.dagger", name = "hilt-android-compiler", version.ref = "dagger-hilt" }
4044

4145
[plugins]
4246
android-application = { id = "com.android.application", version.ref = "agp" }
4347
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
4448
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
4549
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
50+
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
51+
dagger-hilt = { id = "com.google.dagger.hilt.android", version.ref = "dagger-hilt" }
4652

4753
[bundles]
4854
ktor = [

0 commit comments

Comments
 (0)