mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-14 22:17:03 +05:30
refactor(di): move ApiModule to core and api
This commit is contained in:
parent
9d41b7e819
commit
005dc9ab83
4 changed files with 69 additions and 18 deletions
|
@ -9,12 +9,18 @@
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm")
|
kotlin("jvm")
|
||||||
id("dev.msfjarvis.claw.kotlin-library")
|
id("dev.msfjarvis.claw.kotlin-library")
|
||||||
|
alias(libs.plugins.anvil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
anvil { generateDaggerFactories.set(true) }
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api(projects.model)
|
api(projects.model)
|
||||||
api(libs.retrofit)
|
api(libs.retrofit)
|
||||||
api(libs.eithernet)
|
api(libs.eithernet)
|
||||||
|
implementation(projects.diScopes)
|
||||||
|
implementation(libs.dagger)
|
||||||
|
implementation(libs.javax.inject)
|
||||||
testImplementation(testFixtures(libs.eithernet))
|
testImplementation(testFixtures(libs.eithernet))
|
||||||
testImplementation(kotlin("test-junit"))
|
testImplementation(kotlin("test-junit"))
|
||||||
testImplementation(libs.kotlinx.coroutines.core)
|
testImplementation(libs.kotlinx.coroutines.core)
|
||||||
|
|
|
@ -4,39 +4,35 @@
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
*/
|
*/
|
||||||
package dev.msfjarvis.claw.android.injection
|
package dev.msfjarvis.claw.api.injection
|
||||||
|
|
||||||
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
|
||||||
import com.slack.eithernet.ApiResultCallAdapterFactory
|
|
||||||
import com.slack.eithernet.ApiResultConverterFactory
|
|
||||||
import com.squareup.anvil.annotations.ContributesTo
|
import com.squareup.anvil.annotations.ContributesTo
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import dev.msfjarvis.claw.api.LobstersApi
|
import dev.msfjarvis.claw.api.LobstersApi
|
||||||
import dev.msfjarvis.claw.injection.scopes.AppScope
|
import dev.msfjarvis.claw.injection.scopes.AppScope
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import okhttp3.MediaType.Companion.toMediaType
|
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
|
import retrofit2.CallAdapter
|
||||||
|
import retrofit2.Converter
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.create
|
import retrofit2.create
|
||||||
|
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
|
||||||
@Module
|
@Module
|
||||||
@ContributesTo(AppScope::class)
|
@ContributesTo(AppScope::class)
|
||||||
object ApiModule {
|
object ApiModule {
|
||||||
@Provides
|
@Provides
|
||||||
fun provideRetrofit(
|
fun provideRetrofit(
|
||||||
client: OkHttpClient,
|
client: OkHttpClient,
|
||||||
json: Json,
|
converterFactories: Set<@JvmSuppressWildcards Converter.Factory>,
|
||||||
|
callAdapterFactories: Set<@JvmSuppressWildcards CallAdapter.Factory>
|
||||||
): Retrofit {
|
): Retrofit {
|
||||||
val contentType = "application/json".toMediaType()
|
|
||||||
return Retrofit.Builder()
|
return Retrofit.Builder()
|
||||||
.client(client)
|
.client(client)
|
||||||
.baseUrl(LobstersApi.BASE_URL)
|
.baseUrl(LobstersApi.BASE_URL)
|
||||||
.addConverterFactory(ApiResultConverterFactory)
|
.apply {
|
||||||
.addConverterFactory(json.asConverterFactory(contentType))
|
converterFactories.forEach(this::addConverterFactory)
|
||||||
.addCallAdapterFactory(ApiResultCallAdapterFactory)
|
callAdapterFactories.forEach(this::addCallAdapterFactory)
|
||||||
|
}
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +40,4 @@ object ApiModule {
|
||||||
fun provideApi(retrofit: Retrofit): LobstersApi {
|
fun provideApi(retrofit: Retrofit): LobstersApi {
|
||||||
return retrofit.create()
|
return retrofit.create()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
|
||||||
fun provideJsonSerializer(): Json {
|
|
||||||
return Json { ignoreUnknownKeys = true }
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -18,10 +18,13 @@ anvil { generateDaggerFactories.set(true) }
|
||||||
android { namespace = "dev.msfjarvis.claw.core" }
|
android { namespace = "dev.msfjarvis.claw.core" }
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
api(libs.kotlinx.serialization.json)
|
||||||
api(libs.okhttp.loggingInterceptor)
|
api(libs.okhttp.loggingInterceptor)
|
||||||
implementation(projects.diScopes)
|
implementation(projects.diScopes)
|
||||||
implementation(libs.dagger)
|
implementation(libs.dagger)
|
||||||
|
implementation(libs.eithernet)
|
||||||
implementation(libs.javax.inject)
|
implementation(libs.javax.inject)
|
||||||
implementation(libs.napier)
|
implementation(libs.napier)
|
||||||
implementation(libs.okhttp.core)
|
implementation(libs.okhttp.core)
|
||||||
|
implementation(libs.retrofit.kotlinxSerializationConverter)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2022 Harsh Shandilya.
|
||||||
|
* Use of this source code is governed by an MIT-style
|
||||||
|
* license that can be found in the LICENSE file or at
|
||||||
|
* https://opensource.org/licenses/MIT.
|
||||||
|
*/
|
||||||
|
package dev.msfjarvis.claw.core.injection
|
||||||
|
|
||||||
|
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
|
||||||
|
import com.slack.eithernet.ApiResultCallAdapterFactory
|
||||||
|
import com.slack.eithernet.ApiResultConverterFactory
|
||||||
|
import com.squareup.anvil.annotations.ContributesTo
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
import dagger.multibindings.IntoSet
|
||||||
|
import dev.msfjarvis.claw.injection.scopes.AppScope
|
||||||
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import okhttp3.MediaType
|
||||||
|
import retrofit2.CallAdapter
|
||||||
|
import retrofit2.Converter
|
||||||
|
|
||||||
|
@Module
|
||||||
|
@ContributesTo(AppScope::class)
|
||||||
|
object RetrofitModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@IntoSet
|
||||||
|
fun provideApiResultCallAdapterFactory(): CallAdapter.Factory {
|
||||||
|
return ApiResultCallAdapterFactory
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@IntoSet
|
||||||
|
fun provideApiResultConverterFactory(): Converter.Factory {
|
||||||
|
return ApiResultConverterFactory
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
|
@Provides
|
||||||
|
@IntoSet
|
||||||
|
fun provideJsonConverterFactory(json: Json): Converter.Factory {
|
||||||
|
val contentType = MediaType.get("application/json")
|
||||||
|
return json.asConverterFactory(contentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
fun provideJsonSerializer(): Json {
|
||||||
|
return Json { ignoreUnknownKeys = true }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue