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

Android Dagger Hilt migration #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 19 additions & 15 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'
apply plugin: 'de.mannodermaus.android-junit5'
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
id 'de.mannodermaus.android-junit5'
id 'dagger.hilt.android.plugin'
}

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.githubfirebaseissue"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -49,7 +49,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0'

api "androidx.appcompat:appcompat:$appcompat_version"
api "androidx.constraintlayout:constraintlayout:$androidx_constraint_layout_version"
Expand All @@ -66,7 +66,7 @@ dependencies {
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
api "com.squareup.retrofit2:adapter-rxjava:$retrofit_version"
api 'com.squareup.retrofit2:adapter-rxjava2:2.7.1'
api "com.squareup.okhttp3:okhttp:4.3.1"
api "com.squareup.okhttp3:okhttp:4.6.0"
api "com.squareup.okhttp3:logging-interceptor:$logging_interceptor_version"

api "com.github.bumptech.glide:glide:$glide_version"
Expand All @@ -87,7 +87,7 @@ dependencies {
api "com.google.code.gson:gson:$gson_version"

//shimmer
api 'com.facebook.shimmer:shimmer:0.1.0@aar'
api 'com.facebook.shimmer:shimmer:0.5.0@aar'

// dagger
implementation "com.google.dagger:dagger:$dagger_version"
Expand All @@ -102,11 +102,15 @@ dependencies {
testImplementation "com.jraska.livedata:testing-ktx:$jraska_version"
testImplementation "junit:junit:$junit_version"
testImplementation "org.mockito:mockito-inline:$mockito_version"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.4.1'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation "org.hamcrest:hamcrest-library:$hamcrest_version"
testImplementation "com.google.truth:truth:1.0"

//hilt
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

//Spek
testImplementation 'org.junit.platform:junit-platform-engine:1.6.2'
testImplementation 'org.jetbrains.spek:spek-api:1.1.5'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.example.githubfirebaseissue

import com.example.githubfirebaseissue.di.component.DaggerApplicationComponent
import dagger.android.AndroidInjector
import dagger.android.DaggerApplication
import android.app.Application
import dagger.hilt.android.HiltAndroidApp


class GithubApplication : DaggerApplication() {

override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerApplicationComponent.factory().create(this)
}
}
@HiltAndroidApp
class GithubApplication : Application()
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.example.githubfirebaseissue.base

import android.os.Bundle
import dagger.android.AndroidInjection
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import dagger.android.support.DaggerAppCompatActivity
import dagger.hilt.android.AndroidEntryPoint


abstract class BaseActivity : DaggerAppCompatActivity() {
abstract class BaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(getLayoutRes())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.githubfirebaseissue.R
import dagger.android.support.AndroidSupportInjection
import okio.IOException
import retrofit2.HttpException


abstract class BaseFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package com.example.githubfirebaseissue.common
import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

interface RxScheduler {
val io: Scheduler
val main: Scheduler
}

class BaseSchedulerProvider: RxScheduler {
class BaseSchedulerProvider @Inject constructor() : RxScheduler {
override val io: Scheduler
get() = Schedulers.io()
override val main: Scheduler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package com.example.githubfirebaseissue.di.component

import com.example.githubfirebaseissue.GithubApplication
import com.example.githubfirebaseissue.di.module.ApiModule
import com.example.githubfirebaseissue.di.module.ActivityBindingModule
import com.example.githubfirebaseissue.di.module.ViewModelModule
import com.example.githubfirebaseissue.di.scope.AppScope
import dagger.Component
import dagger.android.AndroidInjector
import dagger.android.support.AndroidSupportInjectionModule
import javax.inject.Singleton

@AppScope
@Component(
modules = [
AndroidSupportInjectionModule::class,
ActivityBindingModule::class,
ViewModelModule::class,
ApiModule::class]
)
interface ApplicationComponent : AndroidInjector<GithubApplication> {

@Component.Factory
abstract class Builder: AndroidInjector.Factory<GithubApplication>
}
@Singleton
@Component(modules = [ApiModule::class])
interface ApplicationComponent

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ import com.example.githubfirebaseissue.api.ApiConstant
import com.example.githubfirebaseissue.api.GithubApi
import com.example.githubfirebaseissue.common.BaseSchedulerProvider
import com.example.githubfirebaseissue.common.RxScheduler
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory


@InstallIn(SingletonComponent::class)
@Module(includes = [InterceptorModule::class, RepositoryModule::class])
class ApiModule {
abstract class ApiModule {

@Provides
fun provideRetrofit(client: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(ApiConstant.GITHUB_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
}
companion object {
@Provides
fun provideRetrofit(client: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl(ApiConstant.GITHUB_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
}

@Provides
fun provideApiInterface(retrofit: Retrofit): GithubApi {
return retrofit.create(GithubApi::class.java)
@Provides
fun provideApiInterface(retrofit: Retrofit): GithubApi {
return retrofit.create(GithubApi::class.java)
}
}

@Provides
fun provideRxScheduler(): RxScheduler {
return BaseSchedulerProvider()
}
@Binds
abstract fun provideRxScheduler(scheduler: BaseSchedulerProvider): RxScheduler
}

This file was deleted.

Loading