Skip to content

Commit

Permalink
feat: apply patch to react native fast image
Browse files Browse the repository at this point in the history
  • Loading branch information
sola-breakroom committed Nov 15, 2024
1 parent b365ba0 commit fe3bb5f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public Headers getHeaders() {
}

public GlideUrl getGlideUrl() {
Uri uriVal = getUri();
if (Uri.EMPTY.equals(uriVal)) {
return null;
}
return new GlideUrl(getUri().toString(), getHeaders());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.views.imagehelper.ImageSource;
import androidx.annotation.Nullable;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.facebook.react.bridge.Promise;
import java.io.File;

class FastImageViewModule extends ReactContextBaseJavaModule {

private static final String REACT_CLASS = "FastImageView";
private static final String ERROR_LOAD_FAILED = "ERROR_LOAD_FAILED";

FastImageViewModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -86,4 +94,39 @@ public void clearDiskCache(Promise promise) {
Glide.get(activity.getApplicationContext()).clearDiskCache();
promise.resolve(null);
}

@ReactMethod
public void getCachePath(final ReadableMap source, final Promise promise) {
final Activity activity = getCurrentActivity();
if (activity == null) return;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source);
final GlideUrl glideUrl = imageSource.getGlideUrl();
if (glideUrl == null) {
promise.resolve(null);
return;
}
Glide
.with(activity.getApplicationContext())
.asFile()
.load(glideUrl)
.apply(FastImageViewConverter.getOptions(activity, imageSource, source))
.listener(new RequestListener<File>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) {
promise.reject(ERROR_LOAD_FAILED, e);
return false;
}
@Override
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) {
promise.resolve(resource.getAbsolutePath());
return false;
}
})
.submit();
}
});
}
}
18 changes: 18 additions & 0 deletions ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#import <SDWebImage/SDImageCache.h>
#import <SDWebImage/SDWebImagePrefetcher.h>
#import <SDWebImage/SDImageCache.h>


@implementation FFFastImageViewManager

Expand Down Expand Up @@ -49,4 +51,20 @@ - (FFFastImageView*)view {
}];
}

RCT_EXPORT_METHOD(getCachePath:(nonnull FFFastImageSource *)source
withResolver:(RCTPromiseResolveBlock)resolve
andRejecter:(RCTPromiseRejectBlock)reject)
{
SDWebImageManager *imageManager = [SDWebImageManager sharedManager];
NSString *key = [imageManager cacheKeyForURL:source.url];
BOOL isCached = [[SDImageCache sharedImageCache] diskImageDataExistsWithKey:key];
if(isCached) {
NSString *cachePath = [[SDImageCache sharedImageCache] cachePathForKey:key];
resolve(cachePath);
} else {
resolve([NSNull null]);
}
}


@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solabreakroomapp/react-native-fast-image",
"version": "8.6.4",
"version": "8.6.5",
"description": "🚩 FastImage, performant React Native image component.",
"keywords": [
"cache",
Expand Down
22 changes: 13 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import React, { forwardRef, memo } from 'react'
import {
View,
Image,
NativeModules,
requireNativeComponent,
StyleSheet,
AccessibilityProps,
ColorValue,
FlexStyle,
Image,
ImageRequireSource,
LayoutChangeEvent,
NativeModules,
Platform,
ShadowStyleIOS,
StyleProp,
StyleSheet,
TransformsStyle,
ImageRequireSource,
Platform,
AccessibilityProps,
View,
ViewProps,
ColorValue,
requireNativeComponent,
} from 'react-native'

export type ResizeMode = 'contain' | 'cover' | 'stretch' | 'center'
Expand Down Expand Up @@ -234,6 +234,7 @@ export interface FastImageStaticProperties {
preload: (sources: Source[]) => void
clearMemoryCache: () => Promise<void>
clearDiskCache: () => Promise<void>
getCachePath: (source: Source) => Promise<string>
}

const FastImage: React.ComponentType<FastImageProps> &
Expand All @@ -253,6 +254,9 @@ FastImage.clearMemoryCache = () =>

FastImage.clearDiskCache = () => NativeModules.FastImageView.clearDiskCache()

FastImage.getCachePath = (source: Source) =>
NativeModules.FastImageView.getCachePath(source);

const styles = StyleSheet.create({
imageContainer: {
overflow: 'hidden',
Expand Down

0 comments on commit fe3bb5f

Please sign in to comment.