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

create hilt modules #34

Merged
merged 1 commit into from
Mar 16, 2023
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
28 changes: 28 additions & 0 deletions app/src/main/java/com/jumrukovski/quotescompose/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jumrukovski.quotescompose.di

import android.app.Application
import android.content.Context
import com.jumrukovski.quotescompose.data.network.ApiService
import com.jumrukovski.quotescompose.data.repository.Repository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

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

@Provides
@Singleton
internal fun provideContext(application: Application): Context {
return application
}

@Provides
@Singleton
fun provideRepository(apiService: ApiService): Repository {
return Repository(apiService)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.jumrukovski.quotescompose.di

import android.content.Context
import com.jumrukovski.quotescompose.BuildConfig
import com.jumrukovski.quotescompose.data.network.ApiService
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
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import javax.inject.Named
import javax.inject.Singleton

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

@Provides
@Singleton
fun provideService(retrofit: Retrofit): ApiService {
return retrofit.create(ApiService::class.java)
}

@Provides
@Singleton
@Named("apiEndpoint")
fun provideApiEndpoint(): String {
return "https://api.quotable.io/"
}

@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient, @Named("apiEndpoint") apiEndpoint: String): Retrofit {
return Retrofit.Builder()
.baseUrl(apiEndpoint)
.addConverterFactory(MoshiConverterFactory.create())
.client(okHttpClient)
.build()
}

@Provides
@Singleton
fun provideOkHttpClient(httpLoggingInterceptor: HttpLoggingInterceptor, cache: Cache): OkHttpClient {
val builder = OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.cache(cache)
return builder.build()
}

@Provides
@Singleton
fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = if (BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
return loggingInterceptor
}

@Provides
@Singleton
fun provideHttpCache(context: Context): Cache {
val cacheSize = 10 * 1024 * 1024
return Cache(context.cacheDir, cacheSize.toLong())
}
}