Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:app_pym/domain/entities/app_pym/booking.dart';
4 : import 'package:app_pym/domain/entities/authentication/app_user.dart';
5 : import 'package:app_pym/domain/usecases/services/booking/add_booking_to_service.dart';
6 : import 'package:app_pym/domain/usecases/services/booking/booking_of_service_params.dart';
7 : import 'package:app_pym/domain/usecases/services/booking/delete_booking_of_service.dart';
8 : import 'package:app_pym/domain/usecases/services/booking/update_booking_of_service.dart';
9 : import 'package:bloc/bloc.dart';
10 : import 'package:freezed_annotation/freezed_annotation.dart';
11 : import 'package:injectable/injectable.dart';
12 : import 'package:rxdart/rxdart.dart';
13 :
14 : part 'booking_of_service_bloc.freezed.dart';
15 : part 'booking_of_service_event.dart';
16 : part 'booking_of_service_state.dart';
17 :
18 : @prod
19 : @injectable
20 : class BookingOfServiceBloc
21 : extends Bloc<BookingOfServiceEvent, BookingOfServiceState> {
22 : final AddBookingToService addBookingToService;
23 : final DeleteBookingOfService deleteBookingOfService;
24 : final UpdateBookingOfService updateBookingOfService;
25 :
26 0 : BookingOfServiceBloc(
27 : this.addBookingToService,
28 : this.deleteBookingOfService,
29 : this.updateBookingOfService,
30 : );
31 :
32 0 : @override
33 0 : BookingOfServiceState get initialState => BookingOfServiceState.empty();
34 :
35 : @override
36 0 : Stream<BookingOfServiceState> mapEventToState(
37 : BookingOfServiceEvent event,
38 : ) async* {
39 0 : yield* event.when(
40 0 : titleChanged: _mapTitleChangedToState,
41 0 : add: _mapAddToState,
42 0 : delete: _mapDeleteToState,
43 0 : update: _mapUpdateToState,
44 : );
45 : }
46 :
47 0 : @override
48 : Stream<Transition<BookingOfServiceEvent, BookingOfServiceState>>
49 : transformEvents(
50 : Stream<BookingOfServiceEvent> events,
51 : Stream<Transition<BookingOfServiceEvent, BookingOfServiceState>> Function(
52 : BookingOfServiceEvent)
53 : transitionFn,
54 : ) {
55 0 : final nonDebounceStream = events.where((event) {
56 0 : return event is! TitleChanged;
57 : });
58 0 : final debounceStream = events.where((event) {
59 0 : return event is TitleChanged;
60 0 : }).debounceTime(const Duration(milliseconds: 100));
61 0 : return super.transformEvents(
62 0 : nonDebounceStream.mergeWith([debounceStream]), transitionFn);
63 : }
64 :
65 0 : Stream<BookingOfServiceState> _mapAddToState(
66 : Booking booking,
67 : AppUser appUser,
68 : ) async* {
69 0 : yield BookingOfServiceState.loading();
70 : try {
71 0 : await addBookingToService(BookingOfServiceParams(
72 : booking: booking,
73 : appUser: appUser,
74 : ));
75 0 : yield BookingOfServiceState.success();
76 0 : } on Exception catch (e) {
77 0 : yield BookingOfServiceState.failure(e);
78 : }
79 : }
80 :
81 0 : Stream<BookingOfServiceState> _mapDeleteToState(
82 : Booking booking,
83 : AppUser appUser,
84 : ) async* {
85 0 : yield BookingOfServiceState.loading();
86 : try {
87 0 : await deleteBookingOfService(BookingOfServiceParams(
88 : booking: booking,
89 : appUser: appUser,
90 : ));
91 0 : yield BookingOfServiceState.success();
92 0 : } on Exception catch (e) {
93 0 : yield BookingOfServiceState.failure(e);
94 : }
95 : }
96 :
97 0 : Stream<BookingOfServiceState> _mapTitleChangedToState(String title) async* {
98 0 : yield state.updateTitle(isTitleValid: title != null && title.isNotEmpty);
99 : }
100 :
101 0 : Stream<BookingOfServiceState> _mapUpdateToState(
102 : Booking booking,
103 : AppUser appUser,
104 : ) async* {
105 0 : yield BookingOfServiceState.loading();
106 : try {
107 0 : await updateBookingOfService(BookingOfServiceParams(
108 : booking: booking,
109 : appUser: appUser,
110 : ));
111 0 : yield BookingOfServiceState.success();
112 0 : } on Exception catch (e) {
113 0 : yield BookingOfServiceState.failure(e);
114 : }
115 : }
116 : }
|