Skip to content

Commit

Permalink
Renaming of some identifiers
Browse files Browse the repository at this point in the history
Documentation improvements
Added checks to ensure store exists before attempting to write to it
Improved key correctness for image provider
Deprecated `FMTCStore.getTileProvider`
  • Loading branch information
JaffaKetchup committed Dec 30, 2024
1 parent 1a26587 commit 491924e
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 179 deletions.
6 changes: 4 additions & 2 deletions example/lib/src/screens/main/map_view/map_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import '../../../shared/misc/store_metadata_keys.dart';
import '../../../shared/state/download_provider.dart';
import '../../../shared/state/general_provider.dart';
import '../../../shared/state/region_selection_provider.dart';
import '../../../shared/state/selected_tab_state.dart';
import 'components/additional_overlay/additional_overlay.dart';
import 'components/debugging_tile_builder/debugging_tile_builder.dart';
import 'components/download_progress/download_progress_masker.dart';
Expand Down Expand Up @@ -312,8 +313,8 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
maxNativeZoom: 20,
tileProvider: widget.mode != MapViewMode.standard
? NetworkTileProvider()
: FMTCTileProvider.multipleStores(
storeNames: compiledStoreNames,
: FMTCTileProvider(
stores: compiledStoreNames,
otherStoresStrategy: otherStoresStrategy,
loadingStrategy: provider.loadingStrategy,
useOtherStoresAsFallbackOnly:
Expand All @@ -340,6 +341,7 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
context.select<DownloadingProvider, bool>((p) => p.isFocused);

final map = FlutterMap(
key: ValueKey(selectedTabState.value),
mapController: _mapController.mapController,
options: mapOptions,
children: [
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_map_tile_caching.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ part 'src/providers/tile_loading_interceptor/result.dart';
part 'src/providers/tile_loading_interceptor/map_typedef.dart';
part 'src/providers/tile_loading_interceptor/result_path.dart';
part 'src/providers/image_provider/image_provider.dart';
part 'src/providers/image_provider/internal_get_bytes.dart';
part 'src/providers/image_provider/internal_tile_browser.dart';
part 'src/providers/tile_provider/custom_user_agent_compat_map.dart';
part 'src/providers/tile_provider/strategies.dart';
part 'src/providers/tile_provider/tile_provider.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ Map<String, bool> _sharedWriteSingleTile({
final storesBox = root.box<ObjectBoxStore>();
final rootBox = root.box<ObjectBoxRoot>();

final availableStoreNames = storesBox.getAll().map((e) => e.name);

for (final storeName in storeNames) {
if (!availableStoreNames.contains(storeName)) {
throw StoreNotExists(storeName: storeName);
}
}

final compiledStoreNames = writeAllNotIn == null
? storeNames
: [
...storeNames,
...storesBox.getAll().map((e) => e.name).where(
(e) => !writeAllNotIn.contains(e) && !storeNames.contains(e),
),
...availableStoreNames.whereNot(
(e) => writeAllNotIn.contains(e) || storeNames.contains(e),
),
];

final tilesQuery = tiles.query(ObjectBoxTile_.url.equals(url)).build();
Expand Down
20 changes: 0 additions & 20 deletions lib/src/backend/interfaces/models.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Copyright © Luka S (JaffaKetchup) under GPL-v3
// A full license can be found at .\LICENSE

// TODO: Maybe bad design - do we really want inheritance?
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes

import 'dart:typed_data';

import 'package:meta/meta.dart';

import '../../../flutter_map_tile_caching.dart';

/// Represents a tile (which is never directly exposed to the user)
Expand All @@ -28,18 +22,4 @@ abstract base class BackendTile {

/// The raw bytes of the image of this tile
Uint8List get bytes;

/// Uses [url] for equality comparisons only (unless the two objects are
/// [identical])
///
/// Overriding this in an implementation may cause FMTC logic to break, and is
/// therefore not recommended.
@override
@nonVirtual
bool operator ==(Object other) =>
identical(this, other) || (other is BackendTile && url == other.url);

@override
@nonVirtual
int get hashCode => url.hashCode;
}
6 changes: 0 additions & 6 deletions lib/src/providers/image_provider/browsing_errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import 'package:flutter_map/flutter_map.dart';
import 'package:http/http.dart';
import 'package:http/io_client.dart';
import 'package:meta/meta.dart';

import '../../../flutter_map_tile_caching.dart';
Expand Down Expand Up @@ -109,11 +108,6 @@ enum FMTCBrowsingErrorType {
/// Failed to load the tile from the cache or network because it was missing
/// from the cache and there was an unexpected error when requesting from the
/// server
///
/// Try specifying a normal HTTP/1.1 [IOClient] when using
/// [FMTCStore.getTileProvider]. Check that the [TileLayer.urlTemplate] is
/// correct, that any necessary authorization data is correctly included, and
/// that the server serves the viewed region.
unknownFetchException(
'Failed to load the tile from the cache or network because it was missing '
'from the cache and there was an unexpected error when requesting from '
Expand Down
23 changes: 12 additions & 11 deletions lib/src/providers/image_provider/image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _FMTCImageProvider extends ImageProvider<_FMTCImageProvider> {
ImageDecoderCallback decode,
) =>
MultiFrameImageStreamCompleter(
codec: getBytes(
codec: provideTile(
coords: coords,
options: options,
provider: provider,
Expand All @@ -59,19 +59,19 @@ class _FMTCImageProvider extends ImageProvider<_FMTCImageProvider> {
final tileUrl = provider.getTileUrl(coords, options);

return [
DiagnosticsProperty('Store names', provider.storeNames),
DiagnosticsProperty('Stores', provider.stores),
DiagnosticsProperty('Tile coordinates', coords),
DiagnosticsProperty('Tile URL', tileUrl),
DiagnosticsProperty(
'Tile storage-suitable UID',
provider.urlTransformer(tileUrl),
provider.urlTransformer?.call(tileUrl) ?? tileUrl,
),
];
},
);

/// {@macro fmtc.tileProvider.getBytes}
static Future<Uint8List> getBytes({
/// {@macro fmtc.tileProvider.provideTile}
static Future<Uint8List> provideTile({
required TileCoordinates coords,
required TileLayer options,
required FMTCTileProvider provider,
Expand All @@ -80,6 +80,8 @@ class _FMTCImageProvider extends ImageProvider<_FMTCImageProvider> {
void Function()? finishedLoadingBytes,
bool requireValidImage = false,
}) async {
startedLoading?.call();

final currentTLIR =
provider.tileLoadingInterceptor != null ? _TLIRConstructor._() : null;

Expand Down Expand Up @@ -116,11 +118,9 @@ class _FMTCImageProvider extends ImageProvider<_FMTCImageProvider> {
}
}

startedLoading?.call();

final Uint8List bytes;
try {
bytes = await _internalGetBytes(
bytes = await _internalTileBrowser(
coords: coords,
options: options,
provider: provider,
Expand All @@ -146,12 +146,13 @@ class _FMTCImageProvider extends ImageProvider<_FMTCImageProvider> {
Future<_FMTCImageProvider> obtainKey(ImageConfiguration configuration) =>
SynchronousFuture(this);

// TODO: Incorporate tile provider & tile layer options
@override
bool operator ==(Object other) =>
identical(this, other) ||
(other is _FMTCImageProvider && other.coords == coords);
(other is _FMTCImageProvider &&
other.coords == coords &&
other.provider == provider);

@override
int get hashCode => coords.hashCode;
int get hashCode => Object.hash(coords, provider);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

part of '../../../flutter_map_tile_caching.dart';

Future<Uint8List> _internalGetBytes({
Future<Uint8List> _internalTileBrowser({
required TileCoordinates coords,
required TileLayer options,
required FMTCTileProvider provider,
Expand All @@ -29,7 +29,7 @@ Future<Uint8List> _internalGetBytes({
}

final networkUrl = provider.getTileUrl(coords, options);
final matcherUrl = provider.urlTransformer(networkUrl);
final matcherUrl = provider.urlTransformer?.call(networkUrl) ?? networkUrl;

currentTLIR?.networkUrl = networkUrl;
currentTLIR?.storageSuitableUID = matcherUrl;
Expand All @@ -55,7 +55,7 @@ Future<Uint8List> _internalGetBytes({

final tileRetrievableFromOtherStoresAsFallback = existingTile != null &&
provider.useOtherStoresAsFallbackOnly &&
provider.storeNames.keys
provider.stores.keys
.toSet()
.intersection(allExistingStores.toSet())
.isEmpty;
Expand Down Expand Up @@ -206,7 +206,7 @@ Future<Uint8List> _internalGetBytes({
// their read/write settings
// At this point, we've downloaded the tile anyway, so we might as well
// write the stores that allow it, even if the existing tile hasn't expired
final writeTileToSpecified = provider.storeNames.entries
final writeTileToSpecified = provider.stores.entries
.where(
(e) => switch (e.value) {
null => false,
Expand All @@ -223,7 +223,7 @@ Future<Uint8List> _internalGetBytes({
existingTile != null
? writeTileToSpecified.followedBy(
intersectedExistingStores
.whereNot((e) => provider.storeNames.containsKey(e)),
.whereNot((e) => provider.stores.containsKey(e)),
)
: writeTileToSpecified)
.toSet()
Expand All @@ -236,7 +236,7 @@ Future<Uint8List> _internalGetBytes({
storeNames: writeTileToIntermediate,
writeAllNotIn:
provider.otherStoresStrategy == BrowseStoreStrategy.readUpdateCreate
? provider.storeNames.keys.toList(growable: false)
? provider.stores.keys.toList(growable: false)
: null,
url: matcherUrl,
bytes: response.bodyBytes,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/providers/tile_loading_interceptor/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

part of '../../../flutter_map_tile_caching.dart';

/// A 'temporary' object that collects information from [_internalGetBytes] to
/// be used to construct a [TileLoadingInterceptorResult]
/// A 'temporary' object that collects information from [_internalTileBrowser]
/// to be used to construct a [TileLoadingInterceptorResult]
///
/// See documentation on [TileLoadingInterceptorResult] for more information
class _TLIRConstructor {
Expand All @@ -24,7 +24,7 @@ class _TLIRConstructor {
}

/// Information useful to debug and record detailed statistics for the loading
/// mechanisms and paths of a tile
/// mechanisms and paths of a browsed tile load
@immutable
class TileLoadingInterceptorResult {
const TileLoadingInterceptorResult._({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class _CustomUserAgentCompatMap extends MapView<String, String> {
///
/// The identifying mark is injected to seperate traffic sent via FMTC from
/// standard flutter_map traffic, as it significantly changes the behaviour of
/// tile retrieval, and could generate more traffic.
/// tile retrieval.
@override
String putIfAbsent(String key, String Function() ifAbsent) {
if (key != 'User-Agent') return super.putIfAbsent(key, ifAbsent);
Expand Down
9 changes: 8 additions & 1 deletion lib/src/providers/tile_provider/strategies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
part of '../../../flutter_map_tile_caching.dart';

/// Alias for [BrowseLoadingStrategy], to ease migration from v9 -> v10
@Deprecated('`CacheBehavior` has been renamed to `BrowseLoadingStrategy`')
@Deprecated(
'Rename all references to `BrowseLoadingStrategy` instead. '
'The new name is less ambiguous in the context of the new '
'`BrowseStoreStrategy`, and does not depend on a British or American '
'spelling. '
'This feature was deprecated in v10, and will be removed in a future '
'version.',
)
typedef CacheBehavior = BrowseLoadingStrategy;

/// Determines whether the network or cache is preferred during browse caching,
Expand Down
Loading

0 comments on commit 491924e

Please sign in to comment.