added comments and refactored code

This commit is contained in:
2025-01-28 22:36:23 +01:00
parent 6828a9a14b
commit 8f5ed07be9
12 changed files with 83 additions and 58 deletions

View File

@@ -4,19 +4,20 @@ import '../model/flood_station.dart';
import 'api.dart';
class FloodStationProvider extends ChangeNotifier {
// since the getter and setter for the following two fields would be empty, they are publicly accessible
FloodStation? selectedStation;
bool filtered = false;
List<FloodStation> _allStations = [];
List<FloodStation> _filteredStations = [];
CancelableOperation? _filteredStationsFuture;
List<FloodStation> get allStations => _allStations;
List<FloodStation> get filteredStations => _filteredStations;
CancelableOperation? _filteredStationsFuture;
CancelableOperation? get filteredStationsFuture => _filteredStationsFuture;
/// loads all stations in batches of 500 and notifies listeners with every loop except if [silent] = true
/// this has lower performance than loading them all at once an shouldn't be used
Future loadAllStationsInBatches({silent = false}) {
int offset = 0;
return Future.doWhile(() async {
@@ -34,6 +35,7 @@ class FloodStationProvider extends ChangeNotifier {
});
}
/// loads all flood stations and notifies listeners when done
Future loadAllStations({silent = false}) {
return Api.fetchAllStations().then(
(value) {
@@ -45,6 +47,7 @@ class FloodStationProvider extends ChangeNotifier {
);
}
/// loads all stations whose label contains [filter]
Future loadFilteredStations(String filter, {silent = false}) {
if (_filteredStationsFuture != null) {
_filteredStationsFuture!.cancel();
@@ -61,6 +64,7 @@ class FloodStationProvider extends ChangeNotifier {
return future;
}
/// cancels loading of filtered results.
void cancelFilterLoading() {
if (_filteredStationsFuture != null) {
_filteredStationsFuture!.cancel();