diff --git a/lib/service/location_controller.dart b/lib/service/location_controller.dart new file mode 100644 index 0000000..a736ee9 --- /dev/null +++ b/lib/service/location_controller.dart @@ -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 _locations = []; + + Future addLocation(Location location) { + _locations.add(location); + notifyListeners(); + return _repository.createLocation(location); + } + + Future deleteLocation(Location location) { + _locations.remove(location); + notifyListeners(); + return _repository.deleteLocation(location); + } + + Future _loadLocations() { + _locations.clear(); + return _repository.loadLocations().then( + (value) => _locations.addAll(value), + ); + } +}