Skip to content

Commit

Permalink
Improved example app
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup committed Oct 9, 2024
1 parent a3eae60 commit 458a6c3
Show file tree
Hide file tree
Showing 14 changed files with 542 additions and 248 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart' hide Path;
part of '../download_progress_masker.dart';

class GreyscaleMasker extends SingleChildRenderObjectWidget {
const GreyscaleMasker({
Expand All @@ -16,7 +8,7 @@ class GreyscaleMasker extends SingleChildRenderObjectWidget {
required this.mapCamera,
required this.minZoom,
required this.maxZoom,
this.tileSize = 256,
required this.tileSize,
});

final Stream<TileCoordinates> tileCoordinatesStream;
Expand Down Expand Up @@ -413,7 +405,7 @@ class _GreyscaleMaskerRenderer extends RenderProxyBox {
/*context.canvas.clipRect(Offset.zero & size);
context.canvas.drawColor(
Colors.green,
BlendMode.modulate,
BlendMode.hue,
);*/
},
clipBehavior: Clip.hardEdge,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import 'dart:async';
import 'dart:collection';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart' hide Path;

import 'components/greyscale_masker.dart';
part 'components/greyscale_masker.dart';

class DownloadProgressMasker extends StatefulWidget {
const DownloadProgressMasker({
super.key,
required this.tileCoordinatesStream,
required this.minZoom,
required this.maxZoom,
this.tileSize = 256,
required this.child,
});

final Stream<TileCoordinates>? tileCoordinatesStream;
final int minZoom;
final int maxZoom;
final int tileSize;
final TileLayer child;

@override
Expand All @@ -17,12 +32,27 @@ class DownloadProgressMasker extends StatefulWidget {

class _DownloadProgressMaskerState extends State<DownloadProgressMasker> {
@override
Widget build(
BuildContext
context) => /* GreyscaleMasker(
mapCamera: MapCamera.of(context),
tileMapping: _tileMapping,
child: widget.child,
);*/
Placeholder();
Widget build(BuildContext context) {
if (widget.tileCoordinatesStream case final tcs?) {
return RepaintBoundary(
child: GreyscaleMasker(
/*key: ObjectKey(
(
widget.minZoom,
widget.maxZoom,
widget.tileCoordinatesStream,
widget.tileSize,
),
),*/
mapCamera: MapCamera.of(context),
tileCoordinatesStream: tcs,
minZoom: widget.minZoom,
maxZoom: widget.maxZoom,
tileSize: widget.tileSize,
child: widget.child,
),
);
}
return widget.child;
}
}
36 changes: 12 additions & 24 deletions example/lib/src/screens/main/map_view/map_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
Expand All @@ -11,14 +10,13 @@ import 'package:flutter_map_tile_caching/flutter_map_tile_caching.dart';
import 'package:http/io_client.dart';
import 'package:latlong2/latlong.dart';
import 'package:provider/provider.dart';
import 'package:stream_transform/stream_transform.dart';

import '../../../shared/misc/shared_preferences.dart';
import '../../../shared/misc/store_metadata_keys.dart';
import '../../../shared/state/general_provider.dart';
import '../../../shared/state/region_selection_provider.dart';
import 'components/debugging_tile_builder/debugging_tile_builder.dart';
import 'components/download_progress/components/greyscale_masker.dart';
import 'components/download_progress/download_progress_masker.dart';
import 'components/region_selection/crosshairs.dart';
import 'components/region_selection/custom_polygon_snapping_indicator.dart';
import 'components/region_selection/region_shape.dart';
Expand Down Expand Up @@ -67,7 +65,7 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
},
).distinct(mapEquals);

final _testingCoordsList = [
/*final _testingCoordsList = [
//TileCoordinates(2212, 1468, 12),
//TileCoordinates(2212 * 2, 1468 * 2, 13),
//TileCoordinates(2212 * 2 * 2, 1468 * 2 * 2, 14),
Expand Down Expand Up @@ -122,7 +120,7 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
1468 * 2 * 2 * 2 * 2 * 2 * 2 * 2 + 2,
19,
),
];
];*/

Stream<TileCoordinates>? _testingDownloadTileCoordsStream;

Expand Down Expand Up @@ -177,7 +175,8 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
)
.map(
(event) => event.latestTileEvent.coordinates,
),
)
.asBroadcastStream(),
);
return;
}
Expand Down Expand Up @@ -416,27 +415,16 @@ class _MapViewState extends State<MapView> with TickerProviderStateMixin {
mapController: _mapController.mapController,
options: mapOptions,
children: [
if (_testingDownloadTileCoordsStream == null)
tileLayer
else
RepaintBoundary(
child: Builder(
builder: (context) => GreyscaleMasker(
key: const ValueKey(11),
mapCamera: MapCamera.of(context),
tileCoordinatesStream:
_testingDownloadTileCoordsStream!,
/*tileCoordinates: Stream.periodic(
DownloadProgressMasker(
tileCoordinatesStream: _testingDownloadTileCoordsStream,
/*tileCoordinates: Stream.periodic(
const Duration(seconds: 5),
_testingCoordsList.elementAtOrNull,
).whereNotNull(),*/
minZoom: 11,
maxZoom: 16,
tileSize: 256,
child: tileLayer,
),
),
),
minZoom: 11,
maxZoom: 16,
child: tileLayer,
),
PolygonLayer(
polygons: [
Polygon(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_map_tile_caching/flutter_map_tile_caching.dart';

import '../../../../../../../../shared/misc/store_metadata_keys.dart';

class StoreSelector extends StatefulWidget {
const StoreSelector({
super.key,
this.storeName,
required this.onStoreNameSelected,
});

final String? storeName;
final void Function(String?) onStoreNameSelected;

@override
State<StoreSelector> createState() => _StoreSelectorState();
}

class _StoreSelectorState extends State<StoreSelector> {
late final _storesToTemplatesStream = FMTCRoot.stats
.watchStores(triggerImmediately: true)
.asyncMap(
(_) async => Map.fromEntries(
await Future.wait(
(await FMTCRoot.stats.storesAvailable).map(
(s) async => MapEntry(
s.storeName,
await s.metadata.read.then(
(metadata) => metadata[StoreMetadataKeys.urlTemplate.key],
),
),
),
),
),
)
.distinct(mapEquals);

@override
Widget build(BuildContext context) => LayoutBuilder(
builder: (context, constraints) => SizedBox(
width: constraints.maxWidth,
child: StreamBuilder(
stream: _storesToTemplatesStream,
builder: (context, snapshot) {
if (snapshot.data == null) {
return const Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator.adaptive(),
SizedBox(width: 24),
Text('Loading stores...'),
],
);
}

return DropdownMenu(
dropdownMenuEntries: _constructMenuEntries(snapshot),
onSelected: widget.onStoreNameSelected,
width: constraints.maxWidth,
leadingIcon: const Icon(Icons.inventory),
hintText: 'Select Store',
initialSelection: widget.storeName,
errorText: widget.storeName == null
? 'Select a store to download tiles to'
: null,
inputDecorationTheme: const InputDecorationTheme(
filled: true,
helperMaxLines: 2,
),
);
},
),
),
);

List<DropdownMenuEntry<String>> _constructMenuEntries(
AsyncSnapshot<Map<String, String?>> snapshot,
) =>
snapshot.data!.entries
.whereNot((e) => e.value == null)
.map(
(e) => DropdownMenuEntry(
value: e.key,
label: e.key,
labelWidget: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(e.key),
Text(
Uri.tryParse(e.value!)?.host ?? e.value!,
style: const TextStyle(fontStyle: FontStyle.italic),
),
],
),
),
)
.toList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../../../../shared/state/download_configuration_provider.dart';
import '../../../../../../../shared/state/region_selection_provider.dart';
import 'components/store_selector.dart';

class ConfigOptions extends StatefulWidget {
const ConfigOptions({super.key});
Expand All @@ -14,6 +14,9 @@ class ConfigOptions extends StatefulWidget {
class _ConfigOptionsState extends State<ConfigOptions> {
@override
Widget build(BuildContext context) {
final storeName = context.select<DownloadConfigurationProvider, String?>(
(p) => p.selectedStoreName,
);
final minZoom =
context.select<DownloadConfigurationProvider, int>((p) => p.minZoom);
final maxZoom =
Expand All @@ -28,6 +31,13 @@ class _ConfigOptionsState extends State<ConfigOptions> {
return SingleChildScrollView(
child: Column(
children: [
StoreSelector(
storeName: storeName,
onStoreNameSelected: (storeName) => context
.read<DownloadConfigurationProvider>()
.selectedStoreName = storeName,
),
const Divider(height: 24),
Row(
children: [
const Tooltip(message: 'Zoom Levels', child: Icon(Icons.search)),
Expand All @@ -46,7 +56,7 @@ class _ConfigOptionsState extends State<ConfigOptions> {
),
],
),
const Divider(),
const Divider(height: 24),
Row(
children: [
const Tooltip(
Expand All @@ -58,8 +68,9 @@ class _ConfigOptionsState extends State<ConfigOptions> {
child: Slider(
value: parallelThreads.toDouble(),
label: '$parallelThreads threads',
min: 1,
max: 10,
divisions: 10,
divisions: 9,
onChanged: (r) => context
.read<DownloadConfigurationProvider>()
.parallelThreads = r.toInt(),
Expand Down Expand Up @@ -112,6 +123,29 @@ class _ConfigOptionsState extends State<ConfigOptions> {
),
],
),
const Divider(height: 24),
Row(
children: [
const Icon(Icons.skip_next),
const SizedBox(width: 4),
const Icon(Icons.file_copy),
const SizedBox(width: 12),
const Text('Skip Existing Tiles'),
const Spacer(),
Switch.adaptive(value: true, onChanged: (value) {}),
],
),
Row(
children: [
const Icon(Icons.skip_next),
const SizedBox(width: 4),
const Icon(Icons.waves),
const SizedBox(width: 12),
const Text('Skip Sea Tiles'),
const Spacer(),
Switch.adaptive(value: true, onChanged: (value) {}),
],
),
],
),
);
Expand Down
Loading

0 comments on commit 458a6c3

Please sign in to comment.