Finished custom sorting of deliveries

This commit is contained in:
Dennis Nemec
2026-01-07 15:19:34 +01:00
parent 9111dc92db
commit 622967e5c1
8 changed files with 369 additions and 41 deletions

View File

@ -4,8 +4,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_event.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_state.dart';
import 'package:hl_lieferservice/feature/delivery/overview/model/sorting_information.dart';
import 'package:hl_lieferservice/feature/delivery/overview/repository/tour_repository.dart';
import 'package:hl_lieferservice/feature/delivery/overview/service/distance_service.dart';
import 'package:hl_lieferservice/feature/delivery/overview/service/reorder_service.dart';
import 'package:hl_lieferservice/model/tour.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
@ -50,6 +52,8 @@ class TourBloc extends Bloc<TourEvent, TourState> {
on<FinishDeliveryEvent>(_finishDelivery);
on<TourUpdated>(_updated);
on<RequestDeliveryDistanceEvent>(_calculateDistances);
on<RequestSortingInformationEvent>(_requestSortingInformation);
on<ReorderDeliveryEvent>(_reorderDelivery);
}
@override
@ -59,6 +63,36 @@ class TourBloc extends Bloc<TourEvent, TourState> {
return super.close();
}
void _reorderDelivery(
ReorderDeliveryEvent event,
Emitter<TourState> emit,
) async {
final currentState = state;
if (currentState is TourLoaded) {
int newPosition = event.newPosition == currentState.sortingInformation.sorting.length ? event.newPosition - 1 : event.newPosition;
SortingInformation informationOld = currentState.sortingInformation.sorting
.firstWhere((info) => info.position == event.oldPosition);
SortingInformation information = currentState
.sortingInformation
.sorting
.firstWhere((info) => info.position == newPosition);
information.position = event.oldPosition;
informationOld.position = newPosition;
await ReorderService().saveSortingInformation(
currentState.sortingInformation,
);
emit(
currentState.copyWith(
sortingInformation: currentState.sortingInformation.copyWith(),
),
);
}
}
void _calculateDistances(
RequestDeliveryDistanceEvent event,
Emitter<TourState> emit,
@ -79,15 +113,104 @@ class TourBloc extends Bloc<TourEvent, TourState> {
debugPrint("Fehler beim Berechnen der Distanzen: $e");
opBloc.add(FailOperation(message: "Fehler beim Berechnen der Distanzen"));
return;
} finally {
// Independent of error state fetch the sorting information
add(
RequestSortingInformationEvent(
tour: event.tour,
payments: event.payments,
distances: distances,
),
);
}
}
emit(
TourLoaded(
tour: event.tour,
paymentOptions: event.payments,
distances: distances,
),
);
void _requestSortingInformation(
RequestSortingInformationEvent event,
Emitter<TourState> emit,
) async {
try {
ReorderService service = ReorderService();
SortingInformationContainer container = SortingInformationContainer(
sorting: [],
);
// Create empty default value if it does not exist yet
if (!service.orderInformationExist()) {
await service.initializeTour(event.tour);
}
// Populate the container with information. If the file did not exist then it
// now contains the standard values.
container = await service.loadSortingInformation();
bool inconsistent = false;
for (final delivery in event.tour.deliveries) {
int info = container.sorting.indexWhere(
(info) => info.deliveryId == delivery.id,
);
int max = container.sorting.fold(0, (acc, element) {
if (element.position > acc) {
return element.position;
}
return acc;
});
// not found, so add it to the list
if (info == -1) {
inconsistent = true;
container.sorting.add(
SortingInformation(
deliveryId: delivery.id,
position: container.sorting.isEmpty ? 0 : max + 1,
),
);
}
}
// if new deliveries were added then save the information with the newly
// populated container
if (inconsistent) {
await service.saveSortingInformation(container);
}
emit(
TourLoaded(
tour: event.tour,
paymentOptions: event.payments,
sortingInformation: container,
distances: event.distances,
),
);
} catch (e, st) {
debugPrint("Fehler beim Lesen der Datei: $e");
debugPrint("$st");
opBloc.add(
FailOperation(
message:
"Fehler beim Laden der Sortierung. Es wird ohne Sortierung fortgefahren",
),
);
SortingInformationContainer container = SortingInformationContainer(
sorting: [],
);
for (final (index, delivery) in event.tour.deliveries.indexed) {
container.sorting.add(
SortingInformation(deliveryId: delivery.id, position: index),
);
}
emit(
TourLoaded(
tour: event.tour,
paymentOptions: event.payments,
sortingInformation: container,
distances: event.distances,
),
);
}
}
void _updated(TourUpdated event, Emitter<TourState> emit) {
@ -97,16 +220,17 @@ class TourBloc extends Bloc<TourEvent, TourState> {
event.payments.map((payment) => payment.copyWith()).toList();
if (currentState is TourLoaded) {
debugPrint("TEST UPDATE");
emit(
TourLoaded(
tour: tour,
paymentOptions: payments,
distances: Map<String, double>.from(currentState.distances ?? {}),
sortingInformation: currentState.sortingInformation,
),
);
}
// Download distances if tour has previously fetched by API
if (currentState is TourLoading) {
add(
RequestDeliveryDistanceEvent(tour: tour.copyWith(), payments: payments),

View File

@ -19,6 +19,21 @@ class RequestDeliveryDistanceEvent extends TourEvent {
RequestDeliveryDistanceEvent({required this.tour, required this.payments});
}
class RequestSortingInformationEvent extends TourEvent {
Tour tour;
List<Payment> payments;
Map<String, double>? distances;
RequestSortingInformationEvent({required this.tour, required this.payments, this.distances});
}
class ReorderDeliveryEvent extends TourEvent {
int newPosition;
int oldPosition;
ReorderDeliveryEvent({required this.newPosition, required this.oldPosition});
}
class TourUpdated extends TourEvent {
Tour tour;
List<Payment> payments;

View File

@ -1,3 +1,5 @@
import 'package:hl_lieferservice/feature/delivery/overview/model/sorting_information.dart';
import '../../../../model/tour.dart';
abstract class TourState {}
@ -13,26 +15,42 @@ class TourRequestingDistances extends TourState {
TourRequestingDistances({required this.tour, required this.payments});
}
class TourRequestingSortingInformation extends TourState {
Tour tour;
Map<String, double>? distances;
List<Payment> paymentOptions;
TourRequestingSortingInformation({
required this.tour,
this.distances,
required this.paymentOptions,
});
}
class TourLoaded extends TourState {
Tour tour;
Map<String, double>? distances;
List<Payment> paymentOptions;
SortingInformationContainer sortingInformation;
TourLoaded({
required this.tour,
this.distances,
required this.paymentOptions,
required this.sortingInformation
});
TourLoaded copyWith({
Tour? tour,
Map<String, double>? distances,
List<Payment>? paymentOptions,
SortingInformationContainer? sortingInformation
}) {
return TourLoaded(
tour: tour ?? this.tour,
distances: distances ?? this.distances,
paymentOptions: paymentOptions ?? this.paymentOptions,
sortingInformation: sortingInformation ?? this.sortingInformation
);
}
}