71 lines
1.7 KiB
Dart
71 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:async/async.dart';
|
|
|
|
import '../model/flood_station.dart';
|
|
import 'api.dart';
|
|
|
|
class FloodStationProvider extends ChangeNotifier {
|
|
FloodStation? selectedStation;
|
|
bool filtered = false;
|
|
|
|
List<FloodStation> _allStations = [];
|
|
List<FloodStation> _filteredStations = [];
|
|
|
|
List<FloodStation> get allStations => _allStations;
|
|
List<FloodStation> get filteredStations => _filteredStations;
|
|
|
|
CancelableOperation? _filteredStationsFuture;
|
|
|
|
CancelableOperation? get filteredStationsFuture => _filteredStationsFuture;
|
|
|
|
Future loadAllStationsInBatches({silent = false}) {
|
|
int offset = 0;
|
|
return Future.doWhile(() async {
|
|
final stations = await Api.fetchStationsByRange(500, offset);
|
|
if (stations.isNotEmpty) {
|
|
_allStations.addAll(stations);
|
|
if (!silent) {
|
|
notifyListeners();
|
|
}
|
|
offset += 500;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
});
|
|
}
|
|
|
|
Future loadAllStations({silent = false}) {
|
|
return Api.fetchAllStations().then(
|
|
(value) {
|
|
_allStations = value;
|
|
if (!silent) {
|
|
notifyListeners();
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
Future loadFilteredStations(String filter, {silent = false}) {
|
|
if (_filteredStationsFuture != null) {
|
|
_filteredStationsFuture!.cancel();
|
|
}
|
|
final future = Api.fetchFilteredStations(filter);
|
|
_filteredStationsFuture = CancelableOperation.fromFuture(future).then(
|
|
(value) {
|
|
_filteredStations = value;
|
|
if (!silent) {
|
|
notifyListeners();
|
|
}
|
|
},
|
|
);
|
|
return future;
|
|
}
|
|
|
|
void cancelFilterLoading() {
|
|
if (_filteredStationsFuture != null) {
|
|
_filteredStationsFuture!.cancel();
|
|
}
|
|
}
|
|
}
|