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,353 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/dto/discount_add_response.dart';
import 'package:hl_lieferservice/dto/discount_update_response.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/delivery_event.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/delivery_state.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_event.dart';
import 'package:hl_lieferservice/feature/delivery/detail/repository/delivery_repository.dart';
import 'package:hl_lieferservice/feature/delivery/detail/repository/note_repository.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
import '../../../../dto/discount_remove_response.dart';
import '../../../../model/article.dart';
import '../../../../model/delivery.dart' as model;
class DeliveryBloc extends Bloc<DeliveryEvent, DeliveryState> {
OperationBloc opBloc;
NoteBloc noteBloc;
DeliveryRepository repository;
NoteRepository noteRepository;
DeliveryBloc({
required this.opBloc,
required this.repository,
required this.noteRepository,
required this.noteBloc
}) : super(DeliveryInitial()) {
on<UnscanArticleEvent>(_unscan);
on<ResetScanAmountEvent>(_resetAmount);
on<LoadDeliveryEvent>(_load);
on<AddDiscountEvent>(_addDiscount);
on<RemoveDiscountEvent>(_removeDiscount);
on<UpdateDiscountEvent>(_updateDiscount);
on<UpdateDeliveryOptionEvent>(_updateDeliveryOptions);
on<UpdateSelectedPaymentMethodEvent>(_updatePayment);
on<FinishDeliveryEvent>(_finishDelivery);
}
void _finishDelivery(FinishDeliveryEvent event,
Emitter<DeliveryState> emit,) async {
final currentState = state;
opBloc.add(LoadOperation());
if (currentState is DeliveryLoaded) {
try {
model.Delivery newDelivery = event.delivery.copyWith();
newDelivery.state = model.DeliveryState.finished;
for (final option in event.delivery.options) {
debugPrint("VALUE=${option.value};KEY=${option.key}");
}
await repository.updateDelivery(newDelivery);
await noteRepository.addNamedImage(
event.delivery.id,
event.driverSignature,
"delivery_${event.delivery.id}_signature_driver.jpg",
);
await noteRepository.addNamedImage(
event.delivery.id,
event.customerSignature,
"delivery_${event.delivery.id}_signature_customer.jpg",
);
emit(DeliveryFinished(delivery: newDelivery));
opBloc.add(FinishOperation());
} catch (e, st) {
opBloc.add(FailOperation(message: "Failed to update delivery"));
debugPrint(st.toString());
}
}
}
void _updatePayment(UpdateSelectedPaymentMethodEvent event,
Emitter<DeliveryState> emit,) {
final currentState = state;
if (currentState is DeliveryLoaded) {
emit(
DeliveryLoaded(
delivery: currentState.delivery.copyWith(payment: event.payment),
),
);
}
}
void _updateDeliveryOptions(UpdateDeliveryOptionEvent event,
Emitter<DeliveryState> emit,) {
final currentState = state;
if (currentState is DeliveryLoaded) {
List<model.DeliveryOption> options =
currentState.delivery.options.map((option) {
if (option.key == event.key) {
if (option.numerical) {
return option.copyWith(value: event.value);
} else {
return option.copyWith(value: event.value == true ? "1" : "0");
}
}
return option;
}).toList();
emit(
DeliveryLoaded(
delivery: currentState.delivery.copyWith(options: options),
),
);
}
}
void _updateDiscount(UpdateDiscountEvent event,
Emitter<DeliveryState> emit,) async {
opBloc.add(LoadOperation());
try {
final currentState = state;
if (currentState is DeliveryLoaded) {
DiscountUpdateResponseDTO response = await repository.updateDiscount(
event.deliveryId,
event.reason,
event.value,
);
model.Delivery delivery = currentState.delivery;
if (response.values?.receipt != null) {
delivery.totalNetValue = response.values!.receipt.net;
delivery.totalGrossValue = response.values!.receipt.gross;
}
String discountArticleNumber = delivery.discount!.article.articleNumber;
delivery.discount = model.Discount(
article:
response.values?.article != null
? Article.fromDTO(response.values!.article)
: delivery.discount!.article,
note:
response.values?.note != null
? response.values!.note.noteDescription
: delivery.discount!.note,
noteId:
response.values?.note != null
? response.values!.note.rowId
: delivery.discount!.noteId,
);
delivery.articles = [
...delivery.articles.where(
(article) => article.articleNumber != discountArticleNumber,
),
delivery.discount!.article,
];
emit(currentState.copyWith(delivery));
opBloc.add(FinishOperation());
}
} catch (e, st) {
debugPrint(
"Fehler beim Hinzufügen eins Discounts zur Lieferung: ${event
.deliveryId}:",
);
debugPrint("$e");
debugPrint("$st");
opBloc.add(
FailOperation(message: "Fehler beim Hinzufügen des Discounts: $e"),
);
}
}
void _removeDiscount(RemoveDiscountEvent event,
Emitter<DeliveryState> emit,) async {
opBloc.add(LoadOperation());
try {
final currentState = state;
if (currentState is DeliveryLoaded) {
model.Delivery delivery = currentState.delivery;
DiscountRemoveResponseDTO response = await repository.removeDiscount(
event.deliveryId,
);
delivery.articles =
delivery.articles
.where(
(article) =>
article.internalId !=
delivery.discount?.article.internalId,
)
.toList();
delivery.discount = null;
delivery.totalGrossValue = response.receipt.gross;
delivery.totalNetValue = response.receipt.net;
emit(currentState.copyWith(delivery));
opBloc.add(FinishOperation());
}
} catch (e, st) {
debugPrint(
"Fehler beim Löschen des Discounts der Lieferung: ${event.deliveryId}:",
);
debugPrint("$e");
debugPrint("$st");
opBloc.add(
FailOperation(message: "Fehler beim Löschen des Discounts: $e"),
);
}
}
void _addDiscount(AddDiscountEvent event, Emitter<DeliveryState> emit) async {
opBloc.add(LoadOperation());
try {
final currentState = state;
if (currentState is DeliveryLoaded) {
DiscountAddResponseDTO response = await repository.addDiscount(
event.deliveryId,
event.reason,
event.value,
);
model.Delivery delivery = currentState.delivery;
delivery.totalNetValue = response.values.receipt.net;
delivery.totalGrossValue = response.values.receipt.gross;
delivery.discount = model.Discount(
article: Article.fromDTO(response.values.article),
note: response.values.note.noteDescription,
noteId: response.values.note.rowId,
);
noteBloc.add(AddNoteOffline(note: response.values.note.noteDescription,
deliveryId: delivery.id,
noteId: response.values.note.rowId));
delivery.articles = [...delivery.articles, delivery.discount!.article];
emit(currentState.copyWith(delivery));
opBloc.add(FinishOperation());
}
} catch (e, st) {
debugPrint(
"Fehler beim Hinzufügen eins Discounts zur Lieferung: ${event
.deliveryId}:",
);
debugPrint("$e");
debugPrint("$st");
opBloc.add(
FailOperation(message: "Fehler beim Hinzufügen des Discounts: $e"),
);
}
}
void _load(LoadDeliveryEvent event, Emitter<DeliveryState> emit) async {
debugPrint("Discount; ${event.delivery.discount?.note}");
emit(DeliveryLoaded(delivery: event.delivery));
}
void _unscan(UnscanArticleEvent event, Emitter<DeliveryState> emit) async {
opBloc.add(LoadOperation());
try {
String? noteId = await repository.unscan(
event.articleId,
event.newAmount,
event.reason,
);
if (noteId != null) {
final currentState = state;
if (currentState is DeliveryLoaded) {
Article article = currentState.delivery.articles.firstWhere(
(article) => article.internalId == int.parse(event.articleId),
);
article.removeNoteId = noteId;
article.scannedRemovedAmount += event.newAmount;
article.scannedAmount -= event.newAmount;
List<Article> articles = [
...currentState.delivery.articles.where(
(article) => article.internalId != int.parse(event.articleId),
),
article,
];
currentState.delivery.articles = articles;
emit.call(currentState.copyWith(currentState.delivery));
}
}
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Unscan des Artikels: ${event.articleId}:");
debugPrint("$e");
debugPrint("$st");
opBloc.add(FailOperation(message: "Fehler beim Unscan des Artikels: $e"));
}
}
void _resetAmount(ResetScanAmountEvent event,
Emitter<DeliveryState> emit,) async {
opBloc.add(LoadOperation());
try {
final currentState = state;
await repository.resetScan(event.articleId);
if (currentState is DeliveryLoaded) {
Article article = currentState.delivery.articles.firstWhere(
(article) => article.internalId == int.parse(event.articleId),
);
article.removeNoteId = null;
article.scannedRemovedAmount = 0;
article.scannedAmount = article.amount;
List<Article> articles = [
...currentState.delivery.articles.where(
(article) => article.internalId != int.parse(event.articleId),
),
article,
];
currentState.delivery.articles = articles;
emit.call(currentState.copyWith(currentState.delivery));
}
opBloc.add(FinishOperation());
} catch (e, st) {
debugPrint("Fehler beim Unscan des Artikels: ${event.articleId}:");
debugPrint("$e");
debugPrint("$st");
opBloc.add(FailOperation(message: "Fehler beim Zurücksetzen: $e"));
}
}
}

