@@ -20,31 +20,29 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
2020
2121 final String ? cacheDirectory;
2222 final int ? maxCacheSize;
23+ final String Function (String url)? tileKeyGenerator;
2324 final Duration ? overrideFreshAge;
24- final String Function (String url)? cacheKeyGenerator;
2525 final bool readOnly;
2626
2727 @internal
2828 BuiltInMapCachingProviderImpl .createAndInitialise ({
2929 required this .cacheDirectory,
3030 required this .maxCacheSize,
3131 required this .overrideFreshAge,
32- required this .cacheKeyGenerator ,
32+ required this .tileKeyGenerator ,
3333 required this .readOnly,
3434 }) {
3535 // This should only be called/constructed once
3636 () async {
37- if (cacheKeyGenerator == null ) {
37+ if (tileKeyGenerator == null ) {
3838 _uuid = Uuid (goptions: GlobalOptions (MathRNG ()));
3939 }
4040
4141 final cacheDirectoryPath = p.join (
42- this .cacheDirectory ??
43- (await getApplicationCacheDirectory ()).absolute.path,
42+ cacheDirectory ?? (await getApplicationCacheDirectory ()).absolute.path,
4443 'fm_cache' ,
4544 );
46- final cacheDirectory = Directory (cacheDirectoryPath);
47- await cacheDirectory.create (recursive: true );
45+ await Directory (cacheDirectoryPath).create (recursive: true );
4846
4947 final sizeMonitorFilePath =
5048 p.join (cacheDirectoryPath, sizeMonitorFileName);
@@ -59,16 +57,13 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
5957 // monitoring (and potentially run the reducer) if necessary
6058 // Reading does not depend on this.
6159 void sendMessageToWriter (Object message) {
62- if (writerPort != null ) {
63- writerPort.send (message);
64- } else {
65- writerPortReady.future.then ((port) => port.send (message));
66- }
60+ if (writerPort != null ) return writerPort.send (message);
61+ writerPortReady.future.then ((port) => port.send (message));
6762 }
6863
69- _writeTileFile = ({ required path, required metadata, tileBytes} ) =>
70- sendMessageToWriter (
71- (path : path, metadata : metadata, tileBytes : tileBytes) );
64+ _writeTileFile = (path, metadata, tileBytes) => sendMessageToWriter (
65+ (path : path, metadata : metadata, tileBytes : tileBytes),
66+ );
7267 _reportReadFailure = () => sendMessageToWriter (false );
7368
7469 final writerReceivePort = ReceivePort ();
@@ -78,7 +73,7 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
7873 port: writerReceivePort.sendPort,
7974 cacheDirectoryPath: cacheDirectoryPath,
8075 sizeMonitorFilePath: sizeMonitorFilePath,
81- sizeLimit : maxCacheSize,
76+ maxCacheSize : maxCacheSize,
8277 ),
8378 debugName: '[flutter_map: cache] Tile & Size Monitor Writer' ,
8479 );
@@ -93,14 +88,14 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
9388
9489 late final Uuid _uuid; // left un-inited if provided generator
9590
96- late final void Function ({
97- required String path,
98- required CachedMapTileMetadata metadata,
91+ late final void Function (
92+ String path,
93+ CachedMapTileMetadata metadata,
9994 Uint8List ? tileBytes,
100- } ) _writeTileFile;
95+ ) _writeTileFile;
10196
102- /// See `disableSizeMonitor` in worker
103- late final void Function () _reportReadFailure;
97+ late final void Function ()
98+ _reportReadFailure; // See `disableSizeMonitor` in worker
10499
105100 @override
106101 bool get isSupported => true ;
@@ -110,7 +105,7 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
110105 String url,
111106 ) async {
112107 final key =
113- cacheKeyGenerator ? .call (url) ?? _uuid.v5 (Namespace .url.value, url);
108+ tileKeyGenerator ? .call (url) ?? _uuid.v5 (Namespace .url.value, url);
114109 final tileFile = File (
115110 p.join (_cacheDirectoryPath ?? await _cacheDirectoryPathReady.future, key),
116111 );
@@ -121,7 +116,7 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
121116 final bytes = await tileFile.readAsBytes ();
122117
123118 if (bytes.lengthInBytes < 22 ) {
124- throw CachedMapTileReadFailureException (
119+ throw CachedMapTileReadFailure (
125120 url: url,
126121 description:
127122 'cache file (${bytes .lengthInBytes }) was shorter than the '
@@ -145,14 +140,13 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
145140 etag = const AsciiDecoder ().convert (etagBytes);
146141 }
147142
148- // Performing an unaligned read is a hassle
149- final tileBytesExpectedLength =
143+ final tileBytesExpectedLength = // Perform an unaligned read
150144 bytes.buffer.asByteData (18 + etagLength, 4 ).getUint32 (0 , Endian .host);
151145
152146 final tileBytes = Uint8List .sublistView (bytes, 18 + etagLength + 4 );
153147
154148 if (tileBytes.lengthInBytes != tileBytesExpectedLength) {
155- throw CachedMapTileReadFailureException (
149+ throw CachedMapTileReadFailure (
156150 url: url,
157151 description:
158152 'tile image bytes (${tileBytes .lengthInBytes }) were not of '
@@ -168,13 +162,13 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
168162 ),
169163 bytes: tileBytes,
170164 );
171- } on CachedMapTileReadFailureException {
165+ } on CachedMapTileReadFailure {
172166 _reportReadFailure ();
173167 rethrow ;
174168 } catch (error, stackTrace) {
175169 _reportReadFailure ();
176170 Error .throwWithStackTrace (
177- CachedMapTileReadFailureException (url: url, originalError: error),
171+ CachedMapTileReadFailure (url: url, originalError: error),
178172 stackTrace,
179173 );
180174 }
@@ -189,20 +183,22 @@ class BuiltInMapCachingProviderImpl implements BuiltInMapCachingProvider {
189183 if (readOnly) return ;
190184
191185 final key =
192- cacheKeyGenerator ? .call (url) ?? _uuid.v5 (Namespace .url.value, url);
186+ tileKeyGenerator ? .call (url) ?? _uuid.v5 (Namespace .url.value, url);
193187 final path = p.join (
194188 _cacheDirectoryPath ?? await _cacheDirectoryPathReady.future,
195189 key,
196190 );
197191
198- final resolvedMetadata = overrideFreshAge != null
199- ? CachedMapTileMetadata (
200- staleAt: DateTime .timestamp ().add (overrideFreshAge! ),
201- lastModified: metadata.lastModified,
202- etag: metadata.etag,
203- )
204- : metadata;
205-
206- _writeTileFile (path: path, metadata: resolvedMetadata, tileBytes: bytes);
192+ _writeTileFile (
193+ path,
194+ overrideFreshAge != null
195+ ? CachedMapTileMetadata (
196+ staleAt: DateTime .timestamp ().add (overrideFreshAge! ),
197+ lastModified: metadata.lastModified,
198+ etag: metadata.etag,
199+ )
200+ : metadata,
201+ bytes,
202+ );
207203 }
208204}
0 commit comments