diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java index 0e3329a61..390ac9a83 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java @@ -84,6 +84,12 @@ public void setResizeMode(FastImageViewWithUrl view, String resizeMode) { view.setScaleType(scaleType); } + + @ReactProp(name = "useLastImageAsDefaultSource") + public void useLastImageAsDefaultSource(FastImageViewWithUrl view, @Nullable Boolean isActivated) { + view.useLastImageAsDefaultSource(isActivated); + } + @Override public void onDropViewInstance(@NonNull FastImageViewWithUrl view) { // This will cancel existing requests. diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java index bd144b6fa..ccff6bce8 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java @@ -1,4 +1,6 @@ package com.dylanvann.fastimage; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; import static com.dylanvann.fastimage.FastImageRequestListener.REACT_ON_ERROR_EVENT; @@ -28,6 +30,8 @@ import javax.annotation.Nonnull; class FastImageViewWithUrl extends AppCompatImageView { + private Boolean mUseLastImageAsDefaultSource = false; + private boolean mNeedsReload = false; private ReadableMap mSource = null; private Drawable mDefaultSource = null; @@ -38,16 +42,31 @@ public FastImageViewWithUrl(Context context) { super(context); } - public void setSource(@Nullable ReadableMap source) { - mNeedsReload = true; - mSource = source; +public void setSource(@Nullable ReadableMap source) { + mNeedsReload = true; + mSource = source; + + if (mUseLastImageAsDefaultSource) { + Drawable currentDrawable = this.getDrawable(); + if (currentDrawable instanceof BitmapDrawable) { + Bitmap bitmap = ((BitmapDrawable) currentDrawable).getBitmap(); + if (bitmap != null && !bitmap.isRecycled()) { + Bitmap defaultBitmap = bitmap.copy(bitmap.getConfig(), false); + setDefaultSource(new BitmapDrawable(getResources(), defaultBitmap)); + } + } } +} public void setDefaultSource(@Nullable Drawable source) { mNeedsReload = true; mDefaultSource = source; } + public void useLastImageAsDefaultSource(@Nullable Boolean isActivated) { + mUseLastImageAsDefaultSource = isActivated; + } + private boolean isNullOrEmpty(final String url) { return url == null || url.trim().isEmpty(); } diff --git a/ios/FastImage/FFFastImageView.h b/ios/FastImage/FFFastImageView.h index e52fca798..f2951b18e 100644 --- a/ios/FastImage/FFFastImageView.h +++ b/ios/FastImage/FFFastImageView.h @@ -15,6 +15,7 @@ @property (nonatomic, copy) RCTDirectEventBlock onFastImageError; @property (nonatomic, copy) RCTDirectEventBlock onFastImageLoad; @property (nonatomic, copy) RCTDirectEventBlock onFastImageLoadEnd; +@property (nonatomic, assign) BOOL useLastImageAsDefaultSource; @property (nonatomic, assign) RCTResizeMode resizeMode; @property (nonatomic, strong) FFFastImageSource *source; @property (nonatomic, strong) UIImage *defaultSource; diff --git a/ios/FastImage/FFFastImageView.m b/ios/FastImage/FFFastImageView.m index f7100815e..f7cffc520 100644 --- a/ios/FastImage/FFFastImageView.m +++ b/ios/FastImage/FFFastImageView.m @@ -30,6 +30,12 @@ - (void) setResizeMode: (RCTResizeMode)resizeMode { } } +- (void) setUseLastImageAsDefaultSource: (BOOL*)useLastImageAsDefaultSource { + if (useLastImageAsDefaultSource != _useLastImageAsDefaultSource) { + _useLastImageAsDefaultSource = useLastImageAsDefaultSource; + } +} + - (void) setOnFastImageLoadEnd: (RCTDirectEventBlock)onFastImageLoadEnd { _onFastImageLoadEnd = onFastImageLoadEnd; if (self.hasCompleted) { @@ -205,7 +211,7 @@ - (void) reloadImage { - (void) downloadImage: (FFFastImageSource*)source options: (SDWebImageOptions)options context: (SDWebImageContext*)context { __weak typeof(self) weakSelf = self; // Always use a weak reference to self in blocks [self sd_setImageWithURL: _source.url - placeholderImage: _defaultSource + placeholderImage: _useLastImageAsDefaultSource == YES ? [super image] : _defaultSource options: options context: context progress: ^(NSInteger receivedSize, NSInteger expectedSize, NSURL* _Nullable targetURL) { diff --git a/package.json b/package.json index 83ed61d5a..77f5df5f8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@applicaster/react-native-fast-image", - "version": "8.7.2", + "version": "8.8.1", "description": "🚩 FastImage, performant React Native image component.", "keywords": [ "cache", @@ -78,5 +78,6 @@ "peerDependencies": { "react": "^17 || ^18", "react-native": ">=0.60.0" - } + }, + "packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610" } diff --git a/src/index.js.flow b/src/index.js.flow index 57908331d..b4358399b 100644 --- a/src/index.js.flow +++ b/src/index.js.flow @@ -64,6 +64,7 @@ export type FastImageProps = $ReadOnly<{| tintColor?: number | string, resizeMode?: ?ResizeModes, fallback?: ?boolean, + useLastImageAsDefaultSource?: ?boolean, testID?: ?string, |}> diff --git a/src/index.tsx b/src/index.tsx index e1888961b..54dde32c9 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -87,6 +87,7 @@ export interface FastImageProps extends AccessibilityProps, ViewProps { defaultSource?: ImageRequireSource resizeMode?: ResizeMode fallback?: boolean + useLastImageAsDefaultSource?: boolean onLoadStart?(): void