74 lines
2.1 KiB
Dart
74 lines
2.1 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'attraction_details_event.dart';
|
|
import 'attraction_details_state.dart';
|
|
import '../repository/attraction_details_repository.dart';
|
|
|
|
class AttractionDetailsBloc
|
|
extends Bloc<AttractionDetailsEvent, AttractionDetailsState> {
|
|
final AttractionDetailsRepository repository;
|
|
|
|
AttractionDetailsBloc({
|
|
required this.repository,
|
|
}) : super(AttractionDetailsInitial()) {
|
|
on<FetchAttractionDetails>(_onFetchAttractionDetails);
|
|
on<ToggleDescriptionExpanded>(_onToggleDescriptionExpanded);
|
|
on<UpdateGalleryIndex>(_onUpdateGalleryIndex);
|
|
on<UpdateFullScreenGalleryIndex>(_onUpdateFullScreenGalleryIndex);
|
|
}
|
|
|
|
void _onToggleDescriptionExpanded(
|
|
ToggleDescriptionExpanded event,
|
|
Emitter<AttractionDetailsState> emit,
|
|
) {
|
|
if (state is AttractionDetailsLoaded) {
|
|
final currentState = state as AttractionDetailsLoaded;
|
|
emit(currentState.copyWith(isExpanded: !currentState.isExpanded));
|
|
}
|
|
}
|
|
|
|
void _onUpdateGalleryIndex(
|
|
UpdateGalleryIndex event,
|
|
Emitter<AttractionDetailsState> emit,
|
|
) {
|
|
if (state is AttractionDetailsLoaded) {
|
|
final currentState = state as AttractionDetailsLoaded;
|
|
emit(currentState.copyWith(galleryIndex: event.index));
|
|
}
|
|
}
|
|
|
|
void _onUpdateFullScreenGalleryIndex(
|
|
UpdateFullScreenGalleryIndex event,
|
|
Emitter<AttractionDetailsState> emit,
|
|
) {
|
|
if (state is AttractionDetailsLoaded) {
|
|
final currentState = state as AttractionDetailsLoaded;
|
|
emit(currentState.copyWith(fullScreenGalleryIndex: event.index));
|
|
}
|
|
}
|
|
|
|
Future<void> _onFetchAttractionDetails(
|
|
FetchAttractionDetails event,
|
|
Emitter<AttractionDetailsState> emit,
|
|
) async {
|
|
emit(AttractionDetailsLoading());
|
|
|
|
try {
|
|
final response = await repository.fetchAttractionDetails(
|
|
attractionId: event.attractionId,
|
|
);
|
|
|
|
emit(
|
|
AttractionDetailsLoaded(
|
|
attractionDetails: response,
|
|
),
|
|
);
|
|
} catch (e) {
|
|
emit(
|
|
AttractionDetailsError(
|
|
message: e.toString(),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|