Implement network module as the multiplatform module

pull/1323/head
lihenggui 2 years ago
parent 6d8b2f8f12
commit 4dc4ba670a

@ -14,6 +14,8 @@
* limitations under the License.
*/
import com.codingfeline.buildkonfig.compiler.FieldSpec.Type.STRING
plugins {
alias(libs.plugins.nowinandroid.kmp.library)
alias(libs.plugins.nowinandroid.android.library.jacoco)
@ -38,7 +40,7 @@ secrets {
defaultPropertiesFileName = "secrets.defaults.properties"
}
buildKonfig {
buildkonfig {
packageName = "com.google.samples.apps.nowinandroid.core.network"
defaultConfigs {
buildConfigField(STRING, "BACKEND_URL", "\"https://www.example.com\"")

@ -4761,4 +4761,4 @@ internal val NEWS_DATA = """
]
}
]
""".trimIndent()
""".trimIndent()

@ -1,25 +0,0 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.di
import coil3.disk.DiskCache
import me.tatarka.inject.annotations.Provides
expect class DiskCacheComponent {
@Provides
internal fun newDiskCache(): DiskCache?
}

@ -18,7 +18,6 @@ package com.google.samples.apps.nowinandroid.core.network.di
import coil3.ImageLoader
import coil3.PlatformContext
import coil3.disk.DiskCache
import coil3.memory.MemoryCache
import coil3.request.crossfade
import coil3.util.DebugLogger
@ -37,7 +36,6 @@ abstract class ImageLoaderComponent {
@Provides
fun provideImageLoader(
context: PlatformContext,
diskCache: DiskCache?,
debug: Boolean,
): ImageLoader {
return ImageLoader.Builder(context)
@ -47,9 +45,6 @@ abstract class ImageLoaderComponent {
.maxSizePercent(context, percent = 0.25)
.build()
}
.diskCache {
diskCache
}
// Show a short crossfade when loading images asynchronously.
.crossfade(true)
// Enable logging if this is a debug build.

@ -16,6 +16,7 @@
package com.google.samples.apps.nowinandroid.core.network.di
import com.google.samples.apps.nowinandroid.core.network.BuildKonfig
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.converter.builtin.CallConverterFactory
import de.jensklingenberg.ktorfit.converter.builtin.FlowConverterFactory
@ -37,7 +38,7 @@ internal abstract class NetworkModule {
@Provides
fun provideKtorfit(json: Json): Ktorfit = ktorfit {
baseUrl(BuildConfig.BACKEND_URL)
baseUrl(BuildKonfig.BACKEND_URL)
httpClient(
HttpClient {
install(ContentNegotiation) {

@ -18,6 +18,8 @@ package com.google.samples.apps.nowinandroid.core.network.fake
import com.google.samples.apps.nowinandroid.core.di.IODispatcher
import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource
import com.google.samples.apps.nowinandroid.core.network.assets.NEWS_DATA
import com.google.samples.apps.nowinandroid.core.network.assets.TOPICS_DATA
import com.google.samples.apps.nowinandroid.core.network.model.NetworkChangeList
import com.google.samples.apps.nowinandroid.core.network.model.NetworkNewsResource
import com.google.samples.apps.nowinandroid.core.network.model.NetworkTopic
@ -37,13 +39,13 @@ class FakeNiaNetworkDataSource @Inject constructor(
@OptIn(ExperimentalSerializationApi::class)
override suspend fun getTopics(ids: List<String>?): List<NetworkTopic> =
withContext(ioDispatcher) {
networkJson.decodeFromString(TOPICS_ASSET)
networkJson.decodeFromString(TOPICS_DATA)
}
@OptIn(ExperimentalSerializationApi::class)
override suspend fun getNewsResources(ids: List<String>?): List<NetworkNewsResource> =
withContext(ioDispatcher) {
networkJson.decodeFromString(NEWS_ASSET)
networkJson.decodeFromString(NEWS_DATA)
}
override suspend fun getTopicChangeList(after: Int?): List<NetworkChangeList> =
@ -51,11 +53,6 @@ class FakeNiaNetworkDataSource @Inject constructor(
override suspend fun getNewsResourceChangeList(after: Int?): List<NetworkChangeList> =
getNewsResources().mapToChangeList(NetworkNewsResource::id)
companion object {
private const val NEWS_ASSET = "news.json"
private const val TOPICS_ASSET = "topics.json"
}
}
/**

@ -29,7 +29,7 @@ import me.tatarka.inject.annotations.Inject
/**
* Retrofit API declaration for NIA Network API
*/
private interface RetrofitNiaNetworkApi {
internal interface RetrofitNiaNetworkApi {
@GET(value = "topics")
suspend fun getTopics(
@Query("id") ids: List<String>?,
@ -55,7 +55,7 @@ private interface RetrofitNiaNetworkApi {
* Wrapper for data provided from the [NIA_BASE_URL]
*/
@Serializable
private data class NetworkResponse<T>(
internal data class NetworkResponse<T>(
val data: T,
)

@ -24,9 +24,8 @@ import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.serialization.json.Json
import org.junit.Before
import org.junit.Test
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
class FakeNiaNetworkDataSourceTest {

@ -1,32 +0,0 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.di
import com.google.samples.apps.nowinandroid.core.network.NiaNetworkDataSource
import com.google.samples.apps.nowinandroid.core.network.retrofit.RetrofitNiaNetwork
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Module
@InstallIn(SingletonComponent::class)
internal interface FlavoredNetworkModule {
@Binds
fun binds(impl: RetrofitNiaNetwork): NiaNetworkDataSource
}

@ -75,7 +75,7 @@ sqldelight = "2.0.1"
kotlinInject = '0.6.3'
multiplatform-settings = "1.1.1"
kermit = "2.0.3"
ktor = "3.0.0-beta-1"
ktor = "2.3.9"
ktrofit = "1.12.0"
buildKonfig = "0.15.1"

Loading…
Cancel
Save