added controller for locations
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
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 = [];
|
||||||
|
|
||||||
|
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> _loadLocations() {
|
||||||
|
_locations.clear();
|
||||||
|
return _repository.loadLocations().then(
|
||||||
|
(value) => _locations.addAll(value),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user