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 16, 2024
1 parent c5c223c commit 699b130
Show file tree
Hide file tree
Showing 32 changed files with 817 additions and 1,582 deletions.
23 changes: 4 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import 'src/screens/import/import.dart';
import 'src/screens/initialisation_error/initialisation_error.dart';
import 'src/screens/main/main.dart';
import 'src/screens/main/secondary_view/contents/home/components/stores_list/state/export_selection_provider.dart';
import 'src/screens/old/configure_download/configure_download.dart';
import 'src/screens/old/download/download.dart';
import 'src/screens/store_editor/store_editor.dart';
import 'src/shared/misc/shared_preferences.dart';
import 'src/shared/state/download_configuration_provider.dart';
import 'src/shared/state/download_provider.dart';
import 'src/shared/state/general_provider.dart';
import 'src/shared/state/region_selection_provider.dart';

Expand Down Expand Up @@ -63,22 +62,6 @@ class _AppContainer extends StatelessWidget {
fullscreenDialog: true,
),
),
ConfigureDownloadPopup.route: (
std: null,
custom: (context, settings) => MaterialPageRoute(
builder: (context) => const ConfigureDownloadPopup(),
settings: settings,
fullscreenDialog: true,
),
),
DownloadPopup.route: (
std: null,
custom: (context, settings) => MaterialPageRoute(
builder: (context) => const DownloadPopup(),
settings: settings,
fullscreenDialog: true,
),
),
};

@override
Expand Down Expand Up @@ -119,7 +102,9 @@ class _AppContainer extends StatelessWidget {
),
ChangeNotifierProvider(
create: (_) => DownloadConfigurationProvider(),
//lazy: true,
),
ChangeNotifierProvider(
create: (_) => DownloadingProvider(),
),
],
child: MaterialApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class InitialisationError extends StatelessWidget {
await dir.delete(recursive: true);
} on FileSystemException {
showFailure();
rethrow;
return;
}

