diff --git a/CHANGELOG.md b/CHANGELOG.md index e1519f4..6599d4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.5.0] + +- Boolean added to refresh the cache image. (@chetan-cueclad & @kaumudpa) +- README updated. +- Dependencies updated. + ## [0.4.1] - README.md updated diff --git a/README.md b/README.md index 51155ef..efb4600 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# Paulonia Cache Image +# Paulonia Cache Image (with null safety) + +**Null safety:** This package supports [Null safety](https://pub.dev/packages/paulonia_cache_image/versions) but due to an error the null-safety badge is not placed. Flutter package for download and store images in cache. It supports in-memory and storage cache in Android, iOS and Web for network and Google Cloud Storage images. @@ -62,6 +64,14 @@ void main() async{ This functionality works as a queue, to save a new image to memory, the oldest one is deleted. +### Clear Entire Cache + +There could be situations where you might want to clear the whole cache may be to rebuild the cache again. Calling the following method will empty the cache. + +```dart +await PCacheImage.clearAllCacheImages(); +``` + ## CORS on web On the web when you try to make a request and download an image, it can throw an error with the CORS. Depends on the image type there is a solution: @@ -96,6 +106,7 @@ retryDuration | If the download fails, retry after this duration | 2s maxRetryDuration | Max accumulated time of retries | 10s imageScale | The image scale | 1.0 maxInMemoryImages | Global variable, sets a max number of images in memory | 7 +clearCacheImage | Deletes the image from cache | false ## Author diff --git a/example/ios/Flutter/Debug.xcconfig b/example/ios/Flutter/Debug.xcconfig index 592ceee..ec97fc6 100644 --- a/example/ios/Flutter/Debug.xcconfig +++ b/example/ios/Flutter/Debug.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig" #include "Generated.xcconfig" diff --git a/example/ios/Flutter/Release.xcconfig b/example/ios/Flutter/Release.xcconfig index 592ceee..c4855bf 100644 --- a/example/ios/Flutter/Release.xcconfig +++ b/example/ios/Flutter/Release.xcconfig @@ -1 +1,2 @@ +#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig" #include "Generated.xcconfig" diff --git a/lib/InMemoryManager.dart b/lib/InMemoryManager.dart index cdb6424..831b94d 100644 --- a/lib/InMemoryManager.dart +++ b/lib/InMemoryManager.dart @@ -26,7 +26,11 @@ class InMemoryManager { /// /// Verifies if the image is in memory cache and returns it. Otherwise it calls /// [getImage()] from the service of the platform. - static ImageStreamCompleter getImage(PCacheImage key) { + static ImageStreamCompleter getImage(PCacheImage key, + {bool clearMemoryImg = false}) { + // clear the image from memory + if (clearMemoryImg) _manager.remove(key.url); + if (_manager.containsKey(key.url)) return _manager[key.url]!; ImageStreamCompleter res = MultiFrameImageStreamCompleter( codec: PCacheImageService.getImage( diff --git a/lib/paulonia_cache_image.dart b/lib/paulonia_cache_image.dart index fe308b7..df21df6 100644 --- a/lib/paulonia_cache_image.dart +++ b/lib/paulonia_cache_image.dart @@ -20,6 +20,7 @@ class PCacheImage extends ImageProvider { this.retryDuration, this.maxRetryDuration, this.enableInMemory, + this.clearCacheImage = false, }); /// The url of the image @@ -31,6 +32,9 @@ class PCacheImage extends ImageProvider { /// Enable or disable the cache. bool? enableCache; + /// clear cache. + bool clearCacheImage; + /// If download fails, retry after this duration Duration? retryDuration; @@ -82,10 +86,12 @@ class PCacheImage extends ImageProvider { @override ImageStreamCompleter load(PCacheImage key, DecoderCallback decode) { _initializeValues(); - if (enableCache! && enableInMemory!) return InMemoryManager.getImage(key); + if (enableCache! && enableInMemory!) + return InMemoryManager.getImage(key, clearMemoryImg: clearCacheImage); return MultiFrameImageStreamCompleter( codec: PCacheImageService.getImage( - url, retryDuration!, maxRetryDuration!, enableCache!), + url, retryDuration!, maxRetryDuration!, enableCache!, + clearCacheImage: clearCacheImage), scale: key.imageScale!, ); } @@ -101,4 +107,8 @@ class PCacheImage extends ImageProvider { if (enableInMemory == null) enableInMemory = GlobalValues.globalInMemoryValue; } + + static Future clearAllCacheImages() async { + return await PCacheImageService.clearAllImages(); + } } diff --git a/lib/paulonia_cache_image_mobile.dart b/lib/paulonia_cache_image_mobile.dart index 3d832e4..c57506b 100644 --- a/lib/paulonia_cache_image_mobile.dart +++ b/lib/paulonia_cache_image_mobile.dart @@ -35,16 +35,18 @@ class PCacheImageService { /// in cache and returns it if [enableCache] is true. If the images is not in cache /// then the function download the image and stores in cache if [enableCache] /// is true. - static Future getImage( - String url, - Duration retryDuration, - Duration maxRetryDuration, - bool enableCache, - ) async { + static Future getImage(String url, Duration retryDuration, + Duration maxRetryDuration, bool enableCache, + {bool clearCacheImage = false}) async { Uint8List bytes; String id = _stringToBase64.encode(url); + String path = _tempPath + '/' + id; final File file = File(path); + + if (clearCacheImage) { + file.deleteSync(); + } if (_fileIsCached(file)) bytes = file.readAsBytesSync(); else { @@ -109,4 +111,11 @@ class PCacheImageService { Uri uri = Uri.parse(gsUrl); return FirebaseStorage.instance.ref().child(uri.path).getDownloadURL(); } + + static Future clearAllImages() async { + Directory directory = await getTemporaryDirectory(); + directory.deleteSync(recursive: true); + _tempPath = (await getTemporaryDirectory()).path; + return 'success'; + } } diff --git a/lib/paulonia_cache_image_web.dart b/lib/paulonia_cache_image_web.dart index 9831323..9f0907b 100644 --- a/lib/paulonia_cache_image_web.dart +++ b/lib/paulonia_cache_image_web.dart @@ -22,7 +22,7 @@ class PCacheImageService { static final Codec _stringToBase64 = utf8.fuse(base64); /// Hive box used to store the image as a persistent storage - static final _cacheBox = Hive.box(Constants.HIVE_CACHE_IMAGE_BOX); + static var _cacheBox = Hive.box(Constants.HIVE_CACHE_IMAGE_BOX); /// Initialize the service on web /// @@ -39,13 +39,14 @@ class PCacheImageService { /// in cache and returns it if [enableCache] is true. If the images is not in cache /// then the function download the image and stores in cache if [enableCache] /// is true. - static Future getImage( - String url, - Duration retryDuration, - Duration maxRetryDuration, - bool enableCache, - ) async { + static Future getImage(String url, Duration retryDuration, + Duration maxRetryDuration, bool enableCache, + {bool clearCacheImage = false}) async { Uint8List bytes; + // delete single image from cache + if (clearCacheImage) { + await _deleteHiveImage(url); + } HiveCacheImage? cacheImage = _getHiveImage(url); if (cacheImage == null) { bytes = await _downloadImage( @@ -108,6 +109,12 @@ class PCacheImageService { return _cacheBox.get(id); } + /// delete the image form the hive storage + static Future _deleteHiveImage(String url) { + String id = _stringToBase64.encode(url); + return _cacheBox.delete(id); + } + /// Save the image in the hive storage static HiveCacheImage _saveHiveImage(String url, Uint8List image) { String id = _stringToBase64.encode(url); @@ -125,4 +132,12 @@ class PCacheImageService { static Future _getStandardUrlFromGsUrl(String gsUrl) async { return (await fb.storage().refFromURL(gsUrl).getDownloadURL()).toString(); } + + /// delete all images from storage + static Future clearAllImages() async { + await _cacheBox.close(); + await _cacheBox.deleteFromDisk(); + _cacheBox = await Hive.openBox(Constants.HIVE_CACHE_IMAGE_BOX); + return 'success'; + } } diff --git a/pubspec.lock b/pubspec.lock index 2f1d7b0..52c93f1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.5.0" + version: "2.6.1" boolean_selector: dependency: transitive description: @@ -84,42 +84,42 @@ packages: name: firebase_core url: "https://pub.dartlang.org" source: hosted - version: "1.0.3" + version: "1.4.0" firebase_core_platform_interface: dependency: transitive description: name: firebase_core_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "4.0.0" + version: "4.0.1" firebase_core_web: dependency: transitive description: name: firebase_core_web url: "https://pub.dartlang.org" source: hosted - version: "1.0.2" + version: "1.1.0" firebase_storage: dependency: "direct main" description: name: firebase_storage url: "https://pub.dartlang.org" source: hosted - version: "8.0.3" + version: "10.0.1" firebase_storage_platform_interface: dependency: transitive description: name: firebase_storage_platform_interface url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "4.0.0" firebase_storage_web: dependency: transitive description: name: firebase_storage_web url: "https://pub.dartlang.org" source: hosted - version: "1.0.3" + version: "3.0.0" flutter: dependency: "direct main" description: flutter @@ -141,14 +141,14 @@ packages: name: hive url: "https://pub.dartlang.org" source: hosted - version: "2.0.2" + version: "2.0.4" http: dependency: "direct main" description: name: http url: "https://pub.dartlang.org" source: hosted - version: "0.13.1" + version: "0.13.3" http_parser: dependency: transitive description: @@ -190,7 +190,7 @@ packages: name: path_provider url: "https://pub.dartlang.org" source: hosted - version: "2.0.1" + version: "2.0.2" path_provider_linux: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index c6955a4..da506d2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: paulonia_cache_image description: Flutter package for cache images in storage or memory from the network or Google Cloud Storage. It supports Android, iOS and Web. -version: 0.4.1 +version: 0.5.0 homepage: https://paulonia.dev repository: https://github.com/PauloniaAQP/paulonia_cache_image @@ -11,9 +11,9 @@ environment: dependencies: flutter: sdk: flutter - firebase_storage: ^8.0.6 + firebase_storage: ^10.0.1 firebase: ^9.0.1 - path_provider: ^2.0.1 + path_provider: ^2.0.2 hive: ^2.0.4 http: ^0.13.3