From ecf8745762d1a90c0be3a9ef14cdd1155dcad23d Mon Sep 17 00:00:00 2001 From: Dennis Nemec Date: Fri, 9 Jan 2026 12:16:52 +0100 Subject: [PATCH] Finalized sorting in delivery overview --- .../delivery/detail/bloc/note_bloc.dart | 1 - .../overview/presentation/delivery_list.dart | 103 ++++++++++++---- .../presentation/delivery_overview.dart | 112 +++++++++--------- 3 files changed, 137 insertions(+), 79 deletions(-) diff --git a/lib/feature/delivery/detail/bloc/note_bloc.dart b/lib/feature/delivery/detail/bloc/note_bloc.dart index 1c479d0..14f6b9d 100644 --- a/lib/feature/delivery/detail/bloc/note_bloc.dart +++ b/lib/feature/delivery/detail/bloc/note_bloc.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/cupertino.dart'; diff --git a/lib/feature/delivery/overview/presentation/delivery_list.dart b/lib/feature/delivery/overview/presentation/delivery_list.dart index fc7eee2..9d76c25 100644 --- a/lib/feature/delivery/overview/presentation/delivery_list.dart +++ b/lib/feature/delivery/overview/presentation/delivery_list.dart @@ -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 deliveries; - final Map distances; - final List 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 createState() => _DeliveryListState(); @@ -25,34 +20,98 @@ class DeliveryList extends StatefulWidget { class _DeliveryListState extends State { @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 deliveries, + List sortingInformation, + Map distances, + ) { + List 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( builder: (context, state) { final currentState = state; if (currentState is TourLoaded) { - List sorted = [...currentState.sortingInformation.sorting]; - sorted.sort((a, b) => a.position.compareTo(b.position)); + List 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, ); } diff --git a/lib/feature/delivery/overview/presentation/delivery_overview.dart b/lib/feature/delivery/overview/presentation/delivery_overview.dart index e587a05..276a5e6 100644 --- a/lib/feature/delivery/overview/presentation/delivery_overview.dart +++ b/lib/feature/delivery/overview/presentation/delivery_overview.dart @@ -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 { int? _selectedCarId; - late List _deliveries; + late SortType _sortType; @override void initState() { @@ -36,17 +36,7 @@ class _DeliveryOverviewState extends State { // 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 _loadTour() async { @@ -102,6 +92,11 @@ class _DeliveryOverviewState extends State { ); } + /// 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 { ), ], ), - PopupMenuButton( - onSelected: (String value) { - setState(() { - if (value == "name-asc") { + PopupMenuButton( + 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) => >[ - PopupMenuItem( - value: 'name-asc', - child: Text('Name (A-Z)'), + (BuildContext context) => >[ + PopupMenuItem( + value: SortType.nameAsc, + child: Text( + 'Name (A-Z)', + style: _sortType == SortType.nameAsc ? _popupItemTextStyle() : null, + ), ), - PopupMenuItem( - value: 'name-desc', - child: Text('Name (Z-A)'), + PopupMenuItem( + value: SortType.nameDesc, + child: Text( + 'Name (Z-A)', + style: _sortType == SortType.nameDesc ? _popupItemTextStyle() : null, + ), ), - PopupMenuItem( - value: 'distance', - child: Text('Entfernung'), + PopupMenuItem( + value: SortType.distance, + child: Text( + 'Entfernung', + style: _sortType == SortType.distance ? _popupItemTextStyle() : null, + ), ), - PopupMenuItem( - value: 'custom', - child: Text('Eigene Sortierung'), + PopupMenuItem( + 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 { ), Expanded( child: DeliveryList( - distances: widget.distances, - sortingInformation: - (context.read().state as TourLoaded) - .sortingInformation - .sorting, - deliveries: - _deliveries - .where( - (delivery) => - delivery.carId == _selectedCarId && - delivery.allArticlesScanned(), - ) - .toList(), + selectedCarId: _selectedCarId, + sortType: _sortType, ), ), ],