Implemented new custom sort and separated the sorted view for each car
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
class SortingInformation {
|
||||
String deliveryId;
|
||||
@ -18,36 +19,38 @@ class SortingInformation {
|
||||
}
|
||||
|
||||
class SortingInformationContainer {
|
||||
List<SortingInformation> sorting;
|
||||
Map<String, List<String>> cars;
|
||||
|
||||
SortingInformationContainer({required this.sorting});
|
||||
|
||||
void sort() {
|
||||
sorting.sort((a, b) => a.position.compareTo(b.position));
|
||||
}
|
||||
SortingInformationContainer({required this.cars});
|
||||
|
||||
static SortingInformationContainer fromJson(Map<String, dynamic> json) {
|
||||
SortingInformationContainer container = SortingInformationContainer(
|
||||
sorting: [],
|
||||
cars: {},
|
||||
);
|
||||
|
||||
for (final info in json["sorting"]) {
|
||||
container.sorting.add(SortingInformation.fromJson(info));
|
||||
for (final car in json["cars"].entries) {
|
||||
List<String> values = [];
|
||||
for (String value in car.value) {
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
container.cars[car.key] = values;
|
||||
}
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"sorting":
|
||||
sorting.map((info) => SortingInformation.toJson(info)).toList(),
|
||||
};
|
||||
Map<String, dynamic> cars = {};
|
||||
|
||||
for (final car in this.cars.entries) {
|
||||
cars[car.key] = car.value;
|
||||
}
|
||||
|
||||
return {"cars": cars};
|
||||
}
|
||||
|
||||
SortingInformationContainer copyWith({List<SortingInformation>? sorting}) {
|
||||
return SortingInformationContainer(
|
||||
sorting: sorting ?? this.sorting,
|
||||
);
|
||||
SortingInformationContainer copyWith({Map<String, List<String>>? sorting}) {
|
||||
return SortingInformationContainer(cars: sorting ?? cars);
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,18 +26,19 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
|
||||
Widget _showCustomSortedList(
|
||||
List<Delivery> deliveries,
|
||||
List<SortingInformation> sortingInformation,
|
||||
List<String> sortingInformation,
|
||||
Map<String, double> distances,
|
||||
) {
|
||||
List<SortingInformation> sorted = [...sortingInformation];
|
||||
sorted.sort((a, b) => a.position.compareTo(b.position));
|
||||
|
||||
return ListView.separated(
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
separatorBuilder: (context, index) => const Divider(height: 0),
|
||||
itemBuilder: (context, index) {
|
||||
SortingInformation info = sorted[index];
|
||||
String id = sortingInformation[index];
|
||||
Delivery delivery = deliveries.firstWhere(
|
||||
(delivery) => info.deliveryId == delivery.id,
|
||||
(delivery) =>
|
||||
id == delivery.id &&
|
||||
delivery.carId == widget.selectedCarId,
|
||||
);
|
||||
|
||||
return DeliveryListItem(
|
||||
@ -45,7 +46,7 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
distance: distances[delivery.id] ?? 0.0,
|
||||
);
|
||||
},
|
||||
itemCount: sorted.length,
|
||||
itemCount: sortingInformation.length,
|
||||
);
|
||||
}
|
||||
|
||||
@ -60,12 +61,24 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
.where(
|
||||
(delivery) =>
|
||||
delivery.carId == widget.selectedCarId &&
|
||||
delivery.allArticlesScanned() || delivery.state == DeliveryState.finished,
|
||||
delivery.allArticlesScanned() &&
|
||||
delivery.state != DeliveryState.finished,
|
||||
)
|
||||
.toList();
|
||||
|
||||
List<Delivery> finishedDeliveries =
|
||||
currentState.tour.deliveries
|
||||
.where(
|
||||
(delivery) =>
|
||||
delivery.state == DeliveryState.finished &&
|
||||
delivery.carId == widget.selectedCarId,
|
||||
)
|
||||
.toList();
|
||||
|
||||
if (deliveries.isEmpty) {
|
||||
return ListView(
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
Center(child: const Text("Keine Auslieferungen gefunden")),
|
||||
],
|
||||
@ -75,8 +88,8 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
switch (widget.sortType) {
|
||||
case SortType.custom:
|
||||
return _showCustomSortedList(
|
||||
deliveries,
|
||||
currentState.sortingInformation.sorting,
|
||||
currentState.tour.deliveries,
|
||||
currentState.sortingInformation[widget.selectedCarId.toString()] ?? [],
|
||||
currentState.distances ?? {},
|
||||
);
|
||||
|
||||
@ -101,8 +114,12 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
break;
|
||||
}
|
||||
|
||||
//deliveries.addAll(finishedDeliveries);
|
||||
|
||||
return ListView.separated(
|
||||
separatorBuilder: (context, index) => const Divider(height: 0),
|
||||
shrinkWrap: true,
|
||||
physics: NeverScrollableScrollPhysics(),
|
||||
itemBuilder: (context, index) {
|
||||
Delivery delivery = deliveries[index];
|
||||
|
||||
|
||||
@ -101,8 +101,8 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _loadTour,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
child: ListView(
|
||||
//crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
DeliveryInfo(tour: widget.tour),
|
||||
Padding(
|
||||
@ -150,7 +150,7 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
showDialog(
|
||||
context: context,
|
||||
fullscreenDialog: true,
|
||||
builder: (context) => CustomSortDialog(),
|
||||
builder: (context) => CustomSortDialog(selectedCarId: _selectedCarId,),
|
||||
);
|
||||
break;
|
||||
}
|
||||
@ -195,11 +195,9 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 20),
|
||||
child: _carSelection(),
|
||||
),
|
||||
Expanded(
|
||||
child: DeliveryList(
|
||||
selectedCarId: _selectedCarId,
|
||||
sortType: _sortType,
|
||||
),
|
||||
DeliveryList(
|
||||
selectedCarId: _selectedCarId,
|
||||
sortType: _sortType,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@ -3,33 +3,34 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/bloc/tour_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/bloc/tour_event.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/bloc/tour_state.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/util.dart';
|
||||
import 'package:hl_lieferservice/model/delivery.dart';
|
||||
|
||||
import '../model/sorting_information.dart';
|
||||
|
||||
class CustomSortDialog extends StatefulWidget {
|
||||
const CustomSortDialog({super.key});
|
||||
const CustomSortDialog({super.key, this.selectedCarId});
|
||||
|
||||
final int? selectedCarId;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _CustomSortDialogState();
|
||||
}
|
||||
|
||||
class _CustomSortDialogState extends State<CustomSortDialog> {
|
||||
late List<SortingInformation> _localSortedList;
|
||||
late List<String> _localSortedList;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final state = context.read<TourBloc>().state;
|
||||
if (state is TourLoaded) {
|
||||
_localSortedList = [...state.sortingInformation.sorting];
|
||||
_localSortedList.sort((a, b) => a.position.compareTo(b.position));
|
||||
_localSortedList = [
|
||||
...state.sortingInformation[widget.selectedCarId.toString()] ?? [],
|
||||
];
|
||||
} else {
|
||||
_localSortedList = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Widget _information() {
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(top: 15),
|
||||
@ -68,49 +69,49 @@ class _CustomSortDialogState extends State<CustomSortDialog> {
|
||||
child: ReorderableListView(
|
||||
onReorder: (oldIndex, newIndex) {
|
||||
setState(() {
|
||||
if (oldIndex < newIndex) {
|
||||
newIndex -= 1;
|
||||
}
|
||||
final SortingInformation item = _localSortedList.removeAt(oldIndex);
|
||||
_localSortedList.insert(newIndex, item);
|
||||
_localSortedList = reorderList(
|
||||
_localSortedList,
|
||||
oldIndex,
|
||||
newIndex,
|
||||
);
|
||||
});
|
||||
|
||||
context.read<TourBloc>().add(
|
||||
ReorderDeliveryEvent(
|
||||
newPosition: newIndex,
|
||||
oldPosition: oldIndex,
|
||||
carId: widget.selectedCarId.toString(),
|
||||
),
|
||||
);
|
||||
},
|
||||
children:
|
||||
_localSortedList.map((info) {
|
||||
Delivery delivery = currentState.tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == info.deliveryId,
|
||||
);
|
||||
SortingInformation information = currentState
|
||||
.sortingInformation
|
||||
.sorting
|
||||
.firstWhere((info) => info.deliveryId == delivery.id);
|
||||
_localSortedList
|
||||
.map((id) {
|
||||
Delivery delivery = currentState.tour.deliveries
|
||||
.firstWhere((delivery) => delivery.id == id);
|
||||
int pos = _localSortedList.indexOf(id) + 1;
|
||||
|
||||
return ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
child: Text(
|
||||
"${information.position + 1}",
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.onSecondary,
|
||||
return ListTile(
|
||||
leading: CircleAvatar(
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
child: Text(
|
||||
"$pos",
|
||||
style: TextStyle(
|
||||
color:
|
||||
Theme.of(context).colorScheme.onSecondary,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
title: Text(delivery.customer.name),
|
||||
subtitle: Text(
|
||||
delivery.customer.address.toString(),
|
||||
style: TextStyle(fontSize: 11),
|
||||
),
|
||||
trailing: Icon(Icons.drag_handle),
|
||||
key: Key("reorder-item-${delivery.id}"),
|
||||
);
|
||||
}).toList(),
|
||||
title: Text(delivery.customer.name),
|
||||
subtitle: Text(
|
||||
delivery.customer.address.toString(),
|
||||
style: TextStyle(fontSize: 11),
|
||||
),
|
||||
trailing: Icon(Icons.drag_handle),
|
||||
key: Key("reorder-item-${delivery.id}"),
|
||||
);
|
||||
})
|
||||
.toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -28,6 +28,8 @@ class _DeliveryOverviewPageState extends State<DeliveryOverviewPage> {
|
||||
);
|
||||
}
|
||||
|
||||
debugPrint(state.toString());
|
||||
|
||||
return Container();
|
||||
},
|
||||
);
|
||||
|
||||
@ -1,17 +1,15 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../model/sorting_information.dart';
|
||||
|
||||
class ReorderService {
|
||||
get _path async {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final date = DateTime.now();
|
||||
final filename =
|
||||
"custom_sort_${date.year}_${date.month}_${date.day}.json";
|
||||
final filename = "custom_sort_${date.year}_${date.month}_${date.day}.json";
|
||||
final path = "${dir.path}/$filename";
|
||||
|
||||
return path;
|
||||
@ -24,30 +22,53 @@ class ReorderService {
|
||||
return file;
|
||||
}
|
||||
|
||||
Future<void> saveSortingInformation(SortingInformationContainer container) async {
|
||||
(await _file).writeAsString(jsonEncode(container.toJson()));
|
||||
Future<void> saveSortingInformation(
|
||||
Map<String, List<String>> container,
|
||||
) async {
|
||||
debugPrint("CONTAINER: ${jsonEncode(container)}");
|
||||
|
||||
(await _file).writeAsString(jsonEncode(container));
|
||||
}
|
||||
|
||||
Future<void> initializeTour(Tour tour) async {
|
||||
(await _file).create();
|
||||
SortingInformationContainer container = SortingInformationContainer(sorting: []);
|
||||
|
||||
for (final (index, delivery) in tour.deliveries.indexed) {
|
||||
container.sorting.add(
|
||||
SortingInformation(deliveryId: delivery.id, position: index),
|
||||
);
|
||||
Map<String, List<String>> sorting = {};
|
||||
|
||||
for (final delivery in tour.deliveries) {
|
||||
if (!sorting.containsKey(delivery.carId.toString())) {
|
||||
sorting[delivery.carId.toString()] = [delivery.id];
|
||||
} else {
|
||||
sorting[delivery.carId.toString()]!.add(delivery.id);
|
||||
}
|
||||
}
|
||||
|
||||
(await _file).writeAsString(jsonEncode(container.toJson()));
|
||||
(await _file).writeAsString(jsonEncode({"cars": sorting}));
|
||||
}
|
||||
|
||||
bool orderInformationExist() {
|
||||
return false;
|
||||
}
|
||||
|
||||
Future<SortingInformationContainer> loadSortingInformation() async {
|
||||
return SortingInformationContainer.fromJson(
|
||||
jsonDecode(await (await _file).readAsString()),
|
||||
);
|
||||
Future<Map<String, List<String>>> loadSortingInformation() async {
|
||||
debugPrint("FILE: ${await (await _file).readAsString()}");
|
||||
Map<String, List<String>> container = {};
|
||||
Map<String, dynamic> json = jsonDecode(await (await _file).readAsString());
|
||||
|
||||
if (!json.containsKey("cars")) {
|
||||
throw Exception("No cars found in file");
|
||||
}
|
||||
|
||||
for (final car in json["cars"].entries) {
|
||||
List<String> values = [];
|
||||
|
||||
for (String value in car.value) {
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
container[car.key] = values;
|
||||
}
|
||||
|
||||
|
||||
return container;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user