Refactor `:core:network` DI

- Delete unused `NetworkModule` from benchmark sourceSet (contains unknowns references)
- Extract common bindings into a single `NetworkModule`
- Expose "flavored" bindings through `FlavoredNetworkModule`s
pull/417/head
Simon Marquis 2 years ago
parent 7e0bfb43fa
commit ee8eac34b8

@ -16,39 +16,17 @@
package com.google.samples.apps.nowinandroid.core.network.di package com.google.samples.apps.nowinandroid.core.network.di
import android.content.Context
import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiaNetworkDataSource
import dagger.Binds import dagger.Binds
import dagger.Module import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import kotlinx.serialization.json.Json
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
interface NetworkModule { interface FlavoredNetworkModule {
@Binds @Binds
fun bindsNiaNetwork( fun FakeNiaNetworkDataSource.binds(): NiaNetworkDataSource
niANetwork: FakeNiaNetworkDataSource
): NiaNetworkDataSource
companion object {
@Provides
@Singleton
fun providesNetworkJson(): Json = Json {
ignoreUnknownKeys = true
}
@Provides
@Singleton
fun providesFakeAssetManager(
@ApplicationContext context: Context,
): FakeAssetManager = FakeAssetManager(context.assets::open)
}
} }

@ -17,28 +17,19 @@
package com.google.samples.apps.nowinandroid.core.network.di package com.google.samples.apps.nowinandroid.core.network.di
import android.content.Context import android.content.Context
import com.google.samples.apps.nowinandroid.core.network.NiANetwork
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
import com.google.samples.apps.nowinandroid.core.network.fake.FakeNiANetwork
import dagger.Binds
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton import javax.inject.Singleton
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
interface NetworkModule { object NetworkModule {
@Binds
fun bindsNiANetwork(
niANetwork: FakeNiANetwork
): NiANetwork
companion object {
@Provides @Provides
@Singleton @Singleton
fun providesNetworkJson(): Json = Json { fun providesNetworkJson(): Json = Json {
@ -48,7 +39,6 @@ interface NetworkModule {
@Provides @Provides
@Singleton @Singleton
fun providesFakeAssetManager( fun providesFakeAssetManager(
@ApplicationContext context: Context @ApplicationContext context: Context,
): FakeAssetManager = FakeAssetManager(context.assets::open) ): FakeAssetManager = FakeAssetManager(context.assets::open)
}
} }

@ -16,39 +16,17 @@
package com.google.samples.apps.nowinandroid.core.network.di package com.google.samples.apps.nowinandroid.core.network.di
import android.content.Context
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource
import com.google.samples.apps.nowinandroid.core.network.retrofit.RetrofitNiaNetwork import com.google.samples.apps.nowinandroid.core.network.retrofit.RetrofitNiaNetwork
import dagger.Binds import dagger.Binds
import dagger.Module import dagger.Module
import dagger.Provides
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import kotlinx.serialization.json.Json
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
interface NetworkModule { interface FlavoredNetworkModule {
@Binds @Binds
fun bindsNiaNetwork( fun RetrofitNiaNetwork.binds(): NiaNetworkDataSource
niANetwork: RetrofitNiaNetwork
): NiaNetworkDataSource
companion object {
@Provides
@Singleton
fun providesNetworkJson(): Json = Json {
ignoreUnknownKeys = true
}
@Provides
@Singleton
fun providesFakeAssetManager(
@ApplicationContext context: Context,
): FakeAssetManager = FakeAssetManager(context.assets::open)
}
} }
Loading…
Cancel
Save