-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3eae60
commit 458a6c3
Showing
14 changed files
with
542 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
.../contents/download_configuration/components/config_options/components/store_selector.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.