Skip to content

Commit

Permalink
fix: updates to clearCache
Browse files Browse the repository at this point in the history
  • Loading branch information
gtokman committed May 13, 2024
1 parent 1c2bebe commit 9971991
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package com.candlefinance.fasterimage

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Outline
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Base64
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.ImageView.ScaleType
import androidx.appcompat.widget.AppCompatImageView
import android.graphics.BitmapFactory
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Outline
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Base64
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.ImageView.ScaleType
import androidx.appcompat.widget.AppCompatImageView
import coil.annotation.ExperimentalCoilApi
import coil.imageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.size.Scale
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.events.RCTEventEmitter
import okhttp3.Headers
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.size.Scale
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.events.RCTEventEmitter


class FasterImageViewManager : SimpleViewManager<AppCompatImageView>() {
Expand Down Expand Up @@ -174,6 +175,15 @@
return intArray
}

@OptIn(ExperimentalCoilApi::class)
@ReactMethod
fun clearCache(view: AppCompatImageView, promise: Promise) {
val imageLoader = view.context.imageLoader
imageLoader.memoryCache?.clear()
imageLoader.diskCache?.clear()
promise.resolve(null)
}

companion object {
private val RESIZE_MODE = mapOf(
"contain" to ScaleType.FIT_CENTER,
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native/ReactCommon/yoga"

SPEC CHECKSUMS:
boost: 7dcd2de282d72e344012f7d6564d024930a6a440
boost: 57d2868c099736d80fcd648bf211b4431e51a558
DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54
FasterImage: 4242647a08a8d846bf81803f16d8e1b65dcdf82b
FBLazyVector: 719c60593e0a5b54cba28d1e50250581efff79d2
Expand Down Expand Up @@ -474,4 +474,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 949b735967dd6c64873d44a6244cb933a1eac03b

COCOAPODS: 1.15.2
COCOAPODS: 1.14.3
7 changes: 5 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const imageURLs = Array.from(
);

if (__DEV__ && Platform.OS === 'ios') {
DevSettings.addMenuItem('Clear Cache', () => clearCache());
DevSettings.addMenuItem('Clear Cache', async () => {
const result = await clearCache();
console.log(result);
});
}

export default function App() {
Expand Down Expand Up @@ -66,7 +69,7 @@ export default function App() {
base64Placeholder:
'iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAADy9JREFUeF7tnXmMnlMUxs8QtXVqazBopBFDQhAaEVuJSkutiSUkKIKUNrGkqNgq+EOiRNNNaKkQ/CHErkU0VRFji0R0LBlttZZWhIaqZeTcmum0M98373KXc8593kTSme++557znOf33nvfLzItHR0d3cOHD6chQ4YQLigABTYqsGHDBlqz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vp/3335+23nrr3t8XBgRwFO4TBhLRY489RpdeemmvFieccAK9/PLLtMMOOzgjnnzyydTZ2ek+nzBhAj366KMOIDb5OeecQy+88IL77JBDDqG33357QIj6Cv3xxx/T6NGj3f9Qxdc+++xDCxcupAMPPLBfzIMPPpgeeughGjVqlIPknnvuoVtvvdXdxz/zfEcccYT7uRAggAOeL6PATz/9RLvvvjvddtttdN1119Fbb73lTP/000/T+eefT2eccQZ9//339Nxzz9HXX39NDM+cOXPoqquuogcffJBuv/12evPNN2n48OE0btw4Ouyww+iZZ55pmsKhhx5KbW1tDsxvv/3WzXf66afTrFmzBozJkNxyyy20bt06N//8+fPplFNOoeuvv54WLVpEK1eupG222WZwQABHGWtgLCvw6quv0qmnnuq2Mtttt50T5dhjj6W9996bZsyYQXvssYd7uo8ZM8Z9dt555zlgFi9eTAcddJAz97Rp09xns2fPpquvvpp+/vlnuuSSS9yWi2PwNXHiRDcHQ8XbuHfeeYeOP/5499mdd95J06dPp19//bVhTJ6Tgfjmm2/ovffec/d99tlnbtViqE888cTmgAAOGL6KAmy87777rnebwvt+Xg3Y7Gy+Y445hn755RfaaaedXPg77riDHnjgAQcBP7VfeeUV9zTni5/mvB3jswWfWU477TR69tln6c8//6SLLrqI3n//feLV4JNPPqHDDz/cAfnPP/+47RYD+eSTTzaNyfCNGDHCQcfbKz6L7LjjjvTII4/Q5Zdf3hgQwFHFGrhnSwXY4HwW4bPHBx984M4hF154If3777/U0tLihvP25rLLLnNbI14h3n33XTr66KPdZ19++aU7UPO9fGa45ppr6IknnnCf8Rbp5ptv3mxKHn/llVfShx9+6FYB3uo1i3n22WfTFVdcQePHj+89uPMKx3F5ezjgGQRwwOh1FeAnMYPBT3s+ALOZt99+e1q6dKlbQdasWUO77babm2bmzJk0b948t83hN098QOdzCl+ffvqpO4Pw6sLbqB9//NFt0fhpzzGGDBnixv311190991301133UXnnnuu23bttddetGHDhqYxL7jgArcF4y0ZvzRgGIcNG0YvvfSSg6YfIICjrjVwP3vopJNOcuZ9/PHHab/99usVpWcLw1ujI4880v2ezxK8LXr44YfdWYWf6jfccIP7jA/2U6ZMoRUrVrifJ0+e7GLyHPfff787Q/B18cUX0+uvv+7G89mh79UsJoPR0dHhgOCYS5Ysceenrq4ut/JsBgjggLl9KMArwFlnneVMx0/xnoufzAwLg7Hnnns6o7/22mtuy/XUU08RP82nTp3qVhMGiM8yfA5gg8+dO9eN5bMJx+XvV2688Ua3wvDbK95K8evanrMLz8mvjfntVrOYPS8UGK4DDjjAAfjRRx+5+JxvLyBDhw7tXWL6foHiQzDEyEuBm266ie67775+RZ955pn0/PPPO/PxU563S3xNmjSp980Uv3XiJzifQ/g66qij6I033nDfZfB3GmPHjqUFCxa4n4877jj3luree+91QA508ZeGjWKyz/nza6+91n0vwheD9uKLL7ovC3m7xa+BW7q6urp5P7flt4t5tRXVxlSAt1TLli1zK8zOO+/cb2o+sPMKwG+YfF3NYvIZh79pZwj55UHPborfvrV0dHR08z96Djy+EkIcKKBZAT7g88LxHwLPk0Gz4D/UAAAAAElFTkSuQmCC',
url: item,
grayscale: 1,
// grayscale: 1,
}}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion ios/FasterImageViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ @interface RCT_EXTERN_MODULE(FasterImageViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(onSuccess, RCTDirectEventBlock)

RCT_EXTERN_METHOD(clearCache)
RCT_EXTERN_METHOD(clearCache:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

@end
15 changes: 11 additions & 4 deletions ios/FasterImageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ final class FasterImageViewManager: RCTViewManager {
return true
}

@objc func clearCache() {
ImagePipeline.shared.cache.removeAll()
ImageCache.shared.removeAll()
DataLoader.sharedUrlCache.removeAllCachedResponses()
@objc(clearCache:rejecter:)
func clearCache(_ resolve: @escaping RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock) {
DispatchQueue.global(qos: .userInteractive).async {
ImagePipeline.shared.cache.removeAll()
ImageCache.shared.removeAll()
DataLoader.sharedUrlCache.removeAllCachedResponses()
DispatchQueue.main.async {
resolve(true)
}
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,7 @@ const ComponentName = 'FasterImageView';
export const FasterImageView =
requireNativeComponent<FasterImageProps>(ComponentName);

export const clearCache = () => {
if (Platform.OS !== 'ios') {
return;
}

export const clearCache = async () => {
const { FasterImageViewManager } = NativeModules;
FasterImageViewManager.clearCache();
return FasterImageViewManager.clearCache();
};

0 comments on commit 9971991

Please sign in to comment.