mirror of
https://github.com/msfjarvis/compose-lobsters.git
synced 2024-06-03 04:18:58 +05:30
refactor(di): move ApiModule to core and api
This commit is contained in:
parent
9d41b7e819
commit
005dc9ab83
|
@ -9,12 +9,18 @@
|
|||
plugins {
|
||||
kotlin("jvm")
|
||||
id("dev.msfjarvis.claw.kotlin-library")
|
||||
alias(libs.plugins.anvil)
|
||||
}
|
||||
|
||||
anvil { generateDaggerFactories.set(true) }
|
||||
|
||||
dependencies {
|
||||
api(projects.model)
|
||||
api(libs.retrofit)
|
||||
api(libs.eithernet)
|
||||
implementation(projects.diScopes)
|
||||
implementation(libs.dagger)
|
||||
implementation(libs.javax.inject)
|
||||
testImplementation(testFixtures(libs.eithernet))
|
||||
testImplementation(kotlin("test-junit"))
|
||||
testImplementation(libs.kotlinx.coroutines.core)
|
||||
|
|
|
@ -4,39 +4,35 @@
|
|||
* license that can be found in the LICENSE file or at
|
||||
* 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 dagger.Module
|
||||
import dagger.Provides
|
||||
import dev.msfjarvis.claw.api.LobstersApi
|
||||
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 retrofit2.CallAdapter
|
||||
import retrofit2.Converter
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.create
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@Module
|
||||
@ContributesTo(AppScope::class)
|
||||
object ApiModule {
|
||||
@Provides
|
||||
fun provideRetrofit(
|
||||
client: OkHttpClient,
|
||||
json: Json,
|
||||
converterFactories: Set<@JvmSuppressWildcards Converter.Factory>,
|
||||
callAdapterFactories: Set<@JvmSuppressWildcards CallAdapter.Factory>
|
||||
): Retrofit {
|
||||
val contentType = "application/json".toMediaType()
|
||||
return Retrofit.Builder()
|
||||
.client(client)
|
||||
.baseUrl(LobstersApi.BASE_URL)
|
||||
.addConverterFactory(ApiResultConverterFactory)
|
||||
.addConverterFactory(json.asConverterFactory(contentType))
|
||||
.addCallAdapterFactory(ApiResultCallAdapterFactory)
|
||||
.apply {
|
||||
converterFactories.forEach(this::addConverterFactory)
|
||||
callAdapterFactories.forEach(this::addCallAdapterFactory)
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
|
@ -44,9 +40,4 @@ object ApiModule {
|
|||
fun provideApi(retrofit: Retrofit): LobstersApi {
|
||||
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" }
|
||||
|
||||
dependencies {
|
||||
api(libs.kotlinx.serialization.json)
|
||||
api(libs.okhttp.loggingInterceptor)
|
||||
implementation(projects.diScopes)
|
||||
implementation(libs.dagger)
|
||||
implementation(libs.eithernet)
|
||||
implementation(libs.javax.inject)
|
||||
implementation(libs.napier)
|
||||
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…
Reference in New Issue
Block a user