Finalized sorting in delivery overview
This commit is contained in:
@ -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';
|
||||||
|
|||||||
@ -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"))],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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>(
|
return BlocBuilder<TourBloc, TourState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
final currentState = state;
|
final currentState = state;
|
||||||
if (currentState is TourLoaded) {
|
if (currentState is TourLoaded) {
|
||||||
List<SortingInformation> sorted = [...currentState.sortingInformation.sorting];
|
List<Delivery> deliveries =
|
||||||
sorted.sort((a, b) => a.position.compareTo(b.position));
|
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(
|
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];
|
Delivery delivery = deliveries[index];
|
||||||
Delivery delivery = currentState.tour.deliveries.firstWhere(
|
|
||||||
(delivery) => info.deliveryId == delivery.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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) {
|
||||||
setState(() {
|
switch (value) {
|
||||||
if (value == "name-asc") {
|
case SortType.nameAsc:
|
||||||
setState(() {
|
setState(() {
|
||||||
_deliveries.sort();
|
_sortType = SortType.nameAsc;
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
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>(
|
PopupMenuItem<SortType>(
|
||||||
value: 'name-desc',
|
value: SortType.nameDesc,
|
||||||
child: Text('Name (Z-A)'),
|
child: Text(
|
||||||
|
'Name (Z-A)',
|
||||||
|
style: _sortType == SortType.nameDesc ? _popupItemTextStyle() : null,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<SortType>(
|
||||||
value: 'distance',
|
value: SortType.distance,
|
||||||
child: Text('Entfernung'),
|
child: Text(
|
||||||
|
'Entfernung',
|
||||||
|
style: _sortType == SortType.distance ? _popupItemTextStyle() : null,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<SortType>(
|
||||||
value: 'custom',
|
value: SortType.custom,
|
||||||
child: Text('Eigene Sortierung'),
|
child: Text(
|
||||||
|
'Eigene Sortierung',
|
||||||
|
style: _sortType == SortType.custom ? _popupItemTextStyle() : null,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
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(),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user