Skip to content
Permalink
Browse files

Migrate from Glide to Coil

  • Loading branch information...
ianhanniballake committed Aug 13, 2019
1 parent 6991827 commit 9a0ccd8d387171534c82bd385eb59d43361cd6d2
@@ -22,6 +22,7 @@ buildscript {
appCompatVersion = '1.1.0'
archCoreVersion = '2.1.0'
browserVersion = '1.0.0'
coilVersion = '0.7.0'
constraintLayoutVersion = '2.0.0-beta2'
coreVersion = '1.2.0-alpha04'
coroutinesVersion = '1.3.0'
@@ -31,7 +32,6 @@ buildscript {
firebaseAnalyticsVersion = '17.2.0'
firebasePerfVersion = '19.0.0'
fragmentVersion = '1.2.0-alpha03'
glideVersion = '4.9.0'
kotlinVersion = '1.3.50'
lifecycleVersion = '2.2.0-alpha04'
materialVersion = '1.1.0-alpha10'
@@ -106,7 +106,7 @@ dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutinesVersion"
implementation "androidx.multidex:multidex:$multidexVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion"
implementation "io.coil-kt:coil:$coilVersion"
implementation "com.google.android.gms:play-services-wearable:$playServicesWearableVersion"
implementation "com.twofortyfouram:android-plugin-api-for-locale:1.0.4"
implementation "com.davemorrissey.labs:subsampling-scale-image-view:3.10.0"
@@ -21,7 +21,6 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
@@ -50,11 +49,7 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import coil.api.load
import com.google.android.apps.muzei.api.provider.MuzeiArtProvider
import com.google.android.apps.muzei.api.provider.ProviderContract
import com.google.android.apps.muzei.legacy.LegacySourceManager
@@ -263,7 +258,7 @@ class ChooseProviderFragment : Fragment(R.layout.choose_provider_fragment) {
}
}

inner class ProviderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), RequestListener<Drawable> {
inner class ProviderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val providerIcon: ImageView = itemView.findViewById(R.id.provider_icon)
private val providerTitle: TextView = itemView.findViewById(R.id.provider_title)
private val providerSelected: ImageView = itemView.findViewById(R.id.provider_selected)
@@ -364,26 +359,6 @@ class ChooseProviderFragment : Fragment(R.layout.choose_provider_fragment) {
}
}

override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
providerArtwork.isVisible = true
return false
}

override fun onLoadFailed(e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
providerArtwork.isVisible = false
return false
}

