Skip to content

Commit

Permalink
- Fix: Add error handling capability to GiphyMediaView.
Browse files Browse the repository at this point in the history
re #25
  • Loading branch information
ALexanderLonsky committed Aug 14, 2024
1 parent 4adb703 commit 3cdee1e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.util.Log
import android.view.View
import com.facebook.drawee.drawable.ScalingUtils
import com.giphy.giphy_flutter_sdk.dto.GiphyFlutterResizeMode
import com.giphy.giphy_flutter_sdk.dto.GiphyApiException
import com.giphy.giphy_flutter_sdk.dto.toMedia
import com.giphy.giphy_flutter_sdk.dto.toRenditionType
import com.giphy.giphy_flutter_sdk.utils.require
Expand Down Expand Up @@ -97,7 +98,13 @@ internal class GiphyFlutterMediaView(
loadedMedia = result?.data
syncMedia()
e?.let {
Log.d("Error while fetching GIF: %s", e.localizedMessage)
val exception = GiphyApiException(it)
val errorMessage = exception.errorMessage
channel.invokeMethod(
"onError", mapOf(
"error" to exception.errorMessage
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.giphy.giphy_flutter_sdk.dto

import com.giphy.sdk.core.network.response.ErrorResponse

class GiphyApiException(private val original: Throwable) : Exception(original) {

val errorResponse: ErrorResponse? by lazy {
try {
val errorResponseField = original::class.java.getDeclaredField("errorResponse")
errorResponseField.isAccessible = true
errorResponseField.get(original) as? ErrorResponse
} catch (e: Exception) {
null
}
}

val errorMessage: String
get() = errorResponse?.message ?: (errorResponse?.meta?.msg ?: "Unknown Error")
}
8 changes: 7 additions & 1 deletion example/lib/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,15 @@ class _MainScreenState extends State<MainScreen>
_mediaList[index].images.original!.gifUrl!),
)
: GiphyMediaView(
media: _mediaList[index],
mediaId: _mediaList[index].id,
autoPlay: true,
renditionType: GiphyRendition.fixedWidth,
onError: (error) {
setState(() {
// Display a crying emoji in case of an error
_mediaList[index] = GiphyMedia.fromJson({"id": "tEuZQV7AGrrv035n3c", "images": {}});
});
},
),
),
);
Expand Down
8 changes: 7 additions & 1 deletion ios/Classes/GiphyFlutterMediaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ public class GiphyFlutterMediaView: NSObject, FlutterPlatformView {
guard let self = self else {
return
}
self.media = response?.data
if let error = error {
channel?.invokeMethod("onError", arguments: [
"error": error.localizedDescription
])
} else {
self.media = response?.data
}
}
}

Expand Down
37 changes: 27 additions & 10 deletions lib/giphy_media_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ class GiphyMediaView extends StatefulWidget {
/// Enable/disable the checkered background for stickers and text media type.
final bool showCheckeredBackground;

/// A callback function that will be called when an error occurs whilst attempting to render media.
final Function(String description)? onError;

/// Constructs a GiphyMediaView.
const GiphyMediaView({
super.key,
this.controller,
this.mediaId,
this.media,
this.autoPlay = true,
this.renditionType = GiphyRendition.fixedWidth,
this.resizeMode = GiphyResizeMode.cover,
this.showCheckeredBackground = true,
});
const GiphyMediaView(
{super.key,
this.controller,
this.mediaId,
this.media,
this.autoPlay = true,
this.renditionType = GiphyRendition.fixedWidth,
this.resizeMode = GiphyResizeMode.cover,
this.showCheckeredBackground = true,
this.onError});

@override
State<GiphyMediaView> createState() => _GiphyMediaViewState();
Expand Down Expand Up @@ -125,10 +128,24 @@ class _GiphyMediaViewState extends State<GiphyMediaView> {
/// Sets up the method channel for communication with the native platform.
void _onPlatformViewCreated(int viewId) {
_channel = MethodChannel('com.giphyfluttersdk/mediaView$viewId');
_channel.setMethodCallHandler(_handleMethodCall);
_isPlatformViewCreated = true;
_updatePlatformView();
}

/// Handles method calls from the native platform.
///
/// This method processes callbacks for errors.
Future<dynamic> _handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'onError':
if (widget.onError != null) {
widget.onError!(call.arguments['error'] ?? "unknown");
}
break;
}
}

/// Updates the platform view with the current widget properties.
Future<void> _updatePlatformView() async {
if (widget.mediaId != null) {
Expand Down

0 comments on commit 3cdee1e

Please sign in to comment.