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

WIP: Evict error tiles #577

Merged
merged 4 commits into from
Mar 16, 2021
Merged
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 example/lib/pages/wms_tile_layer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong/latlong.dart';

import '../widgets/drawer.dart';

class WMSLayerPage extends StatelessWidget {
Expand Down
78 changes: 72 additions & 6 deletions lib/src/layer/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ import 'package:tuple/tuple.dart';

import 'layer.dart';

enum EvictErrorTileStrategy {
// never evict error Tiles
none,
// evict error Tiles during _pruneTiles / _abortLoading calls
dispose,
// evict error Tiles which are not visible anymore but respect margin (see keepBuffer option)
// (Tile's zoom level not equals current _tileZoom or Tile is out of viewport)
notVisibleRespectMargin,
// evict error Tiles which are not visible anymore
// (Tile's zoom level not equals current _tileZoom or Tile is out of viewport)
notVisible,
}

typedef ErrorTileCallBack = void Function(Tile tile, dynamic error);

/// Describes the needed properties to create a tile-based layer.
Expand Down Expand Up @@ -189,6 +202,11 @@ class TileLayerOptions extends LayerOptions {
/// This callback will be execute if some errors by getting tile
final ErrorTileCallBack errorTileCallback;

// If a Tile was loaded with error and if strategy isn't `none` then TileProvider
// will be asked to evict Image based on current strategy
// (see #576 - even Error Images are cached in flutter)
final EvictErrorTileStrategy evictErrorTileStrategy;

TileLayerOptions({
Key key,
this.urlTemplate,
Expand Down Expand Up @@ -224,6 +242,7 @@ class TileLayerOptions extends LayerOptions {
this.overrideTilesWhenUrlChanges = false,
this.retinaMode = false,
this.errorTileCallback,
this.evictErrorTileStrategy = EvictErrorTileStrategy.none,
rebuild,
}) : updateInterval =
updateInterval <= 0 ? null : Duration(milliseconds: updateInterval),
Expand Down Expand Up @@ -525,7 +544,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
var tile = _tiles[key];

tile.tileReady = null;
tile.dispose();
tile.dispose(tile.loadError &&
options.evictErrorTileStrategy != EvictErrorTileStrategy.none);
_tiles.remove(key);
}
}
Expand Down Expand Up @@ -884,6 +904,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
}
}

_evictErrorTilesBasedOnStrategy(tileRange);

// sort tile queue to load tiles in order of their distance to center
queue.sort((a, b) =>
(a.distanceTo(tileCenter) - b.distanceTo(tileCenter)).toInt());
Expand Down Expand Up @@ -929,7 +951,8 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
return;
}

tile.dispose();
tile.dispose(tile.loadError &&
options.evictErrorTileStrategy != EvictErrorTileStrategy.none);
_tiles.remove(key);
}

Expand All @@ -949,6 +972,46 @@ class _TileLayerState extends State<TileLayer> with TickerProviderStateMixin {
tile.loadTileImage();
}

void _evictErrorTilesBasedOnStrategy(Bounds tileRange) {
if (options.evictErrorTileStrategy ==
EvictErrorTileStrategy.notVisibleRespectMargin) {
var toRemove = <String>[];
for (var entry in _tiles.entries) {
var tile = entry.value;

if (tile.loadError && !tile.current) {
toRemove.add(entry.key);
}
}

for (var key in toRemove) {
var tile = _tiles[key];

tile.dispose(true);
_tiles.remove(key);
}
} else if (options.evictErrorTileStrategy ==
EvictErrorTileStrategy.notVisible) {
var toRemove = <String>[];
for (var entry in _tiles.entries) {
var tile = entry.value;
var c = tile.coords;

if (tile.loadError &&
(!tile.current || !tileRange.contains(CustomPoint(c.x, c.y)))) {
toRemove.add(entry.key);
}
}

for (var key in toRemove) {
var tile = _tiles[key];

tile.dispose(true);
_tiles.remove(key);
}
}
}

void _tileReady(Coords<double> coords, dynamic error, Tile tile) {
if (null != error) {
print(error);
Expand Down Expand Up @@ -1101,10 +1164,13 @@ class Tile implements Comparable<Tile> {
// call this before GC!
void dispose([bool evict = false]) {
if (evict && imageProvider != null) {
imageProvider
.evict()
.then((bool succ) => print('evict tile: $coords -> $succ'))
.catchError((error) => print('evict tile: $coords -> $error'));
try {
imageProvider.evict().catchError(print);
} catch (e) {
// this may be never called because catchError will handle errors, however
// we want to avoid random crashes like in #444 / #536
print(e);
}
}

animationController?.removeStatusListener(_onAnimateEnd);
Expand Down