fun setDescription(providerInfo: ProviderInfo) = providerInfo.run {
providerDescription.text = description
providerDescription.isGone = description.isNullOrEmpty()
@@ -392,10 +367,13 @@ class ChooseProviderFragment : Fragment(R.layout.choose_provider_fragment) {
fun setImage(providerInfo: ProviderInfo) = providerInfo.run {
providerArtwork.isVisible = currentArtworkUri != null
if (currentArtworkUri != null) {
Glide.with(this@ChooseProviderFragment)
.load(currentArtworkUri)
.addListener(this@ProviderViewHolder)
.into(providerArtwork)
providerArtwork.load(currentArtworkUri) {
lifecycle(viewLifecycleOwner)
listener(
onError = { _, _ -> providerArtwork.isVisible = false },
onSuccess = { _, _ -> providerArtwork.isVisible = true }
)
}
}
}

@@ -28,19 +28,19 @@ import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import coil.api.load
import com.google.android.apps.muzei.room.Artwork
import com.google.android.apps.muzei.room.MuzeiDatabase
import com.google.android.apps.muzei.util.toast
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.nurik.roman.muzei.R
@@ -89,19 +89,19 @@ class BrowseProviderFragment: Fragment(R.layout.browse_provider_fragment) {
}

class ArtViewHolder(
private val coroutineScope: CoroutineScope,
private val owner: LifecycleOwner,
itemView: View
): RecyclerView.ViewHolder(itemView) {
private val imageView = itemView.findViewById<ImageView>(R.id.browse_image)

fun bind(artwork: Artwork) {
imageView.contentDescription = artwork.title
Glide.with(imageView)
.load(artwork.imageUri)
.into(imageView)
imageView.load(artwork.imageUri) {
lifecycle(owner)
}
itemView.setOnClickListener {
val context = it.context
coroutineScope.launch(Dispatchers.Main) {
owner.lifecycleScope.launch(Dispatchers.Main) {
FirebaseAnalytics.getInstance(context).logEvent(
FirebaseAnalytics.Event.SELECT_CONTENT, bundleOf(
FirebaseAnalytics.Param.ITEM_ID to artwork.id,
@@ -131,7 +131,7 @@ class BrowseProviderFragment: Fragment(R.layout.browse_provider_fragment) {
}
) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
ArtViewHolder(viewLifecycleOwner.lifecycleScope,
ArtViewHolder(viewLifecycleOwner,
layoutInflater.inflate(R.layout.browse_provider_item, parent, false))

override fun onBindViewHolder(holder: ArtViewHolder, position: Int) {
@@ -30,15 +30,15 @@ import android.widget.ImageView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.lifecycle.observe
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import coil.api.load
import coil.bitmappool.BitmapPool
import coil.transform.Transformation
import com.google.android.apps.muzei.settings.EffectsLockScreenOpenLiveData
import com.google.android.apps.muzei.util.ImageBlurrer
import com.google.android.apps.muzei.util.blur
import com.google.android.apps.muzei.util.roundMult4
import java.security.MessageDigest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurRenderer.Callbacks {

@@ -48,7 +48,6 @@ class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurR
private const val ARG_DEMO_FOCUS = "demo_focus"

private const val TRANSFORMATION_ID = "simpleDemoModeTransformation"
private val TRANSFORMATION_BYTES = TRANSFORMATION_ID.toByteArray()

fun createInstance(demoMode: Boolean, demoFocus: Boolean = false): MuzeiRendererFragment {
return MuzeiRendererFragment().apply {
@@ -64,30 +63,24 @@ class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurR
private var demoMode: Boolean = false
private var demoFocus: Boolean = false

private val simpleDemoModeTransformation = object : BitmapTransformation() {
override fun transform(
private val simpleDemoModeTransformation = object : Transformation {
override suspend fun transform(
pool: BitmapPool,
bitmap: Bitmap,
outWidth: Int,
outHeight: Int
input: Bitmap
) = if (!demoFocus) {
bitmap.blur(requireContext())?.apply {
// Dim
val c = Canvas(this)
c.drawColor(Color.argb(255 - MuzeiBlurRenderer.DEFAULT_MAX_DIM,
0, 0, 0))
}
withContext(Dispatchers.IO) {
input.blur(requireContext())?.apply {
// Dim
val c = Canvas(this)
c.drawColor(Color.argb(255 - MuzeiBlurRenderer.DEFAULT_MAX_DIM,
0, 0, 0))
}
} ?: input
} else {
bitmap
input
}

override fun equals(other: Any?) = other === this

override fun hashCode() = TRANSFORMATION_ID.hashCode()

override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(TRANSFORMATION_BYTES)
}
override fun key() = TRANSFORMATION_ID
}

override fun onCreate(savedInstanceState: Bundle?) {
@@ -116,11 +109,11 @@ class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurR
simpleDemoModeImageView = ImageView(context).apply {
scaleType = ImageView.ScaleType.CENTER_CROP
}
Glide.with(this)
.load("file:///android_asset/starrynight.jpg")
.override(targetWidth, targetHeight)
.transform(CenterCrop(), simpleDemoModeTransformation)
.into(simpleDemoModeImageView)
simpleDemoModeImageView.load("file:///android_asset/starrynight.jpg") {
lifecycle(viewLifecycleOwner)
size(targetWidth, targetHeight)
transformations(simpleDemoModeTransformation)
}
return simpleDemoModeImageView
} else {
muzeiView = MuzeiView(requireContext())
@@ -25,7 +25,7 @@ dependencies {
implementation project(':muzei-api')
implementation project(':extensions')
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "com.github.bumptech.glide:glide:$glideVersion"
implementation "io.coil-kt:coil:$coilVersion"
implementation "androidx.core:core-ktx:$coreVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentVersion"
implementation "androidx.appcompat:appcompat:$appCompatVersion"
@@ -64,7 +64,7 @@ import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import coil.api.load
import com.google.android.apps.muzei.util.MultiSelectionController
import com.google.android.apps.muzei.util.getString
import com.google.android.apps.muzei.util.getStringOrNull
@@ -637,11 +637,10 @@ class GallerySettingsActivity : AppCompatActivity(), Observer<PagedList<ChosenPh
for (h in 0 until numImages) {
val thumbView = vh.thumbViews[h]
thumbView.visibility = View.VISIBLE
Glide.with(this@GallerySettingsActivity)
.load(images[h])
.override(targetSize)
.placeholder(placeholderDrawable)
.into(thumbView)
thumbView.load(images[h]) {
size(targetSize)
placeholder(placeholderDrawable)
}
}
for (h in numImages until maxImages) {
val thumbView = vh.thumbViews[h]

0 comments on commit 9a0ccd8

Please sign in to comment.
You can’t perform that action at this time.