mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 17:37:05 +05:30
android: add Hilt module for LobstersApi
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
294052e309
commit
8f4d270c87
5 changed files with 81 additions and 0 deletions
|
@ -2,15 +2,20 @@ plugins {
|
|||
id("org.jetbrains.compose") version "0.4.0"
|
||||
id("com.android.application")
|
||||
kotlin("android")
|
||||
kotlin("kapt")
|
||||
id("dagger.hilt.android.plugin")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
kapt(libs.dagger.hilt.compiler)
|
||||
implementation(projects.api)
|
||||
implementation(projects.common)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.androidx.appcompat)
|
||||
implementation(libs.androidx.paging.compose)
|
||||
implementation(libs.dagger.hilt.android)
|
||||
implementation(libs.retrofit.moshiConverter)
|
||||
implementation(libs.moshix.metadatareflect)
|
||||
}
|
||||
|
||||
android {
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package dev.msfjarvis.claw.android.injection
|
||||
|
||||
import android.util.Log
|
||||
import com.squareup.moshi.Moshi
|
||||
import dagger.Lazy
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dev.msfjarvis.claw.api.LobstersApi
|
||||
import okhttp3.OkHttpClient
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.moshi.MoshiConverterFactory
|
||||
import retrofit2.create
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object ApiModule {
|
||||
|
||||
@Provides
|
||||
fun provideClient(): OkHttpClient {
|
||||
return OkHttpClient.Builder()
|
||||
.addNetworkInterceptor { chain ->
|
||||
val request = chain.request()
|
||||
Log.d("LobstersApi", "${request.method()}: ${request.url()}")
|
||||
chain.proceed(request)
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Using [Lazy] here is a trick I picked up from Zac Sweers, which he explained in more detail
|
||||
* here: https://www.zacsweers.dev/dagger-party-tricks-deferred-okhttp-init/
|
||||
*/
|
||||
@Provides
|
||||
fun provideRetrofit(
|
||||
client: Lazy<OkHttpClient>,
|
||||
moshi: Lazy<Moshi>,
|
||||
): Retrofit {
|
||||
return Retrofit.Builder()
|
||||
.client(client.get())
|
||||
.baseUrl("https://lobste.rs/")
|
||||
.addConverterFactory(MoshiConverterFactory.create(moshi.get()))
|
||||
.build()
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideApi(retrofit: Retrofit): LobstersApi {
|
||||
return retrofit.create()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package dev.msfjarvis.claw.android.injection
|
||||
|
||||
import com.squareup.moshi.Moshi
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.Reusable
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import dev.zacsweers.moshix.reflect.MetadataKotlinJsonAdapterFactory
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object MoshiModule {
|
||||
@Provides
|
||||
@Reusable
|
||||
fun provideMoshi(): Moshi {
|
||||
return Moshi.Builder().add(MetadataKotlinJsonAdapterFactory()).build()
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ buildscript {
|
|||
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10")
|
||||
classpath("com.android.tools.build:gradle:4.1.1")
|
||||
classpath("com.diffplug.spotless:spotless-plugin-gradle:5.12.5")
|
||||
classpath("com.google.dagger:hilt-android-gradle-plugin:2.36")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
[versions]
|
||||
coroutines = "1.5.0"
|
||||
hilt = "2.36"
|
||||
moshix = "0.11.2"
|
||||
retrofit = "2.9.0"
|
||||
sqldelight = "1.5.0"
|
||||
|
@ -15,6 +16,10 @@ androidx-appcompat = "androidx.appcompat:appcompat:1.4.0-alpha02"
|
|||
androidx-browser = "androidx.browser:browser:1.3.0"
|
||||
androidx-paging-compose = "androidx.paging:paging-compose:1.0.0-alpha10"
|
||||
|
||||
dagger-hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
|
||||
dagger-hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hilt" }
|
||||
dagger-hilt-core = { module = "com.google.dagger:hilt-core", version.ref = "hilt" }
|
||||
|
||||
moshi-lib = "com.squareup.moshi:moshi:1.12.0"
|
||||
moshix-ksp = { module = "dev.zacsweers.moshix:moshi-ksp", version.ref = "moshix" }
|
||||
moshix-metadatareflect = { module = "dev.zacsweers.moshix:moshi-metadata-reflect", version.ref = "moshix" }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue