-
-
Notifications
You must be signed in to change notification settings - Fork 897
Added cancellation support to TileProvider and surrounding mechanisms
#1622
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86eaaaa
Added cancellation support to `TileProvider` and surrounding mechanisms
JaffaKetchup c85b1f0
Removed duplicate import
JaffaKetchup 9fcb2ac
Improved documentation
JaffaKetchup 69ad3ed
Added example for cancellable `TileProvider`
JaffaKetchup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
51 changes: 51 additions & 0 deletions
51
example/lib/pages/cancellable_tile_provider/cancellable_tile_provider.dart
This file contains hidden or 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,51 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_map/flutter_map.dart'; | ||
| import 'package:flutter_map/plugin_api.dart'; | ||
| import 'package:flutter_map_example/pages/cancellable_tile_provider/ctp_impl.dart'; | ||
| import 'package:flutter_map_example/widgets/drawer.dart'; | ||
| import 'package:latlong2/latlong.dart'; | ||
|
|
||
| class CancellableTileProviderPage extends StatelessWidget { | ||
| static const String route = '/cancellable_tile_provider_page'; | ||
|
|
||
| const CancellableTileProviderPage({Key? key}) : super(key: key); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| appBar: AppBar(title: const Text('Cancellable Tile Provider')), | ||
| drawer: buildDrawer(context, CancellableTileProviderPage.route), | ||
| body: Column( | ||
| children: [ | ||
| const Padding( | ||
| padding: EdgeInsets.all(12), | ||
| child: Text( | ||
| 'This map uses a custom `TileProvider` that cancels HTTP requests for unnecessary tiles. This should help speed up tile loading and reduce unneccessary costly tile requests, mainly on the web!', | ||
| ), | ||
| ), | ||
| Expanded( | ||
| child: FlutterMap( | ||
| options: MapOptions( | ||
| initialCenter: const LatLng(51.5, -0.09), | ||
| initialZoom: 5, | ||
| cameraConstraint: CameraConstraint.contain( | ||
| bounds: LatLngBounds( | ||
| const LatLng(-90, -180), | ||
| const LatLng(90, 180), | ||
| ), | ||
| ), | ||
| ), | ||
| children: [ | ||
| TileLayer( | ||
| urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', | ||
| userAgentPackageName: 'dev.fleaflet.flutter_map.example', | ||
| tileProvider: CancellableNetworkTileProvider(), | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
| } |
115 changes: 115 additions & 0 deletions
115
example/lib/pages/cancellable_tile_provider/ctp_impl.dart
This file contains hidden or 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,115 @@ | ||
| import 'dart:async'; | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:dio/dio.dart'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/rendering.dart'; | ||
| import 'package:flutter_map/flutter_map.dart'; | ||
| import 'package:http/http.dart'; | ||
| import 'package:http/retry.dart'; | ||
|
|
||
| class CancellableNetworkTileProvider extends TileProvider { | ||
| CancellableNetworkTileProvider({ | ||
| super.headers, | ||
| BaseClient? httpClient, | ||
| }) : httpClient = httpClient ?? RetryClient(Client()); | ||
|
|
||
| final BaseClient httpClient; | ||
|
|
||
| @override | ||
| bool get supportsCancelLoading => true; | ||
|
|
||
| @override | ||
| ImageProvider getImageWithCancelLoadingSupport( | ||
| TileCoordinates coordinates, | ||
| TileLayer options, | ||
| Future<void> cancelLoading, | ||
| ) => | ||
| CancellableNetworkImageProvider( | ||
| url: getTileUrl(coordinates, options), | ||
| fallbackUrl: getTileFallbackUrl(coordinates, options), | ||
| headers: headers, | ||
| httpClient: httpClient, | ||
| cancelLoading: cancelLoading, | ||
| ); | ||
| } | ||
|
|
||
| class CancellableNetworkImageProvider | ||
| extends ImageProvider<CancellableNetworkImageProvider> { | ||
| final String url; | ||
| final String? fallbackUrl; | ||
| final BaseClient httpClient; | ||
| final Map<String, String> headers; | ||
| final Future<void> cancelLoading; | ||
|
|
||
| const CancellableNetworkImageProvider({ | ||
| required this.url, | ||
| required this.fallbackUrl, | ||
| required this.headers, | ||
| required this.httpClient, | ||
| required this.cancelLoading, | ||
| }); | ||
|
|
||
| @override | ||
| ImageStreamCompleter loadImage( | ||
| CancellableNetworkImageProvider key, | ||
| ImageDecoderCallback decode, | ||
| ) { | ||
| final chunkEvents = StreamController<ImageChunkEvent>(); | ||
|
|
||
| return MultiFrameImageStreamCompleter( | ||
| codec: _loadAsync(key, chunkEvents, decode), | ||
| chunkEvents: chunkEvents.stream, | ||
| scale: 1, | ||
| debugLabel: url, | ||
| informationCollector: () => [ | ||
| DiagnosticsProperty('URL', url), | ||
| DiagnosticsProperty('Fallback URL', fallbackUrl), | ||
| DiagnosticsProperty('Current provider', key), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<CancellableNetworkImageProvider> obtainKey( | ||
| ImageConfiguration configuration, | ||
| ) => | ||
| SynchronousFuture<CancellableNetworkImageProvider>(this); | ||
|
|
||
| Future<Codec> _loadAsync( | ||
| CancellableNetworkImageProvider key, | ||
| StreamController<ImageChunkEvent> chunkEvents, | ||
| ImageDecoderCallback decode, { | ||
| bool useFallback = false, | ||
| }) async { | ||
| final cancelToken = CancelToken(); | ||
| cancelLoading.then((_) => cancelToken.cancel()); | ||
|
|
||
| final Uint8List bytes; | ||
| try { | ||
| final dio = Dio(); | ||
| final response = await dio.get<Uint8List>( | ||
| useFallback ? fallbackUrl ?? '' : url, | ||
| cancelToken: cancelToken, | ||
| options: Options( | ||
| headers: headers, | ||
| responseType: ResponseType.bytes, | ||
| ), | ||
| ); | ||
| bytes = response.data!; | ||
| } on DioException catch (err) { | ||
| if (CancelToken.isCancel(err)) { | ||
| return decode( | ||
| await ImmutableBuffer.fromUint8List(TileProvider.transparentImage), | ||
| ); | ||
| } | ||
| if (useFallback || fallbackUrl == null) rethrow; | ||
| return _loadAsync(key, chunkEvents, decode, useFallback: true); | ||
| } catch (_) { | ||
| if (useFallback || fallbackUrl == null) rethrow; | ||
| return _loadAsync(key, chunkEvents, decode, useFallback: true); | ||
| } | ||
|
|
||
| return decode(await ImmutableBuffer.fromUint8List(bytes)); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
| part of 'tile_layer.dart'; | ||
|
|
||
| @Deprecated( | ||
| 'Prefer creating a custom `TileProvider` instead. ' | ||
| 'This option has been deprecated as it is out of scope for the `TileLayer`. ' | ||
| 'This option is deprecated since v6.', | ||
| ) | ||
| typedef TemplateFunction = String Function( | ||
| String str, | ||
| Map<String, String> data, | ||
| ); | ||
|
|
||
| enum EvictErrorTileStrategy { | ||
| /// Never evict images for tiles which failed to load. | ||
| none, | ||
|
|
||
| /// Evict images for tiles which failed to load when they are pruned. | ||
| dispose, | ||
|
|
||
| /// Evict images for tiles which failed to load and: | ||
| /// - do not belong to the current zoom level AND/OR | ||
| /// - are not visible, respecting the pruning buffer (the maximum of the | ||
| /// [keepBuffer] and [panBuffer]. | ||
| notVisibleRespectMargin, | ||
|
|
||
| /// Evict images for tiles which failed to load and: | ||
| /// - do not belong to the current zoom level AND/OR | ||
| /// - are not visible | ||
| notVisible, | ||
| } | ||
|
|
||
| typedef ErrorTileCallBack = void Function( | ||
| TileImage tile, | ||
| Object error, | ||
| StackTrace? stackTrace, | ||
| ); |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.