Line data Source code
1 : import 'dart:async';
2 :
3 : import 'package:app_pym/core/constants/mobility.dart';
4 : import 'package:app_pym/core/usecases/usecase.dart';
5 : import 'package:app_pym/domain/entities/mobility/trip.dart';
6 : import 'package:app_pym/domain/usecases/mobility/fetch_bus_trips.dart';
7 : import 'package:app_pym/domain/usecases/mobility/fetch_train_trips.dart';
8 : import 'package:bloc/bloc.dart';
9 : import 'package:flutter/foundation.dart';
10 : import 'package:freezed_annotation/freezed_annotation.dart';
11 : import 'package:injectable/injectable.dart';
12 :
13 : part 'trips_bloc.freezed.dart';
14 : part 'trips_event.dart';
15 : part 'trips_state.dart';
16 :
17 : @prod
18 : @injectable
19 : class TripsBloc extends Bloc<TripsEvent, TripsState> {
20 : final FetchBusTrips fetchBusTrips;
21 : final FetchTrainTrips fetchTrainTrips;
22 :
23 0 : TripsBloc({
24 : @required this.fetchBusTrips,
25 : @required this.fetchTrainTrips,
26 : });
27 :
28 0 : @override
29 0 : TripsState get initialState => TripsState.initial();
30 :
31 : @override
32 0 : Stream<TripsState> mapEventToState(
33 : TripsEvent event,
34 : ) async* {
35 0 : yield* event.when(
36 0 : fetchBus: (direction) async* {
37 0 : yield state.loading();
38 : try {
39 0 : final trips = await fetchBusTrips(const NoParams());
40 0 : yield state.busLoaded(busTrips: trips, direction: direction);
41 0 : } on Exception catch (e) {
42 0 : yield state.error(e);
43 : }
44 : },
45 0 : hideBus: () async* {
46 0 : yield state.hideBus();
47 : },
48 0 : fetchTrain: (direction) async* {
49 0 : yield state.loading();
50 : try {
51 0 : final trips = await fetchTrainTrips(const NoParams());
52 0 : yield state.trainLoaded(trainTrips: trips, direction: direction);
53 0 : } on Exception catch (e) {
54 0 : yield state.error(e);
55 : }
56 : },
57 0 : hideTrain: () async* {
58 0 : yield state.hideTrain();
59 : },
60 0 : changeDirection: (direction) async* {
61 0 : yield state.changeDirection(direction);
62 : },
63 : );
64 : }
65 : }
|