Skip to content

Commit

Permalink
Added readLatestTile method to backend
Browse files Browse the repository at this point in the history
Completed frontend store stats & management methods connection to backend
  • Loading branch information
JaffaKetchup committed Jan 4, 2024
1 parent 6fc0864 commit 6b45a39
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 84 deletions.
9 changes: 9 additions & 0 deletions lib/src/backend/impls/objectbox/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
args: {'url': url},
))!['tile'];

@override
Future<ObjectBoxTile?> readLatestTile({
required String storeName,
}) async =>
(await _sendCmd(
type: _WorkerCmdType.readLatestTile,
args: {'storeName': storeName},
))!['tile'];

@override
Future<void> writeTile({
required String storeName,
Expand Down
19 changes: 19 additions & 0 deletions lib/src/backend/impls/objectbox/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum _WorkerCmdType {
getStoreMisses,
tileExistsInStore,
readTile,
readLatestTile,
writeTile,
deleteTile,
removeOldestTilesAboveLimit,
Expand Down Expand Up @@ -330,6 +331,24 @@ Future<void> _worker(

query.close();

break;
case _WorkerCmdType.readLatestTile:
final storeName = cmd.args['storeName']! as String;

final query = (root
.box<ObjectBoxTile>()
.query()
.order(ObjectBoxTile_.lastModified, flags: Order.descending)
..linkMany(
ObjectBoxTile_.stores,
ObjectBoxStore_.name.equals(storeName),
))
.build();

sendRes(id: cmd.id, data: {'tile': query.findFirst()});

query.close();

break;
case _WorkerCmdType.writeTile:
final storeName = cmd.args['storeName']! as String;
Expand Down
8 changes: 8 additions & 0 deletions lib/src/backend/interfaces/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ abstract interface class FMTCBackendInternal {
required String url,
});

/// {@template fmtc.backend.readLatestTile}
/// Retrieve the tile most recently modified in the specified store, if any
/// tiles exist
/// {@endtemplate}
Future<BackendTile?> readLatestTile({
required String storeName,
});

/// Create or update a tile (given a [url] and its [bytes]) in the specified
/// store
///
Expand Down
88 changes: 4 additions & 84 deletions lib/src/store/manage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ part of flutter_map_tile_caching;
///
/// If the store is not in the expected state (of existence) when invoking an
/// operation, then an error will be thrown (likely [StoreNotExists] or
/// [StoreAlreadyExists]). It is recommended to check [ready] or [readySync] when
/// necessary.
/// [StoreAlreadyExists]). It is recommended to check [ready] when necessary.
final class StoreManagement extends _WithBackendAccess {
const StoreManagement._(super.store);

Expand Down Expand Up @@ -43,82 +42,8 @@ final class StoreManagement extends _WithBackendAccess {
Future<void> removeTilesOlderThan({required DateTime expiry}) =>
_backend.removeTilesOlderThan(storeName: _storeName, expiry: expiry);

/// Retrieves the most recently modified tile from the store, extracts it's
/// bytes, and renders them to an [Image]
///
/// Prefer [tileImageAsync] to avoid blocking the UI thread. Otherwise, this
/// has slightly better performance.
///
/// Eventually returns `null` if there are no cached tiles in this store,
/// otherwise an [Image] with [size] height and width.
///
/// This method requires the store to be [ready], else an [FMTCStoreNotReady]
/// error will be raised.
Image? tileImage({
double? size,
Key? key,
double scale = 1.0,
ImageFrameBuilder? frameBuilder,
ImageErrorWidgetBuilder? errorBuilder,
String? semanticLabel,
bool excludeFromSemantics = false,
Color? color,
Animation<double>? opacity,
BlendMode? colorBlendMode,
BoxFit? fit,
AlignmentGeometry alignment = Alignment.center,
ImageRepeat repeat = ImageRepeat.noRepeat,
Rect? centerSlice,
bool matchTextDirection = false,
bool gaplessPlayback = false,
bool isAntiAlias = false,
FilterQuality filterQuality = FilterQuality.low,
int? cacheWidth,
int? cacheHeight,
}) {
final latestTile = _registry(_name)
.tiles
.where(sort: Sort.desc)
.anyLastModified()
.limit(1)
.findFirstSync();
if (latestTile == null) return null;

return Image.memory(
Uint8List.fromList(latestTile.bytes),
key: key,
scale: scale,
frameBuilder: frameBuilder,
errorBuilder: errorBuilder,
semanticLabel: semanticLabel,
excludeFromSemantics: excludeFromSemantics,
width: size,
height: size,
color: color,
opacity: opacity,
colorBlendMode: colorBlendMode,
fit: fit,
alignment: alignment,
repeat: repeat,
centerSlice: centerSlice,
matchTextDirection: matchTextDirection,
gaplessPlayback: gaplessPlayback,
isAntiAlias: isAntiAlias,
filterQuality: filterQuality,
cacheWidth: cacheWidth,
cacheHeight: cacheHeight,
);
}

/// Retrieves the most recently modified tile from the store, extracts it's
/// bytes, and renders them to an [Image]
///
/// Eventually returns `null` if there are no cached tiles in this store,
/// otherwise an [Image] with [size] height and width.
///
/// This method requires the store to be [ready], else an [FMTCStoreNotReady]
/// error will be raised.
///
/// {@macro fmtc.backend.readLatestTile}
/// , then render the bytes to an [Image]
Future<Image?> tileImageAsync({
double? size,
Key? key,
Expand All @@ -141,12 +66,7 @@ final class StoreManagement extends _WithBackendAccess {
int? cacheWidth,
int? cacheHeight,
}) async {
final latestTile = await _registry(_name)
.tiles
.where(sort: Sort.desc)
.anyLastModified()
.limit(1)
.findFirst();
final latestTile = await _backend.readLatestTile(storeName: _storeName);
if (latestTile == null) return null;

return Image.memory(
Expand Down

0 comments on commit 6b45a39

Please sign in to comment.