Finalized sorting in delivery overview

This commit is contained in:
Dennis Nemec
2026-01-09 12:16:52 +01:00
parent b481a45afc
commit ecf8745762
3 changed files with 137 additions and 79 deletions

View File

@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';

View File

@ -3,21 +3,16 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_bloc.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/presentation/delivery_overview.dart';
import 'package:hl_lieferservice/model/delivery.dart';
import 'delivery_item.dart';
class DeliveryList extends StatefulWidget {
final List<Delivery> deliveries;
final Map<String, double> distances;
final List<SortingInformation> sortingInformation;
final int? selectedCarId;
final SortType sortType;
const DeliveryList({
super.key,
required this.deliveries,
required this.distances,
required this.sortingInformation,
});
const DeliveryList({super.key, this.selectedCarId, required this.sortType});
@override
State<StatefulWidget> createState() => _DeliveryListState();
@ -25,34 +20,98 @@ class DeliveryList extends StatefulWidget {
class _DeliveryListState extends State<DeliveryList> {
@override
Widget build(BuildContext context) {
if (widget.deliveries.isEmpty) {
return ListView(
children: [Center(child: const Text("Keine Auslieferungen gefunden"))],
);
}
void initState() {
super.initState();
}
Widget _showCustomSortedList(
List<Delivery> deliveries,
List<SortingInformation> sortingInformation,
Map<String, double> distances,
) {
List<SortingInformation> sorted = [...sortingInformation];
sorted.sort((a, b) => a.position.compareTo(b.position));
return ListView.separated(
separatorBuilder: (context, index) => const Divider(height: 0),
itemBuilder: (context, index) {
SortingInformation info = sorted[index];
Delivery delivery = deliveries.firstWhere(
(delivery) => info.deliveryId == delivery.id,
);
return DeliveryListItem(
delivery: delivery,
distance: distances[delivery.id] ?? 0.0,
);
},
itemCount: sorted.length,
);
}
@override
Widget build(BuildContext context) {
return BlocBuilder<TourBloc, TourState>(
builder: (context, state) {
final currentState = state;
if (currentState is TourLoaded) {
List<SortingInformation> sorted = [...currentState.sortingInformation.sorting];
sorted.sort((a, b) => a.position.compareTo(b.position));
List<Delivery> deliveries =
currentState.tour.deliveries
.where(
(delivery) =>
delivery.carId == widget.selectedCarId &&
delivery.allArticlesScanned(),
)
.toList();
if (deliveries.isEmpty) {
return ListView(
children: [
Center(child: const Text("Keine Auslieferungen gefunden")),
],
);
}
switch (widget.sortType) {
case SortType.custom:
return _showCustomSortedList(
deliveries,
currentState.sortingInformation.sorting,
currentState.distances ?? {},
);
case SortType.nameAsc:
deliveries.sort(
(a, b) => a.customer.name.compareTo(b.customer.name),
);
break;
case SortType.nameDesc:
deliveries.sort(
(a, b) => b.customer.name.compareTo(a.customer.name),
);
break;
case SortType.distance:
deliveries.sort(
(a, b) => (currentState.distances![a.id] ?? 0.0).compareTo(
currentState.distances![b.id] ?? 0.0,
),
);
break;
}
return ListView.separated(
separatorBuilder: (context, index) => const Divider(height: 0),
itemBuilder: (context, index) {
SortingInformation info = sorted[index];
Delivery delivery = currentState.tour.deliveries.firstWhere(
(delivery) => info.deliveryId == delivery.id,
);
Delivery delivery = deliveries[index];
return DeliveryListItem(
delivery: delivery,
distance: currentState.distances?[delivery.id] ?? 0.0,
);
},
itemCount: sorted.length,
itemCount: deliveries.length,
);
}

View File

@ -7,10 +7,10 @@ import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery
import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery_overview_custom_sort.dart';
import 'package:hl_lieferservice/model/tour.dart';
import '../../../../model/delivery.dart';
import '../../../authentication/bloc/auth_bloc.dart';
import '../../../authentication/bloc/auth_state.dart';
import '../bloc/tour_state.dart';
enum SortType { nameAsc, nameDesc, distance, custom }
class DeliveryOverview extends StatefulWidget {
const DeliveryOverview({
@ -28,7 +28,7 @@ class DeliveryOverview extends StatefulWidget {
class _DeliveryOverviewState extends State<DeliveryOverview> {
int? _selectedCarId;
late List<Delivery> _deliveries;
late SortType _sortType;
@override
void initState() {
@ -36,17 +36,7 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
// Select the first car for initialization
_selectedCarId = widget.tour.driver.cars.firstOrNull?.id;
_updateDeliveries();
}
@override
void didUpdateWidget(covariant DeliveryOverview oldWidget) {
super.didUpdateWidget(oldWidget);
_updateDeliveries();
}
void _updateDeliveries() {
_deliveries = [...widget.tour.deliveries];
_sortType = SortType.nameAsc;
}
Future<void> _loadTour() async {
@ -102,6 +92,11 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
);
}
/// Highlight the text of the active sorting type.
TextStyle? _popupItemTextStyle() {
return TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold);
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(
@ -128,51 +123,67 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
),
],
),
PopupMenuButton<String>(
onSelected: (String value) {
setState(() {
if (value == "name-asc") {
PopupMenuButton<SortType>(
onSelected: (SortType value) {
switch (value) {
case SortType.nameAsc:
setState(() {
_deliveries.sort();
_sortType = SortType.nameAsc;
});
}
if (value == "name-desc") {
break;
case SortType.nameDesc:
setState(() {
_deliveries = _deliveries.reversed.toList();
_sortType = SortType.nameDesc;
});
break;
case SortType.distance:
setState(() {
_sortType = SortType.distance;
});
break;
case SortType.custom:
setState(() {
_sortType = SortType.custom;
});
}
if (value == "custom") {
showDialog(
context: context,
fullscreenDialog: true,
builder: (context) => CustomSortDialog(),
);
}
if (value == "distance") {
// TODO: muss noch implementiert werden
}
});
break;
}
},
itemBuilder:
(BuildContext context) => <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: 'name-asc',
child: Text('Name (A-Z)'),
(BuildContext context) => <PopupMenuEntry<SortType>>[
PopupMenuItem<SortType>(
value: SortType.nameAsc,
child: Text(
'Name (A-Z)',
style: _sortType == SortType.nameAsc ? _popupItemTextStyle() : null,
),
),
PopupMenuItem<String>(
value: 'name-desc',
child: Text('Name (Z-A)'),
PopupMenuItem<SortType>(
value: SortType.nameDesc,
child: Text(
'Name (Z-A)',
style: _sortType == SortType.nameDesc ? _popupItemTextStyle() : null,
),
),
PopupMenuItem<String>(
value: 'distance',
child: Text('Entfernung'),
PopupMenuItem<SortType>(
value: SortType.distance,
child: Text(
'Entfernung',
style: _sortType == SortType.distance ? _popupItemTextStyle() : null,
),
),
PopupMenuItem<String>(
value: 'custom',
child: Text('Eigene Sortierung'),
PopupMenuItem<SortType>(
value: SortType.custom,
child: Text(
'Eigene Sortierung',
style: _sortType == SortType.custom ? _popupItemTextStyle() : null,
),
),
],
child: Icon(Icons.filter_list),
@ -186,19 +197,8 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
),
Expanded(
child: DeliveryList(
distances: widget.distances,
sortingInformation:
(context.read<TourBloc>().state as TourLoaded)
.sortingInformation
.sorting,
deliveries:
_deliveries
.where(
(delivery) =>
delivery.carId == _selectedCarId &&
delivery.allArticlesScanned(),
)
.toList(),
selectedCarId: _selectedCarId,
sortType: _sortType,
),
),
],