Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11 - Migrate to Hilt #12

Merged
merged 4 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
alias libs.plugins.hilt
}

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
Expand All @@ -10,7 +14,7 @@ android {
defaultConfig {
applicationId "com.ivanalvarado.template"
minSdkVersion 22
targetSdkVersion 31
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -37,6 +41,10 @@ kapt {
correctErrorTypes = true
}

hilt {
enableAggregatingTask = false
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand Down Expand Up @@ -117,5 +125,8 @@ dependencies {

implementation libs.timber

implementation libs.hilt.android
kapt libs.hilt.compiler

testImplementation libs.truth
}
32 changes: 3 additions & 29 deletions app/src/main/java/com/ivanalvarado/template/AppController.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
package com.ivanalvarado.template

import android.app.Activity
import android.app.Application
import com.ivanalvarado.template.di.component.DaggerAppComponent
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import javax.inject.Inject
import dagger.hilt.android.HiltAndroidApp


/*
* we use our AppComponent (now prefixed with Dagger)
* to inject our Application class.
* This way a DispatchingAndroidInjector is injected which is
* then returned when an injector for an activity is requested.
* */
class AppController : Application(), HasActivityInjector {

@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Activity>

override fun activityInjector(): DispatchingAndroidInjector<Activity>? {
return dispatchingAndroidInjector
}

override fun onCreate() {
super.onCreate()
DaggerAppComponent.builder()
.application(this)
.build()
.inject(this)
}
}
@HiltAndroidApp
class AppController : Application()
32 changes: 4 additions & 28 deletions app/src/main/java/com/ivanalvarado/template/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
package com.ivanalvarado.template

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.ivanalvarado.template.viewmodel.ExampleViewModel
import dagger.android.AndroidInjection
import javax.inject.Inject
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

/*
* Step 1: Here as mentioned in Step 5, we need to
* inject the ViewModelFactory. The ViewModelFactory class
* has a list of ViewModels and will provide
* the corresponding ViewModel in this activity
* */
@Inject
internal lateinit var viewModelFactory: ViewModelProvider.Factory

private lateinit var exampleViewModel: ExampleViewModel
private val exampleViewModel: ExampleViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
/*
* Step 2: Remember in our ActivityModule, we
* defined MainActivity injection? So we need
* to call this method in order to inject the
* ViewModelFactory into our Activity
* */
AndroidInjection.inject(this)

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

setUpViewModel()
}

private fun setUpViewModel() {
exampleViewModel =
ViewModelProviders.of(this, viewModelFactory).get(ExampleViewModel::class.java)
}
}
25 changes: 0 additions & 25 deletions app/src/main/java/com/ivanalvarado/template/di/ViewModelFactory.kt

This file was deleted.

13 changes: 0 additions & 13 deletions app/src/main/java/com/ivanalvarado/template/di/ViewModelKey.kt

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import com.ivanalvarado.template.database.AppDatabase
import com.ivanalvarado.template.database.dao.ExampleDao
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


@Module
@InstallIn(SingletonComponent::class)
class DbModule {

@Provides
@Singleton
internal fun provideDatabase(application: Application): AppDatabase {
return Room.databaseBuilder(
application, AppDatabase::class.java, "AndroidBootstrap.db"
).allowMainThreadQueries().build()
}

@Provides
@Singleton
internal fun provideExampleDao(appDatabase: AppDatabase): ExampleDao {
return appDatabase.exampleDao()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.ivanalvarado.template.di.module

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
class LoggingModule {
@Provides
@Singleton
fun providesHttpLoggingInterceptor(): HttpLoggingInterceptor {
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package com.ivanalvarado.template.di.module
import android.app.Application
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.ivanalvarado.template.BuildConfig
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand All @@ -14,30 +15,27 @@ import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit
import javax.inject.Singleton

private const val BASE_URL = "https://base.url.com"

@Module(includes = [LoggingModule::class])
@Module
@InstallIn(SingletonComponent::class)
class NetworkModule {

@Provides
@Singleton
internal fun provideGson(): Gson {
val gsonBuilder = GsonBuilder()
return gsonBuilder.create()
}

@Provides
@Singleton
internal fun provideCache(application: Application): Cache {
val cacheSize = (10 * 1024 * 1024).toLong() // 10 MB
val httpCacheDirectory = File(application.cacheDir, "http-cache")
return Cache(httpCacheDirectory, cacheSize)
}

@Provides
@Singleton
internal fun provideOkHttpClient(
cache: Cache,
loggingInterceptor: HttpLoggingInterceptor
Expand All @@ -53,7 +51,6 @@ class NetworkModule {
}

@Provides
@Singleton
internal fun provideRetrofit(gson: Gson, okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package com.ivanalvarado.template.di.module
import com.ivanalvarado.template.network.services.ExampleApiService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import javax.inject.Singleton

@Module(includes = [NetworkModule::class])
@Module
@InstallIn(SingletonComponent::class)
class ServiceModule {

@Provides
@Singleton
internal fun provideExampleApiService(retrofit: Retrofit): ExampleApiService {
return retrofit.create(ExampleApiService::class.java)
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.ivanalvarado.template.viewmodel

import androidx.lifecycle.ViewModel
import com.ivanalvarado.template.repository.ExampleRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class ExampleViewModel @Inject constructor(private val exampleRepository: ExampleRepository) :
ViewModel() {

Expand Down
Loading