Skip to content

Commit

Permalink
feat: apply patch to react native fast image (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
crystal-phantom authored and derekchan916 committed Sep 9, 2024
1 parent c737c4f commit 59c7d0d
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 107 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 @@ -89,4 +97,43 @@ 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();
}
});
}
}
17 changes: 17 additions & 0 deletions ios/FastImage/FFFastImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

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

@implementation FFFastImageViewManager

Expand Down Expand Up @@ -49,4 +50,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
196 changes: 98 additions & 98 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
{
"name": "@derekchan916/react-native-fast-image",
"version": "8.6.10",
"description": "🚩 FastImage, performant React Native image component.",
"keywords": [
"cache",
"cached",
"fastimage",
"image",
"priority"
],
"homepage": "https://github.com/dream-sports-labs/react-native-fast-image#readme",
"bugs": {
"url": "https://github.com/dream-sports-labs/react-native-fast-image/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/dream-sports-labs/react-native-fast-image.git"
},
"license": "(MIT AND Apache-2.0)",
"author": "Dream Sports Labs <dreamsportslabs@dream11.com> (https://github.com/dream-sports-labs)",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"source": "src/index",
"typings": "dist/index.d.ts",
"files": [
"src",
"android",
"!android/build",
"ios",
"!ios/build",
"dist",
"RNFastImage.podspec"
],
"scripts": {
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"build": "yarn clean && tsc && rollup -c && cp src/index.js.flow dist/index.js.flow && cp src/index.js.flow dist/index.cjs.js.flow",
"example": "yarn workspace reactnativefastimageexample",
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
"prepack": "yarn build",
"test": "jest",
"type-check": "tsc --noEmit",
"release:nightly": "./publish-nightly.sh",
"changelog": "auto-changelog -p",
"release": "release-it --config .release-it.json"
},
"prettier": {
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all"
},
"resolutions": {
"@jest/create-cache-key-function": "^27"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/plugin-transform-private-methods": "^7.24.7",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.24.7",
"@react-native/babel-preset": "0.74.84",
"@react-native/eslint-config": "0.74.84",
"@react-native/metro-config": "0.74.84",
"@react-native/typescript-config": "0.74.84",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.5.1",
"@types/jest": "^26.0.24",
"@types/react": "^18.3.3",
"@types/react-native": "^0.69.5",
"@types/react-test-renderer": "^18.0.0",
"auto-changelog": "^2.4.0",
"babel-jest": "^29.6.3",
"babel-plugin-module-resolver": "^5.0.0",
"dv-scripts": "^1.6.0",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"metro-react-native-babel-preset": "^0.66.1",
"prettier": "^2.3.2",
"react": "18.2.0",
"react-native": "0.74.2",
"react-test-renderer": "^18.3.1",
"release-it": "^17.6.0",
"rollup": "^4.19.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^5.5.4"
},
"peerDependencies": {
"react": "*",
"react-native": "*"
},
"packageManager": "yarn@3.6.4"
"name": "@derekchan916/react-native-fast-image",
"version": "8.6.11",
"description": "🚩 FastImage, performant React Native image component.",
"keywords": [
"cache",
"cached",
"fastimage",
"image",
"priority"
],
"homepage": "https://github.com/dream-sports-labs/react-native-fast-image#readme",
"bugs": {
"url": "https://github.com/dream-sports-labs/react-native-fast-image/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/dream-sports-labs/react-native-fast-image.git"
},
"license": "(MIT AND Apache-2.0)",
"author": "Dream Sports Labs <dreamsportslabs@dream11.com> (https://github.com/dream-sports-labs)",
"main": "dist/index.cjs.js",
"module": "dist/index.js",
"source": "src/index",
"typings": "dist/index.d.ts",
"files": [
"src",
"android",
"!android/build",
"ios",
"!ios/build",
"dist",
"RNFastImage.podspec"
],
"scripts": {
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"build": "yarn clean && tsc && rollup -c && cp src/index.js.flow dist/index.js.flow && cp src/index.js.flow dist/index.cjs.js.flow",
"example": "yarn workspace reactnativefastimageexample",
"lint": "eslint 'src/**/*.{js,jsx,ts,tsx}'",
"prepack": "yarn build",
"test": "jest",
"type-check": "tsc --noEmit",
"release:nightly": "./publish-nightly.sh",
"changelog": "auto-changelog -p",
"release": "release-it --config .release-it.json"
},
"prettier": {
"semi": false,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all"
},
"resolutions": {
"@jest/create-cache-key-function": "^27"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/plugin-transform-private-methods": "^7.24.7",
"@babel/preset-env": "^7.20.0",
"@babel/runtime": "^7.24.7",
"@react-native/babel-preset": "0.74.84",
"@react-native/eslint-config": "0.74.84",
"@react-native/metro-config": "0.74.84",
"@react-native/typescript-config": "0.74.84",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@testing-library/jest-native": "^5.4.3",
"@testing-library/react-native": "^12.5.1",
"@types/jest": "^26.0.24",
"@types/react": "^18.3.3",
"@types/react-native": "^0.69.5",
"@types/react-test-renderer": "^18.0.0",
"auto-changelog": "^2.4.0",
"babel-jest": "^29.6.3",
"babel-plugin-module-resolver": "^5.0.0",
"dv-scripts": "^1.6.0",
"eslint": "^8.19.0",
"jest": "^29.6.3",
"metro-react-native-babel-preset": "^0.66.1",
"prettier": "^2.3.2",
"react": "18.2.0",
"react-native": "0.74.2",
"react-test-renderer": "^18.3.1",
"release-it": "^17.6.0",
"rollup": "^4.19.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^5.5.4"
},
"peerDependencies": {
"react": "*",
"react-native": "*"
},
"packageManager": "yarn@3.6.4"
}
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 @@ -243,6 +243,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 @@ -262,6 +263,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 59c7d0d

Please sign in to comment.