80 lines
1.7 KiB
Dart
80 lines
1.7 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import '../../../core/storage/saved_places_store.dart';
|
|
|
|
class SavedPlacesState {
|
|
final List<SavedPlace> places;
|
|
final bool loading;
|
|
final String? error;
|
|
|
|
const SavedPlacesState({
|
|
this.places = const [],
|
|
this.loading = false,
|
|
this.error,
|
|
});
|
|
|
|
SavedPlacesState copyWith({
|
|
List<SavedPlace>? places,
|
|
bool? loading,
|
|
String? error,
|
|
}) {
|
|
return SavedPlacesState(
|
|
places: places ?? this.places,
|
|
loading: loading ?? this.loading,
|
|
error: error,
|
|
);
|
|
}
|
|
}
|
|
|
|
class SavedPlacesCubit extends Cubit<SavedPlacesState> {
|
|
final SavedPlacesStore _store;
|
|
|
|
SavedPlacesCubit(this._store) : super(const SavedPlacesState());
|
|
|
|
Future<void> load() async {
|
|
emit(state.copyWith(loading: true, error: null));
|
|
try {
|
|
final places = await _store.getAll();
|
|
emit(SavedPlacesState(places: places));
|
|
} catch (e) {
|
|
emit(state.copyWith(loading: false, error: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> addPlace(SavedPlace place) async {
|
|
try {
|
|
await _store.add(place);
|
|
await load();
|
|
} catch (e) {
|
|
emit(state.copyWith(error: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> removePlace(String id) async {
|
|
try {
|
|
await _store.remove(id);
|
|
await load();
|
|
} catch (e) {
|
|
emit(state.copyWith(error: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> setFavorite(String id, FavoriteType type) async {
|
|
try {
|
|
await _store.setFavorite(id, type);
|
|
await load();
|
|
} catch (e) {
|
|
emit(state.copyWith(error: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> clearFavorite(FavoriteType type) async {
|
|
try {
|
|
await _store.clearFavorite(type);
|
|
await load();
|
|
} catch (e) {
|
|
emit(state.copyWith(error: e.toString()));
|
|
}
|
|
}
|
|
}
|