Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
fix #112
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoyongjun committed Jun 10, 2022
1 parent ab3cafd commit ba2f0c8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 36 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,4 @@ dependencies {
implementation(project(":coilimageloader"))
implementation(project(":PhotoViewImageViewLoader"))

debugImplementation("io.github.didi.dokit:dokitx:3.5.0-beta01")
releaseImplementation("io.github.didi.dokit:dokitx-no-op:3.5.0-beta01")
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:requestLegacyExternalStorage="true">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/net/mikaelzero/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class MainActivity : AppCompatActivity() {
}
}
binding.cacheBt.setOnClickListener {
imageLoader.memoryCache.clear()
CoilUtils.createDefaultCache(this).directory.delete()
imageLoader.memoryCache?.clear()
CoilImageLoader.with(applicationContext).cleanCache()
Glide.get(this).clearMemory()
Thread { Glide.get(this).clearDiskCache() }.start()
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Library.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object Library {
const val ART_PLAY_CORE = "org.salient.artvideoplayer:artplayer-core:1.1.0"
const val ART_PLAY_IJK = "org.salient.artvideoplayer:artplayer-ijk:1.1.0"
const val ART_PLAY_V7A = "org.salient.artvideoplayer:artplayer-armv7a:1.1.0"
const val COIL = "io.coil-kt:coil:1.3.0"
const val COIL = "io.coil-kt:coil:2.1.0"

// TEST

Expand Down
7 changes: 4 additions & 3 deletions coilimageloader/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ dependencies {
implementation(mojito.Library.ANDROIDX_APPCOMPAT)
implementation(mojito.Library.ANDROIDX_CORE)
implementation(mojito.Library.OKHTTP)
implementation("io.coil-kt:coil-base:1.3.0")
implementation("io.coil-kt:coil-gif:1.3.0")
implementation("io.coil-kt:coil-svg:1.3.0")
implementation(mojito.Library.COIL)
implementation("io.coil-kt:coil-base:2.1.0")
implementation("io.coil-kt:coil-gif:2.1.0")
implementation("io.coil-kt:coil-svg:2.1.0")
implementation(project(":mojito"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ import android.os.Build.VERSION.SDK_INT
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.decode.SvgDecoder
import coil.imageLoader
import coil.request.Disposable
import coil.request.ImageRequest
import coil.util.CoilUtils
import net.mikaelzero.mojito.loader.ImageLoader
import okhttp3.Cache
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient
import java.io.File
import java.util.*
import kotlin.collections.set


Expand All @@ -25,7 +21,7 @@ import kotlin.collections.set
* @CreateDate: 2021/7/23 11:07 上午
* @Description:
*/
class CoilImageLoader private constructor(val context: Context) : ImageLoader {
class CoilImageLoader private constructor(val context: Context) : net.mikaelzero.mojito.loader.ImageLoader {

private val SCHEMES = setOf(
ContentResolver.SCHEME_FILE,
Expand All @@ -36,17 +32,16 @@ class CoilImageLoader private constructor(val context: Context) : ImageLoader {
private fun getImageLoader(context: Context): coil.ImageLoader {
return coil.ImageLoader.Builder(context)
.crossfade(true)
.componentRegistry {
.components {
if (SDK_INT >= 28) {
add(ImageDecoderDecoder(context))
add(ImageDecoderDecoder.Factory())
} else {
add(GifDecoder())
add(GifDecoder.Factory())
}
add(SvgDecoder(context))
add(SvgDecoder.Factory())
}
.okHttpClient {
OkHttpClient.Builder()
.cache(CoilUtils.createDefaultCache(context))
.addInterceptor { chain ->
val request = chain.request()
val response = chain.proceed(request)
Expand All @@ -66,7 +61,7 @@ class CoilImageLoader private constructor(val context: Context) : ImageLoader {

private val mFlyingRequestTargets: MutableMap<Int, Disposable> = HashMap(3)

override fun loadImage(requestId: Int, uri: Uri?, onlyRetrieveFromCache: Boolean, callback: ImageLoader.Callback?) {
override fun loadImage(requestId: Int, uri: Uri?, onlyRetrieveFromCache: Boolean, callback: net.mikaelzero.mojito.loader.ImageLoader.Callback?) {
val localCache = uri.toString().getCoilCacheFile()
if (onlyRetrieveFromCache && (localCache == null || !localCache.exists())) {
callback?.onFail(Exception(""))
Expand Down Expand Up @@ -109,14 +104,6 @@ class CoilImageLoader private constructor(val context: Context) : ImageLoader {
rememberTarget(requestId, imageLoader.enqueue(request))
}

private fun String?.toFile(): File? {
if (this == null) {
return null
}
val f = File(this)
return if (f.exists()) f else null
}

fun Uri?.getCoilCacheFile(): File? {
if (SCHEMES.contains(this?.scheme)) {
if (this?.path == null) {
Expand All @@ -129,9 +116,7 @@ class CoilImageLoader private constructor(val context: Context) : ImageLoader {
}

fun String?.getCoilCacheFile(): File? {
return this?.toFile() ?: this?.toHttpUrlOrNull()?.let { u ->
CoilUtils.createDefaultCache(context).directory.listFiles()?.lastOrNull { it.name.endsWith(".1") && it.name.contains(Cache.key(u)) }
}
return context.imageLoader.diskCache?.get(this.orEmpty())?.data?.toFile()
}

override fun prefetch(uri: Uri?) {
Expand Down Expand Up @@ -164,8 +149,8 @@ class CoilImageLoader private constructor(val context: Context) : ImageLoader {
}

override fun cleanCache() {
getImageLoader(context).memoryCache.clear()
CoilUtils.createDefaultCache(context).directory.delete()
getImageLoader(context).memoryCache?.clear()
context.imageLoader.diskCache?.clear()
}


Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android.enableJetifier=true
kotlin.code.style=official
# Config
minSdk=16
targetSdk=30
compileSdk=30
targetSdk=31
compileSdk=31
# Maven
GROUP=com.github.mikaelzero
VERSION_NAME=1.8.3

0 comments on commit ba2f0c8

Please sign in to comment.