Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented check meta data cycle #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class FirebaseImageCacheManager {
late String basePath;

final CacheRefreshStrategy cacheRefreshStrategy;
final Duration? metaDataRefreshCycle;

FirebaseImageCacheManager(
this.cacheRefreshStrategy,
this.metaDataRefreshCycle
);

Future<void> open() async {
Expand All @@ -34,11 +36,12 @@ class FirebaseImageCacheManager {
remotePath TEXT,
localPath TEXT,
bucket TEXT,
version INTEGER
version INTEGER,
lastMetaDataCheck INTEGER
)
''');
},
version: 1,
version: 2,
);
basePath = await _createFilePath();
}
Expand Down Expand Up @@ -93,7 +96,15 @@ class FirebaseImageCacheManager {
FirebaseImageObject.fromMap(maps.first);
returnObject.reference = getImageRef(returnObject, image.firebaseApp);
if (CacheRefreshStrategy.BY_METADATA_DATE == this.cacheRefreshStrategy) {
checkForUpdate(returnObject, image); // Check for update in background
if(this.metaDataRefreshCycle != null){ //If metaDataRefreshCycle null always checkForUpdate
DateTime lastMetaDataCheck = DateTime.fromMillisecondsSinceEpoch(returnObject.lastMetaDataCheck);
lastMetaDataCheck.add(this.metaDataRefreshCycle!); // Add duration to lastMetaDataCheck
if(DateTime.now().isAfter(lastMetaDataCheck)){
checkForUpdate(returnObject, image); // Check for update in background
}
}else{
checkForUpdate(returnObject, image); // Check for update in background
}
}
return returnObject;
}
Expand Down Expand Up @@ -152,6 +163,8 @@ class FirebaseImageCacheManager {
.updated
?.millisecondsSinceEpoch ??
0;

object.lastMetaDataCheck = DateTime.now().millisecondsSinceEpoch;
}
Uint8List? bytes = await remoteFileBytes(object, maxSizeBytes);
await putFile(object, bytes);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/firebase_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
/// Default: BY_METADATA_DATE. Specifies the strategy in which to check if the cached version should be refreshed (optional)
final CacheRefreshStrategy cacheRefreshStrategy;

/// Defines in which cycle the meta data will be checked (optional)
final Duration? metaDataRefreshCycle;

/// Default: the default Firebase app. Specifies a custom Firebase app to make the request to the bucket from (optional)
final FirebaseApp? firebaseApp;

Expand All @@ -35,13 +38,15 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
/// [scale] Default: 1.0. The scale to display the image at (optional)
/// [maxSizeBytes] Default: 2.5MB. The maximum size in bytes to be allocated in the device's memory for the image (optional)
/// [cacheRefreshStrategy] Default: BY_METADATA_DATE. Specifies the strategy in which to check if the cached version should be refreshed (optional)
/// [metaDataRefreshCycle] Defines in which cycle the meta data will be checked (optional)
/// [firebaseApp] Default: the default Firebase app. Specifies a custom Firebase app to make the request to the bucket from (optional)
FirebaseImage(
String location, {
this.shouldCache = true,
this.scale = 1.0,
this.maxSizeBytes = 2500 * 1000, // 2.5MB
this.cacheRefreshStrategy = CacheRefreshStrategy.BY_METADATA_DATE,
this.metaDataRefreshCycle,
this.firebaseApp,
}) : _imageObject = FirebaseImageObject(
bucket: _getBucket(location),
Expand Down Expand Up @@ -74,6 +79,7 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
Uint8List? bytes;
FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager(
cacheRefreshStrategy,
metaDataRefreshCycle
);

if (shouldCache) {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/image_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:firebase_storage/firebase_storage.dart';

class FirebaseImageObject {
int version;
int lastMetaDataCheck;
Reference reference;
String? localPath;
final String remotePath;
Expand All @@ -10,6 +11,7 @@ class FirebaseImageObject {

FirebaseImageObject({
this.version = -1,
this.lastMetaDataCheck = -1,
required this.reference,
this.localPath,
required this.bucket,
Expand All @@ -19,6 +21,7 @@ class FirebaseImageObject {
Map<String, dynamic> toMap() {
return {
'version': this.version,
'lastMetaDataCheck': this.lastMetaDataCheck,
'localPath': this.localPath,
'bucket': this.bucket,
'remotePath': this.remotePath,
Expand All @@ -29,6 +32,7 @@ class FirebaseImageObject {
factory FirebaseImageObject.fromMap(Map<String, dynamic> map) {
return FirebaseImageObject(
version: map["version"] ?? -1,
lastMetaDataCheck: map["lastMetaDataCheck"] ?? -1,
reference: map["reference"],
localPath: map["localPath"],
bucket: map["bucket"],
Expand Down