runApp(const SizedBox.shrink()); // Destroy current app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ class _DownloadProgressMaskerState extends State<DownloadProgressMasker> {
child: GreyscaleMasker(
mapCamera: MapCamera.of(context),
tileCoordinatesStream: dps
.where((e) => !e.latestTileEvent.isRepeat)
.map((e) => e.latestTileEvent.coordinates),
.where(
(e) =>
e.latestTileEvent != null && !e.latestTileEvent!.isRepeat,
)
.map((e) => e.latestTileEvent!.coordinates),
minZoom: widget.minZoom,
maxZoom: widget.maxZoom,
tileSize: widget.tileSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RegionShape extends StatelessWidget {
color: Theme.of(context)
.colorScheme
.surface
.withValues(alpha: 0.5),
.withAlpha(255 ~/ 2),
),
],
),
Expand All @@ -88,7 +88,7 @@ class RegionShape extends StatelessWidget {
bounds.southEast,
bounds.southWest,
],
color: color.toColor().withValues(alpha: 0.7),
color: color.toColor().withAlpha(255 ~/ 2),
),
],
),
Expand All @@ -98,7 +98,7 @@ class RegionShape extends StatelessWidget {
point: center,
radius: radius * 1000,
useRadiusInMeter: true,
color: color.toColor().withValues(alpha: 0.7),
color: color.toColor().withAlpha(255 ~/ 2),
),
],
),
Expand All @@ -108,7 +108,7 @@ class RegionShape extends StatelessWidget {
.map(
(o) => Polygon(
points: o,
color: color.toColor().withValues(alpha: 0.7),
color: color.toColor().withAlpha(255 ~/ 2),
),
)
.toList(growable: false),
Expand All @@ -117,7 +117,7 @@ class RegionShape extends StatelessWidget {
polygons: [
Polygon(
points: outline,
color: color.toColor().withValues(alpha: 0.7),
color: color.toColor().withAlpha(255 ~/ 2),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class _ConfigOptionsState extends State<ConfigOptions> {
context.select<DownloadConfigurationProvider, int>((p) => p.rateLimit);
final maxBufferLength = context
.select<DownloadConfigurationProvider, int>((p) => p.maxBufferLength);
final skipExistingTiles =
context.select<DownloadConfigurationProvider, bool>(
(p) => p.skipExistingTiles,
);
final skipSeaTiles = context
.select<DownloadConfigurationProvider, bool>((p) => p.skipSeaTiles);

return SingleChildScrollView(
child: Column(
Expand Down Expand Up @@ -151,7 +157,12 @@ class _ConfigOptionsState extends State<ConfigOptions> {
const SizedBox(width: 12),
const Text('Skip Existing Tiles'),
const Spacer(),
Switch.adaptive(value: true, onChanged: (value) {}),
Switch.adaptive(
value: skipExistingTiles,
onChanged: (v) => context
.read<DownloadConfigurationProvider>()
.skipExistingTiles = v,
),
],
),
Row(
Expand All @@ -162,7 +173,12 @@ class _ConfigOptionsState extends State<ConfigOptions> {
const SizedBox(width: 12),
const Text('Skip Sea Tiles'),
const Spacer(),
Switch.adaptive(value: true, onChanged: (value) {}),
Switch.adaptive(
value: skipSeaTiles,
onChanged: (v) => context
.read<DownloadConfigurationProvider>()
.skipSeaTiles = v,
),
],
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:flutter_map_tile_caching/flutter_map_tile_caching.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import '../../../../../../../shared/misc/store_metadata_keys.dart';
import '../../../../../../../shared/state/download_configuration_provider.dart';
import '../../../../../../../shared/state/download_provider.dart';
import '../../../../../../../shared/state/region_selection_provider.dart';

class ConfirmationPanel extends StatefulWidget {
Expand All @@ -15,16 +17,23 @@ class ConfirmationPanel extends StatefulWidget {
}

class _ConfirmationPanelState extends State<ConfirmationPanel> {
DownloadableRegion<MultiRegion>? _prevDownloadableRegion;
DownloadableRegion<MultiRegion>? _prevTileCountableRegion;
late Future<int> _tileCount;

void _updateTileCount() {
_tileCount = const FMTCStore('').download.check(_prevDownloadableRegion!);
setState(() {});
}
bool _loadingDownloader = false;

@override
Widget build(BuildContext context) {
final regions = context
.select<RegionSelectionProvider, Map<BaseRegion, HSLColor>>(
(p) => p.constructedRegions,
)
.keys
.toList(growable: false);
final minZoom =
context.select<DownloadConfigurationProvider, int>((p) => p.minZoom);
final maxZoom =
context.select<DownloadConfigurationProvider, int>((p) => p.maxZoom);
final startTile =
context.select<DownloadConfigurationProvider, int>((p) => p.startTile);
final endTile =
Expand All @@ -35,31 +44,21 @@ class _ConfirmationPanelState extends State<ConfirmationPanel> {
) !=
null;

// Not suitable for download!
final downloadableRegion = MultiRegion(
context
.select<RegionSelectionProvider, Map<BaseRegion, HSLColor>>(
(p) => p.constructedRegions,
)
.keys
.toList(growable: false),
).toDownloadable(
minZoom:
context.select<DownloadConfigurationProvider, int>((p) => p.minZoom),
maxZoom:
context.select<DownloadConfigurationProvider, int>((p) => p.maxZoom),
final tileCountableRegion = MultiRegion(regions).toDownloadable(
minZoom: minZoom,
maxZoom: maxZoom,
start: startTile,
end: endTile,
options: TileLayer(),
);
if (_prevDownloadableRegion == null ||
downloadableRegion.originalRegion !=
_prevDownloadableRegion!.originalRegion ||
downloadableRegion.minZoom != _prevDownloadableRegion!.minZoom ||
downloadableRegion.maxZoom != _prevDownloadableRegion!.maxZoom ||
downloadableRegion.start != _prevDownloadableRegion!.start ||
downloadableRegion.end != _prevDownloadableRegion!.end) {
_prevDownloadableRegion = downloadableRegion;
if (_prevTileCountableRegion == null ||
tileCountableRegion.originalRegion !=
_prevTileCountableRegion!.originalRegion ||
tileCountableRegion.minZoom != _prevTileCountableRegion!.minZoom ||
tileCountableRegion.maxZoom != _prevTileCountableRegion!.maxZoom ||
tileCountableRegion.start != _prevTileCountableRegion!.start ||
tileCountableRegion.end != _prevTileCountableRegion!.end) {
_prevTileCountableRegion = tileCountableRegion;
_updateTileCount();
}

Expand Down Expand Up @@ -179,13 +178,71 @@ class _ConfirmationPanelState extends State<ConfirmationPanel> {
height: 46,
width: double.infinity,
child: FilledButton.icon(
onPressed: !hasSelectedStoreName ? null : () {},
label: const Text('Start Download'),
icon: const Icon(Icons.download),
onPressed: !hasSelectedStoreName || _loadingDownloader
? null
: _startDownload,
label: _loadingDownloader
? const SizedBox.square(
dimension: 24,
child: CircularProgressIndicator.adaptive(),
)
: const Text('Start Download'),
icon: _loadingDownloader ? null : const Icon(Icons.download),
),
),
],
),
);
}

void _updateTileCount() {
_tileCount = const FMTCStore('').download.check(_prevTileCountableRegion!);
setState(() {});
}

Future<void> _startDownload() async {
setState(() => _loadingDownloader = true);

final downloadingProvider = context.read<DownloadingProvider>();
final regionSelection = context.read<RegionSelectionProvider>();
final downloadConfiguration = context.read<DownloadConfigurationProvider>();

final store = FMTCStore(downloadConfiguration.selectedStoreName!);
final urlTemplate =
(await store.metadata.read)[StoreMetadataKeys.urlTemplate.key];

if (!mounted) return;

final downloadableRegion = MultiRegion(
regionSelection.constructedRegions.keys.toList(growable: false),
).toDownloadable(
minZoom: downloadConfiguration.minZoom,
maxZoom: downloadConfiguration.maxZoom,
start: downloadConfiguration.startTile,
end: downloadConfiguration.endTile,
options: TileLayer(
urlTemplate: urlTemplate,
userAgentPackageName: 'dev.jaffaketchup.fmtc.demo',
),
);

final downloadStream = store.download.startForeground(
region: downloadableRegion,
parallelThreads: downloadConfiguration.parallelThreads,
maxBufferLength: downloadConfiguration.maxBufferLength,
skipExistingTiles: downloadConfiguration.skipExistingTiles,
skipSeaTiles: downloadConfiguration.skipSeaTiles,
rateLimit: downloadConfiguration.rateLimit,
);

downloadingProvider.assignDownload(
storeName: downloadConfiguration.selectedStoreName!,
downloadableRegion: downloadableRegion,
stream: downloadStream,
);

// The downloading view is switched to by `assignDownload`, when the first
// event is recieved from the stream (indicating the preparation is
// complete and the download is starting).
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../../../shared/state/download_provider.dart';

class ConfirmCancellationDialog extends StatefulWidget {
const ConfirmCancellationDialog({super.key});

@override
State<ConfirmCancellationDialog> createState() =>
_ConfirmCancellationDialogState();
}

class _ConfirmCancellationDialogState extends State<ConfirmCancellationDialog> {
bool _isCancelling = false;

@override
Widget build(BuildContext context) => AlertDialog.adaptive(
icon: const Icon(Icons.cancel),
title: const Text('Cancel download?'),
content: const Text('Any tiles already downloaded will not be removed'),
actions: _isCancelling
? [const CircularProgressIndicator.adaptive()]
: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Continue download'),
),
FilledButton(
onPressed: () async {
setState(() => _isCancelling = true);
await context.read<DownloadingProvider>().cancel();
if (context.mounted) Navigator.of(context).pop(true);
},
child: const Text('Cancel download'),
),
],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import 'package:flutter/material.dart';

class DownloadingProgressIndicatorColors {
static final pendingColor = Colors.grey[350]!;
static const failedColor = Colors.red;
static const skippedColor = Colors.orange;
static const successfulColor = Colors.green;
}
Loading

0 comments on commit 699b130

Please sign in to comment.