View File

@ -1,85 +0,0 @@
import 'dart:typed_data';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:hl_lieferservice/model/tour.dart';
abstract class DeliveryEvent {}
class LoadDeliveryEvent extends DeliveryEvent {
LoadDeliveryEvent({required this.delivery});
Delivery delivery;
}
class UnscanArticleEvent extends DeliveryEvent {
UnscanArticleEvent({
required this.articleId,
required this.newAmount,
required this.reason,
});
String articleId;
String reason;
int newAmount;
}
class ResetScanAmountEvent extends DeliveryEvent {
ResetScanAmountEvent({required this.articleId});
String articleId;
}
class AddDiscountEvent extends DeliveryEvent {
AddDiscountEvent({
required this.deliveryId,
required this.value,
required this.reason,
});
String deliveryId;
String reason;
int value;
}
class RemoveDiscountEvent extends DeliveryEvent {
RemoveDiscountEvent({required this.deliveryId});
String deliveryId;
}
class UpdateDiscountEvent extends DeliveryEvent {
UpdateDiscountEvent({
required this.deliveryId,
required this.value,
required this.reason,
});
String deliveryId;
String? reason;
int? value;
}
class UpdateDeliveryOptionEvent extends DeliveryEvent {
UpdateDeliveryOptionEvent({required this.key, required this.value});
String key;
dynamic value;
}
class UpdateSelectedPaymentMethodEvent extends DeliveryEvent {
UpdateSelectedPaymentMethodEvent({required this.payment});
Payment payment;
}
class FinishDeliveryEvent extends DeliveryEvent {
FinishDeliveryEvent({
required this.delivery,
required this.driverSignature,
required this.customerSignature,
});
Delivery delivery;
Uint8List customerSignature;
Uint8List driverSignature;
}

