Skip to content

Commit

Permalink
Merge pull request #19 from PauloniaAQP/dev
Browse files Browse the repository at this point in the history
New release version 0.5.0
  • Loading branch information
ChrisChV authored Aug 16, 2021
2 parents d9dfa41 + c817b9c commit 42350fe
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions example/ios/Flutter/Debug.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"
#include "Generated.xcconfig"
1 change: 1 addition & 0 deletions example/ios/Flutter/Release.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#include? "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"
#include "Generated.xcconfig"
6 changes: 5 additions & 1 deletion lib/InMemoryManager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 12 additions & 2 deletions lib/paulonia_cache_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PCacheImage extends ImageProvider<PCacheImage> {
this.retryDuration,
this.maxRetryDuration,
this.enableInMemory,
this.clearCacheImage = false,
});

/// The url of the image
Expand All @@ -31,6 +32,9 @@ class PCacheImage extends ImageProvider<PCacheImage> {
/// Enable or disable the cache.
bool? enableCache;

/// clear cache.
bool clearCacheImage;

/// If download fails, retry after this duration
Duration? retryDuration;

Expand Down Expand Up @@ -82,10 +86,12 @@ class PCacheImage extends ImageProvider<PCacheImage> {
@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!,
);
}
Expand All @@ -101,4 +107,8 @@ class PCacheImage extends ImageProvider<PCacheImage> {
if (enableInMemory == null)
enableInMemory = GlobalValues.globalInMemoryValue;
}

static Future clearAllCacheImages() async {
return await PCacheImageService.clearAllImages();
}
}
21 changes: 15 additions & 6 deletions lib/paulonia_cache_image_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ui.Codec> getImage(
String url,
Duration retryDuration,
Duration maxRetryDuration,
bool enableCache,
) async {
static Future<ui.Codec> 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 {
Expand Down Expand Up @@ -109,4 +111,11 @@ class PCacheImageService {
Uri uri = Uri.parse(gsUrl);
return FirebaseStorage.instance.ref().child(uri.path).getDownloadURL();
}

static Future<dynamic> clearAllImages() async {
Directory directory = await getTemporaryDirectory();
directory.deleteSync(recursive: true);
_tempPath = (await getTemporaryDirectory()).path;
return 'success';
}
}
29 changes: 22 additions & 7 deletions lib/paulonia_cache_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PCacheImageService {
static final Codec<String, String> _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
///
Expand All @@ -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<ui.Codec> getImage(
String url,
Duration retryDuration,
Duration maxRetryDuration,
bool enableCache,
) async {
static Future<ui.Codec> 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(
Expand Down Expand Up @@ -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);
Expand All @@ -125,4 +132,12 @@ class PCacheImageService {
static Future<String> _getStandardUrlFromGsUrl(String gsUrl) async {
return (await fb.storage().refFromURL(gsUrl).getDownloadURL()).toString();
}

/// delete all images from storage
static Future<dynamic> clearAllImages() async {
await _cacheBox.close();
await _cacheBox.deleteFromDisk();
_cacheBox = await Hive.openBox(Constants.HIVE_CACHE_IMAGE_BOX);
return 'success';
}
}
20 changes: 10 additions & 10 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down

0 comments on commit 42350fe

Please sign in to comment.