Skip to content

[ExtendedNetworkImageProvider] use bytesLoader to custom bytes loader #71

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

Open
wants to merge 1 commit 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.1
* network_image_io: [ExtendedNetworkImageProvider] use bytesLoader to custom bytes loader

## 5.0.0

* Migrate to 3.29.0
Expand Down
5 changes: 5 additions & 0 deletions lib/src/network/extended_network_image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ abstract class ExtendedNetworkImageProvider
String? imageCacheName,
Duration? cacheMaxAge,
WebHtmlElementStrategy webHtmlElementStrategy,
FutureOr<Uint8List> Function(void Function(ImageChunkEvent) chunkEvent)?
bytesLoader,
}) = network_image.ExtendedNetworkImageProvider;

/// Time Limit to request image
Expand Down Expand Up @@ -76,6 +78,9 @@ abstract class ExtendedNetworkImageProvider
/// Has no effect on other platforms, which always fetch bytes.
WebHtmlElementStrategy get webHtmlElementStrategy;

FutureOr<Uint8List> Function(void Function(ImageChunkEvent chunkEvent))?
get bytesLoader;

///get network image data from cached
Future<Uint8List?> getNetworkImageData({
StreamController<ImageChunkEvent>? chunkEvents,
Expand Down
71 changes: 43 additions & 28 deletions lib/src/network/network_image_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ExtendedNetworkImageProvider
this.imageCacheName,
this.cacheMaxAge,
this.webHtmlElementStrategy = WebHtmlElementStrategy.never,
this.bytesLoader,
});

/// The name of [ImageCache], you can define custom [ImageCache] to store this provider.
Expand Down Expand Up @@ -94,6 +95,10 @@ class ExtendedNetworkImageProvider
@override
final WebHtmlElementStrategy webHtmlElementStrategy;

@override
final FutureOr<Uint8List> Function(void Function(ImageChunkEvent chunkEvent))?
bytesLoader;

@override
ImageStreamCompleter loadImage(
image_provider.ExtendedNetworkImageProvider key,
Expand Down Expand Up @@ -188,18 +193,18 @@ class ExtendedNetworkImageProvider
Uint8List? data;
// exist, try to find cache image file
if (_cacheImagesDirectory.existsSync()) {
final File cacheFlie = File(join(_cacheImagesDirectory.path, md5Key));
if (cacheFlie.existsSync()) {
final File cacheFile = File(join(_cacheImagesDirectory.path, md5Key));
if (cacheFile.existsSync()) {
if (key.cacheMaxAge != null) {
final DateTime now = DateTime.now();
final FileStat fs = cacheFlie.statSync();
final FileStat fs = cacheFile.statSync();
if (now.subtract(key.cacheMaxAge!).isAfter(fs.changed)) {
cacheFlie.deleteSync(recursive: true);
cacheFile.deleteSync(recursive: true);
} else {
data = await cacheFlie.readAsBytes();
data = await cacheFile.readAsBytes();
}
} else {
data = await cacheFlie.readAsBytes();
data = await cacheFile.readAsBytes();
}
}
}
Expand All @@ -226,31 +231,41 @@ class ExtendedNetworkImageProvider
) async {
try {
final Uri resolved = Uri.base.resolve(key.url);
final HttpClientResponse? response = await _tryGetResponse(resolved);
if (response == null || response.statusCode != HttpStatus.ok) {
if (response != null) {
// The network may be only temporarily unavailable, or the file will be
// added on the server later. Avoid having future calls to resolve
// fail to check the network again.
await response.drain<List<int>>(<int>[]);

final Uint8List bytes;

if (bytesLoader != null) {
bytes = await bytesLoader!((ImageChunkEvent event) {
chunkEvents?.add(event);
});
} else {
final HttpClientResponse? response = await _tryGetResponse(resolved);
if (response == null || response.statusCode != HttpStatus.ok) {
if (response != null) {
// The network may be only temporarily unavailable, or the file will be
// added on the server later. Avoid having future calls to resolve
// fail to check the network again.
await response.drain<List<int>>(<int>[]);
}
return null;
}
return null;

bytes = await consolidateHttpClientResponseBytes(
response,
onBytesReceived:
chunkEvents != null
? (int cumulative, int? total) {
chunkEvents.add(
ImageChunkEvent(
cumulativeBytesLoaded: cumulative,
expectedTotalBytes: total,
),
);
}
: null,
);
}

final Uint8List bytes = await consolidateHttpClientResponseBytes(
response,
onBytesReceived:
chunkEvents != null
? (int cumulative, int? total) {
chunkEvents.add(
ImageChunkEvent(
cumulativeBytesLoaded: cumulative,
expectedTotalBytes: total,
),
);
}
: null,
);
if (bytes.lengthInBytes == 0) {
return Future<Uint8List>.error(
StateError('NetworkImage is an empty file: $resolved'),
Expand Down
6 changes: 6 additions & 0 deletions lib/src/network/network_image_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter/rendering.dart';
import 'package:http_client_helper/http_client_helper.dart';
import 'package:web/web.dart' as web;
import 'extended_network_image_provider.dart' as extended_image_provider;

// ignore: directives_ordering
import 'package:flutter/src/painting/image_provider.dart' as image_provider;

Expand Down Expand Up @@ -90,6 +91,7 @@ class ExtendedNetworkImageProvider
this.imageCacheName,
this.cacheMaxAge,
this.webHtmlElementStrategy = WebHtmlElementStrategy.never,
this.bytesLoader,
});

@override
Expand Down Expand Up @@ -138,6 +140,10 @@ class ExtendedNetworkImageProvider
@override
final WebHtmlElementStrategy webHtmlElementStrategy;

@override
final FutureOr<Uint8List> Function(void Function(ImageChunkEvent chunkEvent))?
bytesLoader;

@override
Future<ExtendedNetworkImageProvider> obtainKey(
ImageConfiguration configuration,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: extended_image_library
description: Library that contains common base class for `extended_image`, `extended_text`, and `extended_text_field`.
repository: https://github.com/fluttercandies/extended_image_library
version: 5.0.0
version: 5.0.1

environment:
sdk: ^3.7.0
Expand Down