-
-
Notifications
You must be signed in to change notification settings - Fork 860
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added cancellation support to
TileProvider
and surrounding mechanis…
…ms (#1622) * Added cancellation support to `TileProvider` and surrounding mechanisms Cleanup `TileProvider` interface * Removed duplicate import * Improved documentation Close `NetworkTileProvider.httpClient` in `dispose` * Added example for cancellable `TileProvider`
- Loading branch information
1 parent
e5a7ec7
commit 40d213f
Showing
13 changed files
with
580 additions
and
119 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
51 changes: 51 additions & 0 deletions
51
example/lib/pages/cancellable_tile_provider/cancellable_tile_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,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 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 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 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 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 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 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.