diff --git a/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/AppCompatImageViewExtended.kt b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/AppCompatImageViewExtended.kt new file mode 100644 index 0000000000..73fc59e133 --- /dev/null +++ b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/AppCompatImageViewExtended.kt @@ -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() + } + } +} \ No newline at end of file diff --git a/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/ExpoBlueskyGifViewModule.kt b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/ExpoBlueskyGifViewModule.kt index b6674a44ef..cf838a4a8d 100644 --- a/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/ExpoBlueskyGifViewModule.kt +++ b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/ExpoBlueskyGifViewModule.kt @@ -7,32 +7,9 @@ import expo.modules.kotlin.modules.ModuleDefinition import java.lang.ref.WeakReference class ExpoBlueskyGifViewModule : Module() { - companion object { - val visibleViews = mutableSetOf>() - } - 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 -> val activity = appContext.currentActivity ?: return@AsyncFunction val glide = Glide.with(activity) diff --git a/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/GifView.kt b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/GifView.kt index f704203e64..fde815bec1 100644 --- a/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/GifView.kt +++ b/modules/expo-bluesky-gif-view/android/src/main/java/expo/modules/blueskygifview/GifView.kt @@ -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 @@ -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? = null @@ -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() } @@ -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 } @@ -154,26 +148,14 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC // - 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() }