168 lines
4.5 KiB
Dart
168 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
|
|
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
|
|
import 'package:rxdart/rxdart.dart';
|
|
|
|
import '../../../../model/delivery.dart';
|
|
import 'note_event.dart';
|
|
import 'note_state.dart';
|
|
import 'package:hl_lieferservice/feature/delivery/detail/repository/note_repository.dart';
|
|
|
|
class NoteBloc extends Bloc<NoteEvent, NoteState> {
|
|
final NoteRepository repository;
|
|
final OperationBloc opBloc;
|
|
final String deliveryId;
|
|
|
|
StreamSubscription? _combinedSubscription;
|
|
|
|
NoteBloc({
|
|
required this.repository,
|
|
required this.opBloc,
|
|
required this.deliveryId,
|
|
}) : super(NoteInitial()) {
|
|
_combinedSubscription = CombineLatestStream.combine2(
|
|
repository.notes,
|
|
repository.images,
|
|
(note, image) => {"note": note, "image": image},
|
|
).listen(
|
|
(data) => add(
|
|
DataUpdated(
|
|
images: data["image"] as List<ImageNote>,
|
|
notes: data["note"] as List<Note>,
|
|
),
|
|
),
|
|
);
|
|
|
|
on<LoadNote>(_load);
|
|
on<AddNote>(_add);
|
|
on<EditNote>(_edit);
|
|
on<RemoveNote>(_remove);
|
|
on<AddImageNote>(_upload);
|
|
on<RemoveImageNote>(_removeImage);
|
|
on<ResetNotes>(_reset);
|
|
on<DataUpdated>(_dataUpdated);
|
|
}
|
|
|
|
@override
|
|
Future<void> close() {
|
|
_combinedSubscription?.cancel();
|
|
|
|
return super.close();
|
|
}
|
|
|
|
Future<void> _dataUpdated(DataUpdated event, Emitter<NoteState> emit) async {
|
|
emit(NoteLoaded(notes: event.notes, images: event.images));
|
|
}
|
|
|
|
Future<void> _reset(ResetNotes event, Emitter<NoteState> emit) async {
|
|
emit.call(NoteInitial());
|
|
}
|
|
|
|
Future<void> _removeImage(
|
|
RemoveImageNote event,
|
|
Emitter<NoteState> emit,
|
|
) async {
|
|
opBloc.add(LoadOperation());
|
|
|
|
try {
|
|
await repository.deleteImage(event.deliveryId, event.objectId);
|
|
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Löschen des Bildes: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(FailOperation(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> _upload(AddImageNote event, Emitter<NoteState> emit) async {
|
|
opBloc.add(LoadOperation());
|
|
|
|
try {
|
|
Uint8List imageBytes = await event.file.readAsBytes();
|
|
await repository.addImage(event.deliveryId, imageBytes);
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Hinzufügen des Bildes: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(FailOperation(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> _load(LoadNote event, Emitter<NoteState> emit) async {
|
|
emit.call(NoteLoading());
|
|
|
|
try {
|
|
List<String> urls =
|
|
event.delivery.images.map((image) => image.url).toList();
|
|
|
|
debugPrint("IMAGE URLS : $urls");
|
|
|
|
await repository.loadNotes(event.delivery.id);
|
|
await repository.loadTemplates();
|
|
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Herunterladen der Notizen: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(
|
|
FailOperation(message: "Notizen konnten nicht heruntergeladen werden."),
|
|
);
|
|
|
|
emit.call(NoteLoadingFailed());
|
|
}
|
|
}
|
|
|
|
Future<void> _add(AddNote event, Emitter<NoteState> emit) async {
|
|
opBloc.add(LoadOperation());
|
|
|
|
try {
|
|
await repository.addNote(event.deliveryId, event.note);
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Hinzufügen der Notiz: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(FailOperation(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> _edit(EditNote event, Emitter<NoteState> emit) async {
|
|
opBloc.add(LoadOperation());
|
|
|
|
try {
|
|
await repository.editNote(event.noteId, event.content);
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Hinzufügen der Notiz: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(FailOperation(message: e.toString()));
|
|
}
|
|
}
|
|
|
|
Future<void> _remove(RemoveNote event, Emitter<NoteState> emit) async {
|
|
opBloc.add(LoadOperation());
|
|
|
|
try {
|
|
await repository.deleteNote(event.noteId);
|
|
opBloc.add(FinishOperation());
|
|
} catch (e, st) {
|
|
debugPrint("Fehler beim Hinzufügen der Notiz: $e");
|
|
debugPrint(st.toString());
|
|
|
|
opBloc.add(
|
|
FailOperation(message: "Notizen konnte nicht gelöscht werden."),
|
|
);
|
|
}
|
|
}
|
|
}
|