View File

@ -1,25 +0,0 @@
import 'package:hl_lieferservice/model/delivery.dart';
abstract class DeliveryState {}
class DeliveryInitial extends DeliveryState {}
class DeliveryLoaded extends DeliveryState {
DeliveryLoaded({required this.delivery});
Delivery delivery;
DeliveryLoaded copyWith(Delivery? delivery) {
return DeliveryLoaded(delivery: delivery ?? this.delivery);
}
}
class DeliveryFinished extends DeliveryState {
DeliveryFinished({required this.delivery});
Delivery delivery;
DeliveryFinished copyWith(Delivery? delivery) {
return DeliveryFinished(delivery: delivery ?? this.delivery);
}
}

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");

View File

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:image_picker/image_picker.dart';
@ -51,4 +53,16 @@ class RemoveImageNote extends NoteEvent {
final String objectId;
final String deliveryId;
}
class NotesUpdated extends NoteEvent {
final List<Note> notes;
NotesUpdated({required this.notes});
}
class ImageUpdated extends NoteEvent {
final List<ImageNote> images;
ImageUpdated({required this.images});
}

View File

@ -20,18 +20,18 @@ class NoteLoadedBase extends NoteState {
class NoteLoaded extends NoteLoadedBase {
NoteLoaded({
required this.templates,
required this.images,
this.templates,
this.images,
required super.notes,
});
List<NoteTemplate> templates;
List<(ImageNote, Uint8List)> images;
List<NoteTemplate>? templates;
List<ImageNote>? images;
NoteLoaded copyWith({
List<Note>? notes,
List<NoteTemplate>? templates,
List<(ImageNote, Uint8List)>? images,
List<ImageNote>? images,
}) {
return NoteLoaded(
notes: notes ?? this.notes,