Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
#73: Replace Glide with Coil (#115)
Browse files Browse the repository at this point in the history
And bump compile sdk version
  • Loading branch information
mdrlzy authored Nov 15, 2023
1 parent 037ac27 commit d837ae6
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 115 deletions.
15 changes: 9 additions & 6 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ sonarqube {
}
}
android {
compileSdkVersion 32
compileSdkVersion 34
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion 26
targetSdkVersion 31
targetSdkVersion 34
}

buildTypes {
Expand Down Expand Up @@ -73,10 +73,13 @@ dependencies {
implementation "com.google.dagger:dagger:2.41"
implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'
implementation 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
implementation 'com.github.MikeOrtiz:TouchImageView:3.1.1'
implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.11.0"


def coilVersion = "2.4.0"
implementation "io.coil-kt:coil:$coilVersion"
implementation "io.coil-kt:coil-gif:$coilVersion"
implementation "io.coil-kt:coil-svg:$coilVersion"
implementation "io.coil-kt:coil-video:$coilVersion"

testImplementation "junit:junit:4.13.2"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
testImplementation "io.mockk:mockk:1.13.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object ImageMetadataGenerator: MetadataGenerator {
get() = setOf("bmp", "gif", "ico", "jpg", "jpeg",
"png", "tif", "tiff",
"webp", "heic", "heif",
"avif")
"avif", "svg")

override val acceptedMimeTypes: Set<String>
get() = setOf(
Expand All @@ -24,7 +24,8 @@ object ImageMetadataGenerator: MetadataGenerator {
"image/tiff",
"image/webp",
"image/heic",
"image/avif"
"image/avif",
"image/svg+xml"
)

override fun generate(path: Path, resource: Resource): Result<Metadata> =
Expand Down
52 changes: 31 additions & 21 deletions lib/src/main/java/dev/arkbuilders/arklib/data/preview/Preview.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package dev.arkbuilders.arklib.data.preview

import android.graphics.Bitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.DownsampleStrategy
import com.bumptech.glide.request.RequestOptions
import android.util.Log
import androidx.core.graphics.drawable.toBitmap
import coil.ImageLoader
import coil.request.ImageRequest
import coil.size.Precision
import coil.size.Scale
import kotlinx.coroutines.Job
import dev.arkbuilders.arklib.*
import dev.arkbuilders.arklib.app
Expand All @@ -29,27 +30,35 @@ data class Preview(

// we don't have fullscreen preview for
// some kinds of resources, e.g. Image or PlainText
val onlyThumbnail: Boolean) {
val onlyThumbnail: Boolean
) {

internal companion object {
const val THUMBNAIL_SIZE = 128
const val COMPRESSION_QUALITY = 100

fun downscale(bitmap: Bitmap): Bitmap =
downscale(Glide.with(app).asBitmap().load(bitmap))

fun downscale(builder: RequestBuilder<Bitmap>): Bitmap =
builder
.apply(RequestOptions()
.downsample(DownsampleStrategy.CENTER_INSIDE)
.override(THUMBNAIL_SIZE)
.fitCenter()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
/**
* See [ImageRequest.Builder.data] for supported data types
* Note: pass [java.io.File] instead of [java.nio.file.Path]
*/
suspend fun downscale(resource: Path, data: Any): Result<Bitmap> = runCatching {
val request = ImageRequest.Builder(app)
.size(THUMBNAIL_SIZE)
.precision(Precision.EXACT)
.scale(Scale.FIT)
.data(data)
.listener(
onError = { _, result ->
Log.d(
LOG_PREFIX,
"Failed to downscale preview for $resource because ${result.throwable}"
)
},
)
.addListener(ImageUtils.glideExceptionListener<Bitmap>())
.submit()
.get()
.build()

ImageUtils.arkImageLoader.execute(request).drawable!!.toBitmap()
}
}
}

Expand All @@ -67,7 +76,8 @@ class PreviewLocator(
private val previewsDir = root.arkFolder().arkPreviews()
private val thumbnailsDir = root.arkFolder().arkThumbnails()

fun fullscreen(): Path = processor.images[id] ?: previewsDir.resolve(id.toString())
fun fullscreen(): Path =
processor.images[id] ?: previewsDir.resolve(id.toString())

fun thumbnail(): Path = thumbnailsDir.resolve(id.toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ class RootPreviewProcessor private constructor(

previews.saveBitmap(id, it.bitmap)

val thumbnail = Preview.downscale(it.bitmap)
thumbnails.saveBitmap(id, thumbnail)
val thumbnailResult = Preview.downscale(path, it.bitmap)
thumbnailResult.onSuccess { thumbnail ->
thumbnails.saveBitmap(id, thumbnail)
}
}
.onFailure {
Log.w(LOG_PREFIX, "Failed to generate preview for $path")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.arkbuilders.arklib.data.preview.generator

import com.bumptech.glide.Glide
import dev.arkbuilders.arklib.app
import dev.arkbuilders.arklib.data.meta.Kind
import dev.arkbuilders.arklib.data.meta.Metadata
import dev.arkbuilders.arklib.data.preview.Preview
Expand All @@ -15,10 +13,13 @@ object ImagePreviewGenerator: PreviewGenerator {
}

override suspend fun generate(path: Path, meta: Metadata): Result<Preview> {
val bitmap = Preview.downscale(
Glide.with(app).asBitmap().load(path.toFile())
val thumbnailResult = Preview.downscale(
path,
path.toFile()
)

return Result.success(Preview(bitmap, onlyThumbnail = true))
return thumbnailResult.map { thumbnail ->
Preview(thumbnail, onlyThumbnail = true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import androidx.core.graphics.drawable.toBitmap
import coil.request.ImageRequest
import coil.size.Precision
import coil.size.Scale
import dev.arkbuilders.arklib.app
import dev.arkbuilders.arklib.data.meta.Kind
import dev.arkbuilders.arklib.data.meta.Metadata
import dev.arkbuilders.arklib.data.preview.Preview
import dev.arkbuilders.arklib.data.preview.PreviewGenerator
import dev.arkbuilders.arklib.utils.ImageUtils
import java.nio.file.Path
import kotlin.io.path.name
import wseemann.media.FFmpegMediaMetadataRetriever
Expand All @@ -26,7 +29,10 @@ object VideoPreviewGenerator : PreviewGenerator {
Preview(it, onlyThumbnail = false)
}

private fun generateBitmap(path: Path, durationMillis: Long?): Result<Bitmap> {
private suspend fun generateBitmap(
path: Path,
durationMillis: Long?
): Result<Bitmap> {
val timeMicros = (durationMillis ?: 10000) / 1000 / 2

val retriever = FFmpegMediaMetadataRetriever()
Expand All @@ -36,7 +42,7 @@ object VideoPreviewGenerator : PreviewGenerator {
// Trying 3 ways to get preview image for video.
// 1. using FFmpegMediaMetadataRetriever
// 2. using MediaMetadataRetriever
// 3. using Glide
// 3. using Coil
val mainBitmap = retriever.frameAtTime ?: let {
MediaMetadataRetriever().let { mediaMetadataRetriever ->
try {
Expand All @@ -61,10 +67,13 @@ object VideoPreviewGenerator : PreviewGenerator {
}
}
bitmap
} ?: Glide.with(app.baseContext).asBitmap()
.load(path.toFile())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.submit().get()
} ?: let {
val request = ImageRequest.Builder(app)
.data(path.toFile())
.build()

ImageUtils.arkImageLoader.execute(request).drawable!!.toBitmap()
}
}

if (mainBitmap != null) {
Expand Down
Loading

0 comments on commit d837ae6

Please sign in to comment.