Skip to content

Commit

Permalink
fix gifs animating on app resume android
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyok committed Apr 22, 2024
1 parent 18ba143 commit 15cdc8f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package expo.modules.blueskygifview

import android.content.Context
import android.graphics.Canvas
import android.graphics.drawable.Animatable
import android.util.Log
import androidx.appcompat.widget.AppCompatImageView

class AppCompatImageViewExtended(context: Context, val parent: GifView): AppCompatImageView(context) {
override fun onDraw(canvas: Canvas) {
Log.d("AppCompatImageViewExtended", "onDraw")
super.onDraw(canvas)
if (!parent.isPlaying) {
this.pause()
}
}

fun pause() {
val drawable = this.drawable
if (drawable is Animatable) {
drawable.stop()
}
}

fun play() {
val drawable = this.drawable
if (drawable is Animatable) {
drawable.start()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,9 @@ import expo.modules.kotlin.modules.ModuleDefinition
import java.lang.ref.WeakReference

class ExpoBlueskyGifViewModule : Module() {
companion object {
val visibleViews = mutableSetOf<WeakReference<GifView>>()
}

override fun definition() = ModuleDefinition {
Name("ExpoBlueskyGifView")

OnActivityEntersForeground {
// Activities will start again after entering the foreground, so we want to pause them
// if they are not playing.
visibleViews.forEach {
val view = it.get() ?: return@forEach
if (!view.isPlaying) {
view.setIsAnimating(false)
}
}

val activity = appContext.currentActivity ?: return@OnActivityEntersForeground
Glide.with(activity).resumeRequests()
}

OnActivityEntersBackground {
val activity = appContext.currentActivity ?: return@OnActivityEntersBackground
Glide.with(activity).pauseRequests()
}

AsyncFunction("prefetchAsync") { sources: List<String> ->
val activity = appContext.currentActivity ?: return@AsyncFunction
val glide = Glide.with(activity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package expo.modules.blueskygifview


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
Expand All @@ -25,7 +26,7 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC
// Glide
private val activity = appContext.currentActivity ?: throw Exceptions.MissingActivity()
private val glide: RequestManager = Glide.with(activity)
private val imageView = AppCompatImageView(context)
val imageView = AppCompatImageViewExtended(context, this)

// Requests
private var placeholderRequest: Target<Drawable>? = null
Expand Down Expand Up @@ -62,15 +63,13 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC
if (this.imageView.drawable == null || this.imageView.drawable !is Animatable) {
this.load()
} else if (this.isPlaying) {
this.setIsAnimating(true)
this.imageView.play()
}
ExpoBlueskyGifViewModule.visibleViews.add(WeakReference(this))
super.onAttachedToWindow()
}

override fun onDetachedFromWindow() {
this.setIsAnimating(false)
ExpoBlueskyGifViewModule.visibleViews.remove(WeakReference(this))
this.imageView.pause()
super.onDetachedFromWindow()
}

Expand All @@ -97,11 +96,6 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC
if (placeholderRequest != null) {
glide.clear(placeholderRequest)
}

// Glide always autoplays the animations, so if we have autoplay disabled let's stop it
if (resource is Animatable && !autoplay) {
resource.stop()
}
return false
}

Expand Down Expand Up @@ -154,26 +148,14 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC

//<editor-fold desc="Controls">

fun setIsAnimating(isAnimating: Boolean) {
val drawable = this.imageView.drawable

if (drawable is Animatable) {
if (isAnimating) {
drawable.start()
} else {
drawable.stop()
}
}
}

fun play() {
this.setIsAnimating(true)
this.imageView.play()
this.isPlaying = true
this.firePlayerStateChange()
}

fun pause() {
this.setIsAnimating(false)
this.imageView.pause()
this.isPlaying = false
this.firePlayerStateChange()
}
Expand Down

0 comments on commit 15cdc8f

Please sign in to comment.