parent
2a5672f6cd
commit
bd2059ba88
@ -1,42 +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.
|
||||
*/
|
||||
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import com.google.samples.apps.nowinandroid.core.network.fake.FakeAssetManager
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.util.Properties
|
||||
|
||||
/**
|
||||
* This class helps with loading Android `/assets` files, especially when running JVM unit tests.
|
||||
* It must remain on the root package for an easier [Class.getResource] with relative paths.
|
||||
* @see <a href="https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/UnitTestOptions">UnitTestOptions</a>
|
||||
*/
|
||||
@VisibleForTesting
|
||||
internal object JvmUnitTestFakeAssetManager : FakeAssetManager {
|
||||
private val config =
|
||||
requireNotNull(javaClass.getResource("com/android/tools/test_config.properties")) {
|
||||
"""
|
||||
Missing Android resources properties file.
|
||||
Did you forget to enable the feature in the gradle build file?
|
||||
android.testOptions.unitTests.isIncludeAndroidResources = true
|
||||
""".trimIndent()
|
||||
}
|
||||
private val properties = Properties().apply { config.openStream().use(::load) }
|
||||
private val assets = File(properties["android_merged_assets"].toString())
|
||||
|
||||
override fun open(fileName: String): InputStream = File(assets, fileName).inputStream()
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.ImageLoader
|
||||
import coil3.PlatformContext
|
||||
import coil3.disk.DiskCache
|
||||
import coil3.memory.MemoryCache
|
||||
import coil3.request.crossfade
|
||||
import coil3.util.DebugLogger
|
||||
import me.tatarka.inject.annotations.Component
|
||||
import me.tatarka.inject.annotations.Provides
|
||||
|
||||
@Component
|
||||
abstract class ImageLoaderComponent {
|
||||
/**
|
||||
* Since we're displaying SVGs in the app, Coil needs an ImageLoader which supports this
|
||||
* format. During Coil's initialization it will call `applicationContext.newImageLoader()` to
|
||||
* obtain an ImageLoader.
|
||||
*
|
||||
* @see <a href="https://github.com/coil-kt/coil/blob/main/coil-singleton/src/main/java/coil/Coil.kt">Coil</a>
|
||||
*/
|
||||
@Provides
|
||||
fun provideImageLoader(
|
||||
context: PlatformContext,
|
||||
diskCache: DiskCache?,
|
||||
debug: Boolean,
|
||||
): ImageLoader {
|
||||
return ImageLoader.Builder(context)
|
||||
.memoryCache {
|
||||
MemoryCache.Builder()
|
||||
// Set the max size to 25% of the app's available memory.
|
||||
.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.
|
||||
.apply {
|
||||
if (debug) {
|
||||
logger(DebugLogger())
|
||||
}
|
||||
}
|
||||
.build()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue