diff options
Diffstat (limited to 'modules/expo-bluesky-gif-view/android')
3 files changed, 118 insertions, 109 deletions
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 index 5d2084845..5ba5ccf71 100644 --- 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 @@ -5,7 +5,10 @@ import android.graphics.Canvas import android.graphics.drawable.Animatable import androidx.appcompat.widget.AppCompatImageView -class AppCompatImageViewExtended(context: Context, private val parent: GifView): AppCompatImageView(context) { +class AppCompatImageViewExtended( + context: Context, + private val parent: GifView, +) : AppCompatImageView(context) { override fun onDraw(canvas: Canvas) { super.onDraw(canvas) @@ -34,4 +37,4 @@ class AppCompatImageViewExtended(context: Context, private val parent: GifView): 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 625e1d45f..b0b3e8eeb 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 @@ -6,49 +6,50 @@ import expo.modules.kotlin.modules.Module import expo.modules.kotlin.modules.ModuleDefinition class ExpoBlueskyGifViewModule : Module() { - override fun definition() = ModuleDefinition { - Name("ExpoBlueskyGifView") - - AsyncFunction("prefetchAsync") { sources: List<String> -> - val activity = appContext.currentActivity ?: return@AsyncFunction - val glide = Glide.with(activity) - - sources.forEach { source -> - glide - .download(source) - .diskCacheStrategy(DiskCacheStrategy.DATA) - .submit() + override fun definition() = + ModuleDefinition { + Name("ExpoBlueskyGifView") + + AsyncFunction("prefetchAsync") { sources: List<String> -> + val activity = appContext.currentActivity ?: return@AsyncFunction + val glide = Glide.with(activity) + + sources.forEach { source -> + glide + .download(source) + .diskCacheStrategy(DiskCacheStrategy.DATA) + .submit() + } } - } - View(GifView::class) { - Events( - "onPlayerStateChange" - ) + View(GifView::class) { + Events( + "onPlayerStateChange", + ) - Prop("source") { view: GifView, source: String -> - view.source = source - } + Prop("source") { view: GifView, source: String -> + view.source = source + } - Prop("placeholderSource") { view: GifView, source: String -> - view.placeholderSource = source - } + Prop("placeholderSource") { view: GifView, source: String -> + view.placeholderSource = source + } - Prop("autoplay") { view: GifView, autoplay: Boolean -> - view.autoplay = autoplay - } + Prop("autoplay") { view: GifView, autoplay: Boolean -> + view.autoplay = autoplay + } - AsyncFunction("playAsync") { view: GifView -> - view.play() - } + AsyncFunction("playAsync") { view: GifView -> + view.play() + } - AsyncFunction("pauseAsync") { view: GifView -> - view.pause() - } + AsyncFunction("pauseAsync") { view: GifView -> + view.pause() + } - AsyncFunction("toggleAsync") { view: GifView -> - view.toggle() + AsyncFunction("toggleAsync") { view: GifView -> + view.toggle() + } } } - } } 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 be5830df7..5e467a160 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 @@ -1,6 +1,5 @@ package expo.modules.blueskygifview - import android.content.Context import android.graphics.Color import android.graphics.drawable.Animatable @@ -15,7 +14,10 @@ import expo.modules.kotlin.exception.Exceptions import expo.modules.kotlin.viewevent.EventDispatcher import expo.modules.kotlin.views.ExpoView -class GifView(context: Context, appContext: AppContext) : ExpoView(context, appContext) { +class GifView( + context: Context, + appContext: AppContext, +) : ExpoView(context, appContext) { // Events private val onPlayerStateChange by EventDispatcher() @@ -44,8 +46,7 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC } } - - //<editor-fold desc="Lifecycle"> + // <editor-fold desc="Lifecycle"> init { this.setBackgroundColor(Color.TRANSPARENT) @@ -70,80 +71,82 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC super.onDetachedFromWindow() } - //</editor-fold> + // </editor-fold> - //<editor-fold desc="Loading"> + // <editor-fold desc="Loading"> private fun load() { if (placeholderSource == null || source == null) { return } - this.webpRequest = glide.load(source) - .diskCacheStrategy(DiskCacheStrategy.DATA) - .skipMemoryCache(false) - .listener(object: RequestListener<Drawable> { - override fun onResourceReady( - resource: Drawable?, - model: Any?, - target: Target<Drawable>?, - dataSource: com.bumptech.glide.load.DataSource?, - isFirstResource: Boolean - ): Boolean { - if (placeholderRequest != null) { - glide.clear(placeholderRequest) - } - return false - } - - override fun onLoadFailed( - e: GlideException?, - model: Any?, - target: Target<Drawable>?, - isFirstResource: Boolean - ): Boolean { - return true - } - }) - .into(this.imageView) - - if (this.imageView.drawable == null || this.imageView.drawable !is Animatable) { - this.placeholderRequest = glide.load(placeholderSource) + this.webpRequest = + glide + .load(source) .diskCacheStrategy(DiskCacheStrategy.DATA) - // Let's not bloat the memory cache with placeholders - .skipMemoryCache(true) - .listener(object: RequestListener<Drawable> { - override fun onResourceReady( - resource: Drawable?, - model: Any?, - target: Target<Drawable>?, - dataSource: com.bumptech.glide.load.DataSource?, - isFirstResource: Boolean - ): Boolean { - // Incase this request finishes after the webp, let's just not set - // the drawable. This shouldn't happen because the request should get cancelled - if (imageView.drawable == null) { - imageView.setImageDrawable(resource) + .skipMemoryCache(false) + .listener( + object : RequestListener<Drawable> { + override fun onResourceReady( + resource: Drawable?, + model: Any?, + target: Target<Drawable>?, + dataSource: com.bumptech.glide.load.DataSource?, + isFirstResource: Boolean, + ): Boolean { + if (placeholderRequest != null) { + glide.clear(placeholderRequest) + } + return false } - return true - } - - override fun onLoadFailed( - e: GlideException?, - model: Any?, - target: Target<Drawable>?, - isFirstResource: Boolean - ): Boolean { - return true - } - }) - .submit() + + override fun onLoadFailed( + e: GlideException?, + model: Any?, + target: Target<Drawable>?, + isFirstResource: Boolean, + ): Boolean = true + }, + ).into(this.imageView) + + if (this.imageView.drawable == null || this.imageView.drawable !is Animatable) { + this.placeholderRequest = + glide + .load(placeholderSource) + .diskCacheStrategy(DiskCacheStrategy.DATA) + // Let's not bloat the memory cache with placeholders + .skipMemoryCache(true) + .listener( + object : RequestListener<Drawable> { + override fun onResourceReady( + resource: Drawable?, + model: Any?, + target: Target<Drawable>?, + dataSource: com.bumptech.glide.load.DataSource?, + isFirstResource: Boolean, + ): Boolean { + // Incase this request finishes after the webp, let's just not set + // the drawable. This shouldn't happen because the request should get cancelled + if (imageView.drawable == null) { + imageView.setImageDrawable(resource) + } + return true + } + + override fun onLoadFailed( + e: GlideException?, + model: Any?, + target: Target<Drawable>?, + isFirstResource: Boolean, + ): Boolean = true + }, + ).submit() } } - //</editor-fold> + // </editor-fold> - //<editor-fold desc="Controls"> + // <editor-fold desc="Controls"> fun play() { this.imageView.play() @@ -165,16 +168,18 @@ class GifView(context: Context, appContext: AppContext) : ExpoView(context, appC } } - //</editor-fold> + // </editor-fold> - //<editor-fold desc="Util"> + // <editor-fold desc="Util"> fun firePlayerStateChange() { - onPlayerStateChange(mapOf( - "isPlaying" to this.isPlaying, - "isLoaded" to this.isLoaded, - )) + onPlayerStateChange( + mapOf( + "isPlaying" to this.isPlaying, + "isLoaded" to this.isLoaded, + ), + ) } - //</editor-fold> + // </editor-fold> } |