mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 21:56:59 +05:30
refactor(di): move out Coil configuration to an AppPlugin
This commit is contained in:
parent
93db17e6d7
commit
157fe2c221
3 changed files with 42 additions and 24 deletions
|
@ -7,10 +7,6 @@
|
|||
package dev.msfjarvis.claw.android
|
||||
|
||||
import android.app.Application
|
||||
import coil.ImageLoader
|
||||
import coil.ImageLoaderFactory
|
||||
import coil.disk.DiskCache
|
||||
import coil.memory.MemoryCache
|
||||
import dev.msfjarvis.claw.core.injection.AppPlugin
|
||||
import dev.msfjarvis.claw.injection.Components
|
||||
import dev.msfjarvis.claw.injection.scopes.AppScope
|
||||
|
@ -19,7 +15,7 @@ import tangle.inject.TangleGraph
|
|||
import tangle.inject.TangleScope
|
||||
|
||||
@TangleScope(AppScope::class)
|
||||
class ClawApplication : Application(), ImageLoaderFactory {
|
||||
class ClawApplication : Application() {
|
||||
|
||||
@Inject lateinit var plugins: Set<@JvmSuppressWildcards AppPlugin>
|
||||
|
||||
|
@ -31,23 +27,4 @@ class ClawApplication : Application(), ImageLoaderFactory {
|
|||
TangleGraph.inject(this)
|
||||
plugins.forEach { plugin -> plugin.apply(this) }
|
||||
}
|
||||
|
||||
override fun newImageLoader(): ImageLoader {
|
||||
return ImageLoader.Builder(this)
|
||||
.memoryCache { MemoryCache.Builder(this).maxSizePercent(MEMORY_CACHE_RATIO).build() }
|
||||
.diskCache {
|
||||
DiskCache.Builder()
|
||||
.directory(cacheDir.resolve("image_cache"))
|
||||
.maxSizeBytes(DISK_CACHE_MAX_SIZE)
|
||||
.build()
|
||||
}
|
||||
// Show a short crossfade when loading images asynchronously.
|
||||
.crossfade(true)
|
||||
.build()
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private const val MEMORY_CACHE_RATIO = 0.25
|
||||
private const val DISK_CACHE_MAX_SIZE = 25L * 1024 * 1024 // 25 MB
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,13 +10,17 @@ plugins {
|
|||
kotlin("android")
|
||||
id("dev.msfjarvis.claw.kotlin-common")
|
||||
id("dev.msfjarvis.claw.android-library")
|
||||
alias(libs.plugins.anvil)
|
||||
}
|
||||
|
||||
anvil { generateDaggerFactories.set(true) }
|
||||
|
||||
androidComponents { beforeVariants { it.enableUnitTest = false } }
|
||||
|
||||
dependencies {
|
||||
implementation(platform(libs.androidx.compose.bom))
|
||||
api(libs.napier)
|
||||
implementation(projects.core)
|
||||
implementation(projects.database)
|
||||
implementation(projects.model)
|
||||
implementation(libs.accompanist.flowlayout)
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* 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.common.injection
|
||||
|
||||
import android.app.Application
|
||||
import coil.Coil
|
||||
import coil.ImageLoader
|
||||
import coil.disk.DiskCache
|
||||
import coil.memory.MemoryCache
|
||||
import dev.msfjarvis.claw.core.injection.AppPlugin
|
||||
|
||||
class CoilAppPlugin : AppPlugin {
|
||||
override fun apply(application: Application) {
|
||||
Coil.setImageLoader {
|
||||
ImageLoader.Builder(application)
|
||||
.memoryCache { MemoryCache.Builder(application).maxSizePercent(MEMORY_CACHE_RATIO).build() }
|
||||
.diskCache {
|
||||
DiskCache.Builder()
|
||||
.directory(application.cacheDir.resolve("image_cache"))
|
||||
.maxSizeBytes(DISK_CACHE_MAX_SIZE)
|
||||
.build()
|
||||
}
|
||||
// Show a short crossfade when loading images asynchronously.
|
||||
.crossfade(true)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private const val MEMORY_CACHE_RATIO = 0.25
|
||||
private const val DISK_CACHE_MAX_SIZE = 25L * 1024 * 1024 // 25 MB
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue