46 lines
1.2 KiB
Dart
46 lines
1.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../model/location.dart';
|
|
import '../../model/repositories/interfaces/location_repository.dart';
|
|
|
|
class LocationController extends ChangeNotifier {
|
|
LocationController(LocationRepository repository) : _repository = repository {
|
|
_loadLocations();
|
|
}
|
|
|
|
final LocationRepository _repository;
|
|
|
|
final List<Location> _locations = [];
|
|
|
|
List<Location> get locations => _locations;
|
|
|
|
Future<void> addLocation(Location location) {
|
|
_locations.add(location);
|
|
notifyListeners();
|
|
return _repository.createLocation(location);
|
|
}
|
|
|
|
Future<void> deleteLocation(Location location) {
|
|
_locations.remove(location);
|
|
notifyListeners();
|
|
return _repository.deleteLocation(location);
|
|
}
|
|
|
|
Future<void> updateLocation(Location oldLocation, Location newLocation) {
|
|
final index = _locations.indexOf(oldLocation);
|
|
_locations.remove(oldLocation);
|
|
_locations.insert(index, newLocation);
|
|
notifyListeners();
|
|
return _repository
|
|
.deleteLocation(oldLocation)
|
|
.whenComplete(() => _repository.createLocation(newLocation));
|
|
}
|
|
|
|
Future<void> _loadLocations() {
|
|
_locations.clear();
|
|
return _repository.loadLocations().then(
|
|
(value) => _locations.addAll(value),
|
|
);
|
|
}
|
|
}
|