add filter panel to calculator
This commit is contained in:
@@ -72,9 +72,14 @@ class DataService extends ChangeNotifier {
|
||||
bool get isEventFieldsLoading => _isEventFieldsLoading;
|
||||
|
||||
// Station Data
|
||||
List<Station>? _cachedStations;
|
||||
DateTime? _stationsFetchedAt;
|
||||
Future<List<Station>>? _stationsInFlight;
|
||||
final Map<String, List<Station>> _stationCache = {};
|
||||
final Map<String, Future<List<Station>>?> _stationInFlightByKey = {};
|
||||
List<String> _stationNetworks = [];
|
||||
Map<String, List<String>> _stationCountryNetworks = {};
|
||||
DateTime? _stationFiltersFetchedAt;
|
||||
List<String> get stationNetworks => _stationNetworks;
|
||||
Map<String, List<String>> get stationCountryNetworks =>
|
||||
_stationCountryNetworks;
|
||||
|
||||
List<String> stations = [""];
|
||||
|
||||
@@ -365,37 +370,75 @@ class DataService extends ChangeNotifier {
|
||||
0;
|
||||
}
|
||||
|
||||
Future<List<Station>> fetchStations() async {
|
||||
Future<void> fetchStationFilters() async {
|
||||
final now = DateTime.now();
|
||||
|
||||
// If cache exists and is less than 30 minutes old, return it
|
||||
if (_cachedStations != null &&
|
||||
_stationsFetchedAt != null &&
|
||||
now.difference(_stationsFetchedAt!) < Duration(minutes: 30)) {
|
||||
return _cachedStations!;
|
||||
if (_stationFiltersFetchedAt != null &&
|
||||
now.difference(_stationFiltersFetchedAt!) < const Duration(minutes: 30) &&
|
||||
_stationNetworks.isNotEmpty) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final response = await api.get('/stations/filter');
|
||||
if (response is List && response.isNotEmpty && response.first is Map) {
|
||||
final map = Map<String, dynamic>.from(response.first as Map);
|
||||
final networks = (map['networks'] as List? ?? const [])
|
||||
.whereType<String>()
|
||||
.toList();
|
||||
final countryNetworksRaw =
|
||||
map['country_networks'] as Map? ?? const <String, dynamic>{};
|
||||
final countryNetworks = <String, List<String>>{};
|
||||
countryNetworksRaw.forEach((key, value) {
|
||||
if (value is List) {
|
||||
countryNetworks[key] = value.whereType<String>().toList();
|
||||
}
|
||||
});
|
||||
_stationNetworks = networks;
|
||||
_stationCountryNetworks = countryNetworks;
|
||||
_stationFiltersFetchedAt = now;
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Failed to fetch station filters: $e');
|
||||
}
|
||||
}
|
||||
|
||||
if (_stationsInFlight != null) return _stationsInFlight!;
|
||||
String _stationKey(List<String> countries, List<String> networks) {
|
||||
final c = countries..sort();
|
||||
final n = networks..sort();
|
||||
return 'c:${c.join('|')};n:${n.join('|')}';
|
||||
}
|
||||
|
||||
_stationsInFlight = () async {
|
||||
Future<List<Station>> fetchStations({
|
||||
List<String> countries = const [],
|
||||
List<String> networks = const [],
|
||||
}) async {
|
||||
final key = _stationKey(List.from(countries), List.from(networks));
|
||||
|
||||
if (_stationCache.containsKey(key)) return _stationCache[key]!;
|
||||
final inflight = _stationInFlightByKey[key];
|
||||
if (inflight != null) return inflight;
|
||||
|
||||
final future = () async {
|
||||
try {
|
||||
final response = await api.get('/location');
|
||||
final response = await api.post('/location', {
|
||||
'countries_filter': countries,
|
||||
'network_filter': networks,
|
||||
});
|
||||
if (response is! List) return const <Station>[];
|
||||
final parsed = response
|
||||
.whereType<Map>()
|
||||
.map((e) => Station.fromJson(Map<String, dynamic>.from(e)))
|
||||
.toList();
|
||||
_cachedStations = parsed;
|
||||
_stationsFetchedAt = now;
|
||||
_stationCache[key] = parsed;
|
||||
return parsed;
|
||||
} catch (e) {
|
||||
debugPrint('Failed to fetch stations: $e');
|
||||
return const <Station>[];
|
||||
} finally {
|
||||
_stationsInFlight = null;
|
||||
_stationInFlightByKey.remove(key);
|
||||
}
|
||||
}();
|
||||
|
||||
return _stationsInFlight!;
|
||||
_stationInFlightByKey[key] = future;
|
||||
return future;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user