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

precache #27

Open
wants to merge 3 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
1 change: 1 addition & 0 deletions lib/firebase_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ library firebase_image;

export 'src/firebase_image.dart';
export 'src/cache_refresh_strategy.dart';
export 'src/precache_firebase_image.dart';
17 changes: 15 additions & 2 deletions lib/src/firebase_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
reference: _getImageRef(location, firebaseApp),
);

Uint8List _precachedBytes;

/// Returns the image as bytes
Future<Uint8List> getBytes() {
return _fetchImage();
Future<Uint8List> getBytes() async {
return _precachedBytes ?? await _fetchImage();
}

static String _getBucket(String location) {
Expand Down Expand Up @@ -99,6 +101,17 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
return bytes;
}

/// Precache this image.
Future<void> precache() async {
assert(shouldCache == true);
FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager(
cacheRefreshStrategy,
);
await cacheManager.open();
_precachedBytes ??= await cacheManager.upsertRemoteFileToCache(
_imageObject, this.maxSizeBytes);
}

Future<Codec> _fetchImageCodec() async {
return await PaintingBinding.instance
.instantiateImageCodec(await _fetchImage());
Expand Down
21 changes: 21 additions & 0 deletions lib/src/precache_firebase_image.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:firebase_core/firebase_core.dart';

import 'cache_refresh_strategy.dart';
import 'firebase_image.dart';

Future<void> precacheFirebaseImage(String location,
{FirebaseApp firebaseApp,
int maxSizeBytes = 2500 * 1000,
CacheRefreshStrategy cacheRefreshStrategy =
CacheRefreshStrategy.BY_METADATA_DATE}) async {
assert(location != null);

final image = FirebaseImage(
location,
firebaseApp: firebaseApp,
maxSizeBytes: maxSizeBytes,
cacheRefreshStrategy: cacheRefreshStrategy,
);

await image.precache();
}