summaryrefslogtreecommitdiff
path: root/lib/widgets/dialogs/map
diff options
context:
space:
mode:
Diffstat (limited to 'lib/widgets/dialogs/map')
-rw-r--r--lib/widgets/dialogs/map/style_editor_dialog.dart146
-rw-r--r--lib/widgets/dialogs/map/style_selection_dialog.dart185
2 files changed, 331 insertions, 0 deletions
diff --git a/lib/widgets/dialogs/map/style_editor_dialog.dart b/lib/widgets/dialogs/map/style_editor_dialog.dart
new file mode 100644
index 0000000..620994b
--- /dev/null
+++ b/lib/widgets/dialogs/map/style_editor_dialog.dart
@@ -0,0 +1,146 @@
+import 'package:aves/model/settings/settings.dart';
+import 'package:aves/widgets/common/extensions/build_context.dart';
+import 'package:aves/widgets/dialogs/aves_dialog.dart';
+import 'package:aves_map/aves_map.dart';
+import 'package:flutter/material.dart';
+
+class MapStyleEditorDialog extends StatefulWidget {
+ static const routeName = '/dialog/map_style_editor';
+
+ final EntryMapStyle? initialValue;
+
+ const MapStyleEditorDialog({
+ super.key,
+ this.initialValue,
+ });
+
+ @override
+ State<MapStyleEditorDialog> createState() => _MapStyleEditorDialogState();
+}
+
+class _MapStyleEditorDialogState extends State<MapStyleEditorDialog> {
+ late final Set<EntryMapStyle> _otherCustomStyles;
+ final TextEditingController _nameController = TextEditingController();
+ final TextEditingController _urlController = TextEditingController();
+ final TextEditingController _subdomainsController = TextEditingController();
+ final TextEditingController _userAgentController = TextEditingController();
+ final ValueNotifier<bool> _isValidNotifier = ValueNotifier(false);
+
+ @override
+ void initState() {
+ super.initState();
+ final initialValue = widget.initialValue;
+ _otherCustomStyles = settings.customMapStyles.where((v) => v != initialValue).toSet();
+ _nameController.text = initialValue?.name ?? '';
+ _urlController.text = initialValue?.url ?? '';
+ _subdomainsController.text = initialValue?.subdomains.join(',') ?? '';
+ _userAgentController.text = initialValue?.userAgent ?? '';
+ _validate();
+ }
+
+ @override
+ void dispose() {
+ _nameController.dispose();
+ _urlController.dispose();
+ _subdomainsController.dispose();
+ _userAgentController.dispose();
+ _isValidNotifier.dispose();
+ super.dispose();
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final l10n = context.l10n;
+ return AvesDialog(
+ scrollableContent: [
+ Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
+ child: TextField(
+ controller: _nameController,
+ decoration: InputDecoration(
+ labelText: l10n.mapStyleEditorDialogName,
+ ),
+ autofocus: true,
+ onChanged: (_) => _validate(),
+ ),
+ ),
+ Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
+ child: TextField(
+ controller: _urlController,
+ decoration: InputDecoration(
+ labelText: l10n.mapStyleEditorDialogUrl,
+ ),
+ keyboardType: TextInputType.url,
+ onChanged: (_) => _validate(),
+ ),
+ ),
+ Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
+ child: TextField(
+ controller: _subdomainsController,
+ decoration: InputDecoration(
+ labelText: l10n.mapStyleEditorDialogSubdomains,
+ ),
+ ),
+ ),
+ Padding(
+ padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
+ child: TextField(
+ controller: _userAgentController,
+ decoration: InputDecoration(
+ labelText: l10n.mapStyleEditorDialogUserAgent,
+ ),
+ ),
+ ),
+ ],
+ actions: [
+ const CancelButton(),
+ ValueListenableBuilder<bool>(
+ valueListenable: _isValidNotifier,
+ builder: (context, isValid, child) {
+ return TextButton(
+ onPressed: isValid ? () => _submit(context) : null,
+ child: Text(widget.initialValue != null ? l10n.applyButtonLabel : l10n.createButtonLabel),
+ );
+ },
+ )
+ ],
+ );
+ }
+
+ EntryMapStyle _buildStyle() {
+ final name = _nameController.text.trim();
+ final url = _urlController.text.trim();
+ final subdomains = _subdomainsController.text.split(',').map((v) => v.trim()).toList();
+ final userAgent = _userAgentController.text.trim();
+ return EntryMapStyle(
+ key: 'custom_$name',
+ name: name,
+ url: url,
+ subdomains: subdomains,
+ userAgent: userAgent.isNotEmpty ? userAgent : null,
+ );
+ }
+
+ Future<void> _validate() async {
+ final style = _buildStyle();
+
+ final name = style.name ?? '';
+ final url = style.url ?? '';
+ if (name.isEmpty || url.isEmpty) {
+ _isValidNotifier.value = false;
+ return;
+ }
+
+ final key = style.key;
+ final duplicate = _otherCustomStyles.any((v) => v.key == key || v.url == url);
+ _isValidNotifier.value = !duplicate;
+ }
+
+ void _submit(BuildContext context) {
+ if (_isValidNotifier.value) {
+ Navigator.maybeOf(context)?.pop<EntryMapStyle>(_buildStyle());
+ }
+ }
+}
diff --git a/lib/widgets/dialogs/map/style_selection_dialog.dart b/lib/widgets/dialogs/map/style_selection_dialog.dart
new file mode 100644
index 0000000..ea78ce4
--- /dev/null
+++ b/lib/widgets/dialogs/map/style_selection_dialog.dart
@@ -0,0 +1,185 @@
+import 'package:aves/model/settings/settings.dart';
+import 'package:aves/services/common/services.dart';
+import 'package:aves/theme/icons.dart';
+import 'package:aves/utils/collection_utils.dart';
+import 'package:aves/view/src/settings/enums.dart';
+import 'package:aves/widgets/common/basic/scaffold.dart';
+import 'package:aves/widgets/common/extensions/build_context.dart';
+import 'package:aves/widgets/common/identity/buttons/outlined_button.dart';
+import 'package:aves/widgets/dialogs/aves_dialog.dart';
+import 'package:aves/widgets/dialogs/map/style_editor_dialog.dart';
+import 'package:aves/widgets/dialogs/selection_dialogs/radio_list_tile.dart';
+import 'package:aves_map/aves_map.dart';
+import 'package:collection/collection.dart';
+import 'package:flutter/material.dart';
+import 'package:provider/provider.dart';
+
+class MapStyleSelectionDialog extends StatefulWidget {
+ static const routeName = '/dialog/map_style_selection';
+
+ const MapStyleSelectionDialog({super.key});
+
+ @override
+ State<MapStyleSelectionDialog> createState() => _MapStyleSelectionDialogState();
+}
+
+class _MapStyleSelectionDialogState extends State<MapStyleSelectionDialog> {
+ late EntryMapStyle? _selectedValue;
+
+ @override
+ void initState() {
+ super.initState();
+ _selectedValue = settings.mapStyle;
+ }
+
+ @override
+ Widget build(BuildContext context) {
+ final useTvLayout = settings.useTvLayout;
+ final l10n = context.l10n;
+ final defaultStyles = availability.mapStyles;
+ final customStyles = context.select<Settings, Set<EntryMapStyle>>((v) => v.customMapStyles).sortedBy((v) => v.getName(context));
+
+ return PopScope(
+ onPopInvokedWithResult: (didPop, result) => settings.mapStyle = _selectedValue,
+ child: AvesScaffold(
+ appBar: AppBar(
+ automaticallyImplyLeading: !useTvLayout,
+ title: Text(context.l10n.mapStyleDialogTitle),
+ ),
+ body: SafeArea(
+ bottom: false,
+ child: ListView(
+ children: [
+ ...defaultStyles.map((v) {
+ return SelectionRadioListTile(
+ // key is expected by test driver
+ key: Key(v.key),
+ value: v,
+ title: v.getName(context),
+ needConfirmation: false,
+ secondary: _getDefaultStylePreview(v),
+ getGroupValue: () => _selectedValue,
+ setGroupValue: _setGroupValue,
+ );
+ }),
+ ...customStyles.map((v) {
+ return SelectionRadioListTile(
+ // key is expected by test driver
+ key: Key(v.key),
+ value: v,
+ title: v.getName(context),
+ needConfirmation: false,
+ secondary: _buildCustomStyleButtons(v),
+ getGroupValue: () => _selectedValue,
+ setGroupValue: _setGroupValue,
+ );
+ }),
+ if (!settings.useTvLayout)
+ Container(
+ alignment: Alignment.center,
+ padding: const EdgeInsets.only(top: 4, bottom: 8),
+ child: AvesOutlinedButton(
+ icon: const Icon(AIcons.add),
+ label: l10n.mapStyleDialogAddStyle,
+ onPressed: _add,
+ ),
+ ),
+ ],
+ ),
+ ),
+ ),
+ );
+ }
+
+ void _setGroupValue(EntryMapStyle? style) => setState(() => _selectedValue = style);
+
+ Widget? _getDefaultStylePreview(EntryMapStyle style) {
+ final assetName = _getImageAssetName(style);
+ if (assetName == null) return null;
+
+ return ImageMarker(
+ count: null,
+ locale: null,
+ drawArrow: false,
+ buildThumbnailImage: (_) => Image.asset('assets/$assetName'),
+ );
+ }
+
+ String? _getImageAssetName(EntryMapStyle style) {
+ if (style == EntryMapStyles.googleNormal) return 'map_tile_128_google_normal.png';
+ if (style == EntryMapStyles.googleHybrid) return 'map_tile_128_google_hybrid.png';
+ if (style == EntryMapStyles.googleTerrain) return 'map_tile_128_google_terrain.png';
+ if (style == EntryMapStyles.osmLiberty) return 'map_tile_128_osm_liberty.png';
+ if (style == EntryMapStyles.openTopoMap) return 'map_tile_128_opentopomap.png';
+ if (style == EntryMapStyles.osmHot) return 'map_tile_128_osm_hot.png';
+ if (style == EntryMapStyles.stamenWatercolor) return 'map_tile_128_stamen_watercolor.png';
+ return null;
+ }
+
+ Widget _buildCustomStyleButtons(EntryMapStyle style) {
+ final l10n = context.l10n;
+ return Row(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ IconButton(
+ icon: const Icon(AIcons.edit),
+ onPressed: () => _edit(style),
+ tooltip: l10n.changeTooltip,
+ ),
+ IconButton(
+ icon: const Icon(AIcons.clear),
+ onPressed: () => _remove(style),
+ tooltip: l10n.actionRemove,
+ ),
+ ],
+ );
+ }
+
+ Future<void> _add() async {
+ final newStyle = await showDialog<EntryMapStyle>(
+ context: context,
+ builder: (context) => const MapStyleEditorDialog(),
+ routeSettings: const RouteSettings(name: MapStyleEditorDialog.routeName),
+ );
+ if (newStyle == null) return;
+
+ settings.customMapStyles = settings.customMapStyles..add(newStyle);
+ _setGroupValue(newStyle);
+ }
+
+ Future<void> _edit(EntryMapStyle style) async {
+ final newStyle = await showDialog<EntryMapStyle>(
+ context: context,
+ builder: (context) => MapStyleEditorDialog(initialValue: style),
+ routeSettings: const RouteSettings(name: MapStyleEditorDialog.routeName),
+ );
+ if (newStyle == null) return;
+
+ settings.customMapStyles = settings.customMapStyles..replace(style, newStyle);
+ _setGroupValue(newStyle);
+ }
+
+ Future<void> _remove(EntryMapStyle style) async {
+ final l10n = context.l10n;
+ final confirmed = await showDialog<bool>(
+ context: context,
+ builder: (context) => AvesDialog(
+ content: Text(l10n.genericDangerWarningDialogMessage),
+ actions: [
+ const CancelButton(),
+ TextButton(
+ onPressed: () => Navigator.maybeOf(context)?.pop(true),
+ child: Text(l10n.applyButtonLabel),
+ ),
+ ],
+ ),
+ routeSettings: const RouteSettings(name: AvesDialog.warningRouteName),
+ );
+ if (confirmed == null || !confirmed) return;
+
+ settings.customMapStyles = settings.customMapStyles..remove(style);
+ if (_selectedValue == style) {
+ _setGroupValue(null);
+ }
+ }
+}