Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:app_pym/core/validators.dart';
4 : import 'package:app_pym/domain/usecases/authentication/forgot_password.dart';
5 : import 'package:flutter/foundation.dart';
6 : import 'package:flutter/services.dart';
7 : import 'package:flutter_bloc/flutter_bloc.dart';
8 : import 'package:freezed_annotation/freezed_annotation.dart';
9 : import 'package:injectable/injectable.dart';
10 : import 'package:rxdart/rxdart.dart' show DebounceExtensions, MergeExtension;
11 :
12 : part 'forgot_bloc.freezed.dart';
13 : part 'forgot_event.dart';
14 : part 'forgot_state.dart';
15 :
16 : @prod
17 : @injectable
18 : class ForgotBloc extends Bloc<ForgotEvent, ForgotState> {
19 : final ForgotPassword forgotPassword;
20 :
21 0 : ForgotBloc(this.forgotPassword);
22 :
23 0 : @override
24 0 : ForgotState get initialState => ForgotState.empty();
25 :
26 : @override
27 0 : Stream<ForgotState> mapEventToState(
28 : ForgotEvent event,
29 : ) async* {
30 0 : yield* event.when(
31 0 : emailChanged: _mapEmailChangedToState,
32 0 : submitted: _mapFormSubmittedToState,
33 : );
34 : }
35 :
36 0 : @override
37 : Stream<Transition<ForgotEvent, ForgotState>> transformEvents(
38 : Stream<ForgotEvent> events,
39 : Stream<Transition<ForgotEvent, ForgotState>> Function(ForgotEvent)
40 : transitionFn,
41 : ) {
42 0 : final nonDebounceStream = events.where((event) {
43 0 : return event is! EmailChanged;
44 : });
45 0 : final debounceStream = events.where((event) {
46 0 : return event is EmailChanged;
47 0 : }).debounceTime(const Duration(milliseconds: 300));
48 0 : return super.transformEvents(
49 0 : nonDebounceStream.mergeWith([debounceStream]), transitionFn);
50 : }
51 :
52 0 : Stream<ForgotState> _mapEmailChangedToState(String email) async* {
53 0 : yield state.update(
54 0 : isEmailValid: Validators.isValidEmail(email),
55 : );
56 : }
57 :
58 0 : Stream<ForgotState> _mapFormSubmittedToState(
59 : String email,
60 : ) async* {
61 0 : yield ForgotState.loading();
62 : try {
63 0 : await forgotPassword(email);
64 0 : yield ForgotState.success();
65 0 : } on PlatformException catch (e) {
66 0 : yield ForgotState.failure(e.message);
67 : } catch (e) {
68 0 : yield ForgotState.failure(e.toString());
69 : }
70 : }
71 : }
|