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:async';
import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/cupertino.dart'; 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_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_state.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/model/sorting_information.dart';
import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery_overview.dart';
import 'package:hl_lieferservice/model/delivery.dart'; import 'package:hl_lieferservice/model/delivery.dart';
import 'delivery_item.dart'; import 'delivery_item.dart';
class DeliveryList extends StatefulWidget { class DeliveryList extends StatefulWidget {
final List<Delivery> deliveries; final int? selectedCarId;
final Map<String, double> distances; final SortType sortType;
final List<SortingInformation> sortingInformation;
const DeliveryList({ const DeliveryList({super.key, this.selectedCarId, required this.sortType});
super.key,
required this.deliveries,
required this.distances,
required this.sortingInformation,
});
@override @override
State<StatefulWidget> createState() => _DeliveryListState(); State<StatefulWidget> createState() => _DeliveryListState();
@ -25,34 +20,98 @@ class DeliveryList extends StatefulWidget {
class _DeliveryListState extends State<DeliveryList> { class _DeliveryListState extends State<DeliveryList> {
@override @override
Widget build(BuildContext context) { void initState() {
if (widget.deliveries.isEmpty) { super.initState();
return ListView(
children: [Center(child: const Text("Keine Auslieferungen gefunden"))],
);
} }
return BlocBuilder<TourBloc, TourState>( Widget _showCustomSortedList(
builder: (context, state) { List<Delivery> deliveries,
final currentState = state; List<SortingInformation> sortingInformation,
if (currentState is TourLoaded) { Map<String, double> distances,
List<SortingInformation> sorted = [...currentState.sortingInformation.sorting]; ) {
List<SortingInformation> sorted = [...sortingInformation];
sorted.sort((a, b) => a.position.compareTo(b.position)); sorted.sort((a, b) => a.position.compareTo(b.position));
return ListView.separated( return ListView.separated(
separatorBuilder: (context, index) => const Divider(height: 0), separatorBuilder: (context, index) => const Divider(height: 0),
itemBuilder: (context, index) { itemBuilder: (context, index) {
SortingInformation info = sorted[index]; SortingInformation info = sorted[index];
Delivery delivery = currentState.tour.deliveries.firstWhere( Delivery delivery = deliveries.firstWhere(
(delivery) => info.deliveryId == delivery.id, (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<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) {
Delivery delivery = deliveries[index];
return DeliveryListItem( return DeliveryListItem(
delivery: delivery, delivery: delivery,
distance: currentState.distances?[delivery.id] ?? 0.0, 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/feature/delivery/overview/presentation/delivery_overview_custom_sort.dart';
import 'package:hl_lieferservice/model/tour.dart'; import 'package:hl_lieferservice/model/tour.dart';
import '../../../../model/delivery.dart';
import '../../../authentication/bloc/auth_bloc.dart'; import '../../../authentication/bloc/auth_bloc.dart';
import '../../../authentication/bloc/auth_state.dart'; import '../../../authentication/bloc/auth_state.dart';
import '../bloc/tour_state.dart';
enum SortType { nameAsc, nameDesc, distance, custom }
class DeliveryOverview extends StatefulWidget { class DeliveryOverview extends StatefulWidget {
const DeliveryOverview({ const DeliveryOverview({
@ -28,7 +28,7 @@ class DeliveryOverview extends StatefulWidget {
class _DeliveryOverviewState extends State<DeliveryOverview> { class _DeliveryOverviewState extends State<DeliveryOverview> {
int? _selectedCarId; int? _selectedCarId;
late List<Delivery> _deliveries; late SortType _sortType;
@override @override
void initState() { void initState() {
@ -36,17 +36,7 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
// Select the first car for initialization // Select the first car for initialization
_selectedCarId = widget.tour.driver.cars.firstOrNull?.id; _selectedCarId = widget.tour.driver.cars.firstOrNull?.id;
_updateDeliveries(); _sortType = SortType.nameAsc;
}
@override
void didUpdateWidget(covariant DeliveryOverview oldWidget) {
super.didUpdateWidget(oldWidget);
_updateDeliveries();
}
void _updateDeliveries() {
_deliveries = [...widget.tour.deliveries];
} }
Future<void> _loadTour() async { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RefreshIndicator( return RefreshIndicator(
@ -128,51 +123,67 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
), ),
], ],
), ),
PopupMenuButton<String>( PopupMenuButton<SortType>(
onSelected: (String value) { onSelected: (SortType value) {
switch (value) {
case SortType.nameAsc:
setState(() { setState(() {
if (value == "name-asc") { _sortType = SortType.nameAsc;
setState(() {
_deliveries.sort();
}); });
}
if (value == "name-desc") { break;
case SortType.nameDesc:
setState(() { 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( showDialog(
context: context, context: context,
fullscreenDialog: true, fullscreenDialog: true,
builder: (context) => CustomSortDialog(), builder: (context) => CustomSortDialog(),
); );
break;
} }
if (value == "distance") {
// TODO: muss noch implementiert werden
}
});
}, },
itemBuilder: itemBuilder:
(BuildContext context) => <PopupMenuEntry<String>>[ (BuildContext context) => <PopupMenuEntry<SortType>>[
PopupMenuItem<String>( PopupMenuItem<SortType>(
value: 'name-asc', value: SortType.nameAsc,
child: Text('Name (A-Z)'), child: Text(
'Name (A-Z)',
style: _sortType == SortType.nameAsc ? _popupItemTextStyle() : null,
), ),
PopupMenuItem<String>(
value: 'name-desc',
child: Text('Name (Z-A)'),
), ),
PopupMenuItem<String>( PopupMenuItem<SortType>(
value: 'distance', value: SortType.nameDesc,
child: Text('Entfernung'), child: Text(
'Name (Z-A)',
style: _sortType == SortType.nameDesc ? _popupItemTextStyle() : null,
),
),
PopupMenuItem<SortType>(
value: SortType.distance,
child: Text(
'Entfernung',
style: _sortType == SortType.distance ? _popupItemTextStyle() : null,
),
),
PopupMenuItem<SortType>(
value: SortType.custom,
child: Text(
'Eigene Sortierung',
style: _sortType == SortType.custom ? _popupItemTextStyle() : null,
), ),
PopupMenuItem<String>(
value: 'custom',
child: Text('Eigene Sortierung'),
), ),
], ],
child: Icon(Icons.filter_list), child: Icon(Icons.filter_list),
@ -186,19 +197,8 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
), ),
Expanded( Expanded(
child: DeliveryList( child: DeliveryList(
distances: widget.distances, selectedCarId: _selectedCarId,
sortingInformation: sortType: _sortType,
(context.read<TourBloc>().state as TourLoaded)
.sortingInformation
.sorting,
deliveries:
_deliveries
.where(
(delivery) =>
delivery.carId == _selectedCarId &&
delivery.allArticlesScanned(),
)
.toList(),
), ),
), ),
], ],