Files
Holzleitner-Lieferservice-App/lib/feature/delivery/detail/bloc/note_bloc.dart
2026-01-03 01:29:21 +01:00

168 lines
4.6 KiB
Dart

import 'dart:async';
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 '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? _noteSubscription;
StreamSubscription? _imageSubscription;
NoteBloc({required this.repository, required this.opBloc, required this.deliveryId})
: super(NoteInitial()) {
repository.loadNotes(deliveryId);
_noteSubscription = repository.notes.listen((notes) {
add(NotesUpdated(notes: notes));
});
_imageSubscription = repository.images.listen((images) {
add(ImageUpdated(images: images));
});
on<LoadNote>(_load);
on<AddNote>(_add);
on<EditNote>(_edit);
on<RemoveNote>(_remove);
on<AddImageNote>(_upload);
on<RemoveImageNote>(_removeImage);
on<ResetNotes>(_reset);
on<NotesUpdated>(_noteUpdated);
on<ImageUpdated>(_imageUpdated);
}
@override
Future<void> close() {
_noteSubscription?.cancel();
_imageSubscription?.cancel();
return super.close();
}
Future<void> _imageUpdated(ImageUpdated event, Emitter<NoteState> emit) async {
final currentState = state;
if (currentState is NoteLoaded) {
emit.call(currentState.copyWith(images: event.images));
}
}
Future<void> _noteUpdated(NotesUpdated event, Emitter<NoteState> emit) async {
emit(NoteLoaded(notes: event.notes));
}
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."),
);
}
}
}