Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

34 error event and success event #36

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions android/src/main/java/com/turboimage/TurboImageListener.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.turboimage

import android.widget.ImageView
import coil.request.ErrorResult
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.events.RCTEventEmitter

open class TurboImageListener<T>(private val view: T) : ImageRequest.Listener where T : ImageView {
private val currentContext = view.context as ThemedReactContext
private var eventEmitter: RCTEventEmitter = (view.context as ThemedReactContext).getJSModule(
RCTEventEmitter::class.java
)

fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> {
return MapBuilder.of(
ON_ERROR, MapBuilder.of("registrationName", ON_ERROR),
ON_SUCCESS, MapBuilder.of("registrationName", ON_SUCCESS)
)
}

override fun onError(request: ImageRequest, result: ErrorResult) {

val payload = WritableNativeMap().apply {
putString("error", result.toString())
}
eventEmitter.receiveEvent(view.id, ON_ERROR, payload)
}

override fun onSuccess(request: ImageRequest, result: SuccessResult) {
val payload = WritableNativeMap().apply {
putBoolean("isSampled", result.isSampled)
putString("dataSource", result.dataSource.toString())
}
eventEmitter.receiveEvent(view.id, ON_SUCCESS, payload)
}

companion object {
const val ON_ERROR = "onError"
const val ON_SUCCESS = "onSuccess"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract class TurboImageViewManagerBase<T> : SimpleViewManager<T>() where T : I
override fun createViewInstance(p0: ThemedReactContext): T {
val instance = getImageView(p0)
requestBuilder = ImageRequest.Builder(p0).target { instance.setImageDrawable(it) }
.listener(object : TurboImageListener<T>(instance) {})
return instance
}

Expand Down
16 changes: 13 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import React from 'react';
import { SafeAreaView, ScrollView, StyleSheet, Text } from 'react-native';
import TurboImage from 'react-native-turbo-image';
import { base64Placeholder, images } from './mockData';
import { base64Placeholder } from './mockData';

export default function App() {
const imageURLs = Array.from(
{ length: 100 },
(_, i) => `https://placedog.net/300/200?id=${i}`
);
const handleOnSuccess = () => {};
const handleOnError = (error: any) => {
console.log(`🐵 ------ error`, error);
};
return (
<SafeAreaView style={styles.container}>
<ScrollView
style={styles.container}
contentContainerStyle={styles.contentContainer}
>
<Text>Turbo Image</Text>
{images.map((img, idx) => (
{imageURLs.map((url, idx) => (
<TurboImage
key={idx}
url={img.url}
url={url}
style={styles.box}
resizeMode="contain"
showActivityIndicator
fadeDuration={10}
base64Placeholder={base64Placeholder}
onSuccess={handleOnSuccess}
onError={handleOnError}
/>
))}
</ScrollView>
Expand Down
41 changes: 1 addition & 40 deletions example/src/mockData.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions ios/TurboImage/TurboImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ fileprivate extension TurboImageView {
KF.url(url)
.fade(duration: TimeInterval(truncating: fadeDuration))
.placeholder(UIImage(base64Placeholder: base64Placeholder))
.onSuccess({ result in
self.onSuccess?(["result": result])
.onSuccess({ _ in
self.onSuccess?(["result": "success"])
})
.onFailure({ error in
self.onError?(["error": error.localizedDescription])
Expand Down
8 changes: 4 additions & 4 deletions ios/TurboImage/TurboImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ @interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(url, NSString)

RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onSuccess, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString)

RCT_EXPORT_VIEW_PROPERTY(showActivityIndicator, BOOL)
Expand All @@ -16,5 +12,9 @@ @interface RCT_EXTERN_MODULE(TurboImageViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(fadeDuration, NSNumber)

RCT_EXPORT_VIEW_PROPERTY(onSuccess, RCTDirectEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock)

@end

10 changes: 2 additions & 8 deletions src/TurboImage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ export interface TurboImageProps extends AccessibilityProps, ViewProps {
showActivityIndicator?: boolean;
base64Placeholder?: string;
fadeDuration?: number;
onError?: (result: { nativeEvent: { error: string } }) => void;
onSuccess?: (result: {
nativeEvent: {
width: number;
height: number;
source: string;
};
}) => void;
onSuccess?: () => void;
onError?: (error: any) => void;
ref?: React.Ref<any>;
style?: StyleProp<ImageStyle>;
testID?: string;
Expand Down