-
-
Notifications
You must be signed in to change notification settings - Fork 875
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom `ImageProvider`s Reduced reliance on 'universal_io' library Prepared for review
- Loading branch information
1 parent
d7c7648
commit ad2adb6
Showing
5 changed files
with
207 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
lib/src/layer/tile_layer/tile_provider/network_image_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/painting.dart'; | ||
import 'package:http/http.dart'; | ||
import 'package:http/retry.dart'; | ||
|
||
class NetworkImageProvider extends ImageProvider<NetworkImageProvider> { | ||
/// The URL from which the image will be fetched. | ||
final String url; | ||
|
||
/// The http RetryClient that is used for the requests | ||
final RetryClient retryClient; | ||
|
||
/// Custom headers to add to the image fetch request | ||
final Map<String, String> headers; | ||
|
||
NetworkImageProvider( | ||
this.url, { | ||
RetryClient? retryClient, | ||
this.headers = const {}, | ||
}) : retryClient = retryClient ?? RetryClient(Client()); | ||
|
||
@override | ||
ImageStreamCompleter load(NetworkImageProvider key, DecoderCallback decode) { | ||
return OneFrameImageStreamCompleter(_loadWithRetry(key, decode), | ||
informationCollector: () sync* { | ||
yield ErrorDescription('Image provider: $this'); | ||
yield ErrorDescription('Image key: $key'); | ||
}); | ||
} | ||
|
||
@override | ||
Future<NetworkImageProvider> obtainKey(ImageConfiguration configuration) { | ||
return SynchronousFuture<NetworkImageProvider>(this); | ||
} | ||
|
||
Future<ImageInfo> _loadWithRetry( | ||
NetworkImageProvider key, | ||
DecoderCallback decode, | ||
) async { | ||
assert(key == this); | ||
|
||
final uri = Uri.parse(url); | ||
final response = await retryClient.get(uri, headers: headers); | ||
|
||
if (response.statusCode != 200) { | ||
throw NetworkImageLoadException( | ||
statusCode: response.statusCode, uri: uri); | ||
} | ||
|
||
final codec = await decode(response.bodyBytes); | ||
final image = (await codec.getNextFrame()).image; | ||
|
||
return ImageInfo(image: image); | ||
} | ||
} |
46 changes: 0 additions & 46 deletions
46
lib/src/layer/tile_layer/tile_provider/network_image_with_retry.dart
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
lib/src/layer/tile_layer/tile_provider/network_no_retry_image_provider.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
import 'dart:typed_data'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/painting.dart'; | ||
|
||
class NetworkNoRetryImageProvider | ||
extends ImageProvider<NetworkNoRetryImageProvider> { | ||
/// A valid URL, which is the location of the image to be fetched | ||
final String url; | ||
|
||
/// The client which will be used to fetch the image | ||
final HttpClient httpClient; | ||
|
||
/// Custom headers to add to the image fetch request | ||
final Map<String, String> headers; | ||
|
||
NetworkNoRetryImageProvider( | ||
this.url, { | ||
HttpClient? httpClient, | ||
this.headers = const {}, | ||
}) : httpClient = httpClient ?? HttpClient() | ||
..userAgent = null; | ||
|
||
@override | ||
ImageStreamCompleter load( | ||
NetworkNoRetryImageProvider key, | ||
DecoderCallback decode, | ||
) { | ||
final StreamController<ImageChunkEvent> chunkEvents = | ||
StreamController<ImageChunkEvent>(); | ||
|
||
return MultiFrameImageStreamCompleter( | ||
codec: _loadAsync(key: key, decode: decode, chunkEvents: chunkEvents), | ||
chunkEvents: chunkEvents.stream, | ||
scale: 1, | ||
debugLabel: key.url, | ||
informationCollector: () => <DiagnosticsNode>[ | ||
DiagnosticsProperty<ImageProvider>('Image provider', this), | ||
DiagnosticsProperty<NetworkNoRetryImageProvider>('Image key', key), | ||
], | ||
); | ||
} | ||
|
||
@override | ||
Future<NetworkNoRetryImageProvider> obtainKey( | ||
ImageConfiguration configuration) { | ||
return SynchronousFuture<NetworkNoRetryImageProvider>(this); | ||
} | ||
|
||
Future<Codec> _loadAsync({ | ||
required NetworkNoRetryImageProvider key, | ||
required DecoderCallback decode, | ||
required StreamController<ImageChunkEvent> chunkEvents, | ||
}) async { | ||
try { | ||
assert(key == this); | ||
|
||
final Uri resolved = Uri.base.resolve(key.url); | ||
|
||
final HttpClientRequest request = await httpClient.getUrl(resolved); | ||
|
||
headers.forEach((String name, String value) { | ||
request.headers.add(name, value); | ||
}); | ||
|
||
final HttpClientResponse response = await request.close(); | ||
if (response.statusCode != HttpStatus.ok) { | ||
await response.drain<List<int>>(<int>[]); | ||
throw NetworkImageLoadException( | ||
statusCode: response.statusCode, uri: resolved); | ||
} | ||
|
||
final Uint8List bytes = await consolidateHttpClientResponseBytes( | ||
response, | ||
onBytesReceived: (int cumulative, int? total) { | ||
chunkEvents.add(ImageChunkEvent( | ||
cumulativeBytesLoaded: cumulative, | ||
expectedTotalBytes: total, | ||
)); | ||
}, | ||
); | ||
if (bytes.lengthInBytes == 0) { | ||
throw Exception('NetworkImage is an empty file: $resolved'); | ||
} | ||
|
||
return decode(bytes); | ||
} catch (e) { | ||
scheduleMicrotask(() { | ||
PaintingBinding.instance.imageCache.evict(key); | ||
}); | ||
rethrow; | ||
} finally { | ||
chunkEvents.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters