|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:appetizer/data/constants/constants.dart'; |
| 4 | +import 'package:appetizer/domain/repositories/user/user_repository.dart'; |
| 5 | +import 'package:bloc/bloc.dart'; |
| 6 | +import 'package:equatable/equatable.dart'; |
| 7 | + |
| 8 | +part 'hostel_change_event.dart'; |
| 9 | +part 'hostel_change_state.dart'; |
| 10 | + |
| 11 | +class HostelChangeBloc extends Bloc<HostelChangeEvent, HostelChangeState> { |
| 12 | + final UserRepository repo; |
| 13 | + HostelChangeBloc({required this.repo}) : super(const HostelChangeInitial()) { |
| 14 | + on<HostelChangePressed>(_onHostelChangePressed); |
| 15 | + on<HostelSearchQueryChanged>(_onHostelSearchQueryChanged); |
| 16 | + } |
| 17 | + |
| 18 | + FutureOr<void> _onHostelChangePressed( |
| 19 | + HostelChangePressed event, Emitter<HostelChangeState> emit) async { |
| 20 | + emit(Loading()); |
| 21 | + String hostel = event.hostel; |
| 22 | + String roomNo = event.roomNo; |
| 23 | + if (hostel == "") { |
| 24 | + emit(const HostelChangeInitial(error: "Please select a hostel")); |
| 25 | + return; |
| 26 | + } |
| 27 | + if (roomNo == "") { |
| 28 | + emit(const HostelChangeInitial(error: "Please enter a room number")); |
| 29 | + if (hostel != "") emit(HostelQueryChanged(query: hostel)); |
| 30 | + return; |
| 31 | + } |
| 32 | + try { |
| 33 | + await repo.postChangeHostel(hostel, roomNo); |
| 34 | + emit(HostelChangeSuccess()); |
| 35 | + } catch (e) { |
| 36 | + emit(const HostelChangeInitial(error: AppConstants.GENERIC_FAILURE)); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + FutureOr<void> _onHostelSearchQueryChanged( |
| 41 | + HostelSearchQueryChanged event, Emitter<HostelChangeState> emit) async { |
| 42 | + if (event.query == "") { |
| 43 | + emit(const HostelChangeInitial()); |
| 44 | + } else { |
| 45 | + emit(Loading()); |
| 46 | + emit(HostelQueryChanged(query: event.query)); |
| 47 | + } |
| 48 | + // emit(const HostelChangeInitial()); |
| 49 | + } |
| 50 | +} |
0 commit comments