Added Streams to TourRepository

This commit is contained in:
Dennis Nemec
2026-01-03 01:29:21 +01:00
parent edb8676f5a
commit 9111dc92db
43 changed files with 1232 additions and 931 deletions

View File

@ -1,8 +1,8 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
@ -13,9 +13,23 @@ import 'package:hl_lieferservice/feature/delivery/detail/repository/note_reposit
class NoteBloc extends Bloc<NoteEvent, NoteState> {
final NoteRepository repository;
final OperationBloc opBloc;
final String deliveryId;
NoteBloc({required this.repository, required this.opBloc})
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);
@ -23,61 +37,41 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
on<AddImageNote>(_upload);
on<RemoveImageNote>(_removeImage);
on<ResetNotes>(_reset);
on<AddNoteOffline>(_addOffline);
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> _addOffline(AddNoteOffline event,
Emitter<NoteState> emit,) async {
if (state is NoteInitial) {
emit(
NoteLoadedBase(
notes: [Note(content: event.note, id: int.parse(event.noteId))],
),
);
}
if (state is NoteLoadedBase) {
emit(
NoteLoadedBase(
notes: [
...(state as NoteLoadedBase).notes,
Note(content: event.note, id: int.parse(event.noteId)),
],
),
);
}
if (state is NoteLoaded) {
final current = state as NoteLoaded;
emit(NoteLoaded(notes: [...current.notes, Note(content: event.note, id: int.parse(event.noteId))],
templates: [...current.templates],
images: [...current.images]));
}
}
Future<void> _removeImage(RemoveImageNote event,
Emitter<NoteState> emit,) async {
opBloc.add(LoadOperation());
try {
final currentState = state;
await repository.deleteImage(event.deliveryId, event.objectId);
if (currentState is NoteLoaded) {
emit.call(
currentState.copyWith(
images:
currentState.images
.where((image) => image.$1.objectId != event.objectId)
.toList(),
),
);
}
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Löschen des Bildes: $e");
@ -91,18 +85,8 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
opBloc.add(LoadOperation());
try {
final currentState = state;
Uint8List imageBytes = await event.file.readAsBytes();
ImageNote note = await repository.addImage(event.deliveryId, imageBytes);
if (currentState is NoteLoaded) {
emit.call(
currentState.copyWith(
images: [...currentState.images, (note, imageBytes)],
),
);
}
await repository.addImage(event.deliveryId, imageBytes);
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Hinzufügen des Bildes: $e");
@ -118,20 +102,12 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
try {
List<String> urls =
event.delivery.images.map((image) => image.url).toList();
List<Note> notes = await repository.loadNotes(event.delivery.id);
List<NoteTemplate> templates = await repository.loadTemplates();
List<Uint8List> images = await repository.loadImages(urls);
emit.call(
NoteLoaded(
notes: notes,
templates: templates,
images: List.generate(
images.length,
(index) => (event.delivery.images[index], images[index]),
),
),
);
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");
@ -149,14 +125,7 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
opBloc.add(LoadOperation());
try {
final currentState = state;
Note note = await repository.addNote(event.deliveryId, event.note);
if (currentState is NoteLoaded) {
List<Note> refreshedNotes = [...currentState.notes, note];
emit.call(currentState.copyWith(notes: refreshedNotes));
}
await repository.addNote(event.deliveryId, event.note);
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Hinzufügen der Notiz: $e");
@ -170,19 +139,7 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
opBloc.add(LoadOperation());
try {
final currentState = state;
await repository.editNote(event.noteId, event.content);
if (currentState is NoteLoaded) {
List<Note> refreshedNotes = [
...currentState.notes.where(
(note) => note.id != int.parse(event.noteId),
),
Note(content: event.content, id: int.parse(event.noteId)),
];
emit.call(currentState.copyWith(notes: refreshedNotes));
}
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Hinzufügen der Notiz: $e");
@ -196,18 +153,7 @@ class NoteBloc extends Bloc<NoteEvent, NoteState> {
opBloc.add(LoadOperation());
try {
final currentState = state;
await repository.deleteNote(event.noteId);
if (currentState is NoteLoaded) {
List<Note> refreshedNotes =
currentState.notes
.where((note) => note.id != int.parse(event.noteId))
.toList();
emit.call(currentState.copyWith(notes: refreshedNotes));
}
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Hinzufügen der Notiz: $e");