-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdashboard_bloc.dart
49 lines (46 loc) · 1.66 KB
/
dashboard_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
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:equatable/equatable.dart';
import 'package:file_selector/file_selector.dart';
part 'dashboard_event.dart';
part 'dashboard_state.dart';
/// Handles selecting and removing files.
class DashboardBloc extends Bloc<DashboardEvent, DashboardState> {
DashboardBloc() : super(DashboardState(files: [], alreadyPresent: false)) {
on<DashboardEvent>(_onEvent, transformer: sequential());
}
FutureOr<void> _onEvent(
DashboardEvent event, Emitter<DashboardState> emit) async {
// TODO: logic goes here...
if (event is NewFileAdded) {
List<XFile> finalEventList = List.from(event.files);
bool alreadyPresent = false;
// create your own wrapper around the third party class as use EquatableMixin
for (var stateFile in state.files) {
for (var eventFile in event.files) {
if (eventFile.path == stateFile.path) {
alreadyPresent = true;
finalEventList.remove(eventFile);
}
}
}
if (alreadyPresent) {
emit(state.copyWith(
files: List.from(state.files)..addAll(finalEventList),
alreadyPresent: true));
} else {
emit(state.copyWith(
files: List.from(state.files)..addAll(event.files),
alreadyPresent: false));
;
}
} else if (event is FileRemoved) {
emit(state.copyWith(
files: List.from(state.files)..remove(event.file),
alreadyPresent: false));
} else if (event is RemoveAllFiles) {
emit(state.copyWith(files: [], alreadyPresent: false));
}
}
}