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