-
Notifications
You must be signed in to change notification settings - Fork 7
/
bloc.dart
97 lines (88 loc) · 3.13 KB
/
bloc.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:gallery_service/src/models/src/gallery.dart';
import 'package:gallery_service/src/repository/index.dart';
import 'package:gallery_service/src/repository/src/gallery.dart';
import 'package:http_service/http_service.dart';
part 'event.dart';
part 'state.dart';
class GalleryBloc extends Bloc<GalleryEvent, GalleryState> {
GalleryBloc({required this.galleryRepository}) : super(const GalleryState());
final GalleryRepository galleryRepository;
@override
Stream<GalleryState> mapEventToState(GalleryEvent event) async* {
if (event is GetGalleryRequested) {
yield* _mapGetGalleryRequestedToState(event);
} else if (event is GalleryPushRequested) {
yield* _mapGalleryPushRequestedToState(event);
} else if (event is GalleryPopRequested) {
yield* _mapGalleryPopRequestedToState();
}
}
Stream<GalleryState> _mapGetGalleryRequestedToState(
GetGalleryRequested event) async* {
if (galleryRepository.gallery != null) {
yield state.copyWith(
status: GalleryStatus.success,
gallery: galleryRepository.gallery!,
);
return;
}
yield state.copyWith(status: GalleryStatus.loading);
try {
final gallery = await galleryRepository.getGallery(path: event.path);
yield state.copyWith(
status: GalleryStatus.success,
gallery: gallery,
);
} on NotFoundException {
print('kir to oon urlet');
yield state.copyWith(status: GalleryStatus.notFound);
} on SocketException {
print('kir to netet');
yield state.copyWith(status: GalleryStatus.failure);
} on Exception {
yield state.copyWith(status: GalleryStatus.failure);
}
}
Stream<GalleryState> _mapGalleryPushRequestedToState(
GalleryPushRequested event) async* {
yield state.copyWith(status: GalleryStatus.loading);
try {
final gallery = await galleryRepository.getGallery(path: event.path);
yield state.copyWith(
status: GalleryStatus.successPushed,
pushedGallery: gallery,
);
} on NotFoundException {
print('kir to oon urlet');
yield state.copyWith(status: GalleryStatus.notFound);
} on SocketException {
print('kir to netet');
yield state.copyWith(status: GalleryStatus.failure);
} on Exception {
yield state.copyWith(status: GalleryStatus.failure);
}
}
Stream<GalleryState> _mapGalleryPopRequestedToState() async* {
yield state.copyWith(status: GalleryStatus.loading);
try {
final gallery =
await galleryRepository.getGallery(path: state.gallery!.parent ?? '');
yield state.copyWith(
status: GalleryStatus.successPopped,
poppedGallery: gallery,
);
} on NotFoundException {
print('kir to oon urlet');
yield state.copyWith(status: GalleryStatus.notFound);
} on SocketException {
print('kir to netet');
yield state.copyWith(status: GalleryStatus.failure);
} on Exception {
yield state.copyWith(status: GalleryStatus.failure);
}
}
}