Files
stadtbote-app/lib/popups/accept_jobs_popup.dart
T
2024-03-18 20:26:16 +01:00

234 lines
12 KiB
Dart

import 'dart:collection';
import 'package:expandable/expandable.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:stadtbote/main.dart';
import 'package:stadtbote/protocoll_messages/accept_job_message.dart';
import 'package:stadtbote/model/job.dart';
import 'package:stadtbote/protocoll_messages/fin_task_message.dart';
import 'package:stadtbote/tools/constants.dart';
import 'package:stadtbote/tools/message_service.dart';
import 'package:stadtbote/tools/messages/job_deleted_message.dart';
import 'package:stadtbote/tools/messages/job_received_message.dart';
import 'package:stadtbote/tools/mqtt_core.dart';
import 'package:stadtbote/tools/util.dart';
import 'package:badges/badges.dart' as badges;
Future<void> showAcceptJobsPopup(BuildContext context, List<Job> jobs, Function onFinished) async {
var jobsMap = HashMap<int, Job>();
for (var job in jobs) { jobsMap[job.id] = job; }
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async {
MessageService().unsubscribe(Constants.messageTopicJobDeleted, jobsMap);
MessageService().unsubscribe(Constants.messageTopicJobReceived, jobsMap);
Navigator.of(context).pop();
onFinished();
return true;
},
child: AlertDialog(
contentPadding: EdgeInsets.zero,
shadowColor: Colors.transparent,
backgroundColor: Colors.white,
content: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
if (jobsMap.isEmpty) {
Navigator.of(context).pop();
onFinished();
}
MessageService().unsubscribe(Constants.messageTopicJobDeleted, jobsMap);
MessageService().unsubscribe(Constants.messageTopicJobReceived, jobsMap);
MessageService().subscribe<JobDeletedMessage>(Constants.messageTopicJobDeleted, jobsMap, (jobDeletedMessage) async {
jobsMap.removeWhere((key, value) => key == jobDeletedMessage.jobId);
setState(() {});
});
MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, jobsMap, (jobdMessage) async {
if (jobsMap.containsKey(jobdMessage.job.id)) {
jobsMap.removeWhere((key, value) => key == jobdMessage.job.id);
}
jobsMap[jobdMessage.job.id] = jobdMessage.job;
setState(() {});
});
return SizedBox(
key: GlobalKey(),
height: MediaQuery.of(context).size.height * 1,
width: MediaQuery.of(context).size.width * 1.2,
child: Container(
height: 100,
padding: const EdgeInsets.all(5),
child: ListView.builder(
itemCount: jobsMap.length,
itemBuilder: (_, i) {
var job = jobsMap.values.toList()[i];
String remarkString = '';
for (var remark in job.rem) {
remarkString += '$remark\n';
}
remarkString = remarkString.trim();
return ExpandableNotifier(
child: ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
title: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(
color: Constants.colorPrimary,
width: 1,
),
),
child: badges.Badge(
position: badges.BadgePosition.topEnd(top: -13, end: -5),
showBadge: false,
badgeContent: const Text(
'Neu',
style: TextStyle(
color: Colors.white,
fontSize: 18.0,
),
),
badgeStyle: badges.BadgeStyle(
shape: badges.BadgeShape.square,
badgeColor: Constants.colorPrimary,
padding: const EdgeInsets.all(5),
borderRadius: BorderRadius.circular(5),
borderSide: const BorderSide(color: Constants.colorPrimary, width: 0),
),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
children: [
SizedBox(
width: double.infinity,
child: Text(
DateFormat('dd.MM.yyyy HH:mm').format(DateTime.parse(job.ordertime).toLocal()).toString(),
textAlign: TextAlign.left,
style: TextStyle(backgroundColor: isJobInFuture(job) ? Colors.yellow : Colors.transparent),
),
),
SizedBox(width: double.infinity, child: Text('Auftragsnummer: ${job.id}', textAlign: TextAlign.left)),
SizedBox(width: double.infinity, child: Text(job.tours[0].comp, textAlign: TextAlign.left)),
SizedBox(width: double.infinity, child: Text(job.tours[0].street, textAlign: TextAlign.left)),
SizedBox(width: double.infinity, child: Text(job.tours[0].city, textAlign: TextAlign.left)),
Row(
children: [
Image.asset("assets/${mapVehicleTypeToImageName(job.vhtId)}", width: 60, height: 60),
const SizedBox(width: 10),
Text(mapVehicleTypeToText(job.vhtId)),
],
),
const SizedBox(height: 10),
ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: ExpandablePanel(
theme: const ExpandableThemeData(
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: true,
),
header: const Padding(
padding: EdgeInsets.all(0),
child: Text(
"Weitere Infos....",
style: TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.bold),
)),
collapsed: const SizedBox(),
expanded: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
height: 200,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: const Color(0xfffff4e4),
border: Border.all(
color: Colors.grey,
width: 0.5,
),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(remarkString),
),
),
],
),
),
),
const SizedBox(height: 20),
Row(children: [
Expanded(
child: getDefaultButton(!job.global ? 'Ablehnen' : 'Ignorieren',
() {
_sendAnswer(job, !job.global ? 2 : 3);
jobsMap.removeWhere((key, value) => key == job.id);
setState(() {});
}),
),
const SizedBox(width: 20),
Expanded(
child: getDefaultButton('Annehmen', () {
_sendAnswer(job, 1);
jobsMap.removeWhere((key, value) => key == job.id);
setState(() {});
}),
)
])
],
),
),
),
),
),
);
},
)),
);
})));
});
}
_sendAnswer(Job job, int item) async {
var currentPosition = MyApp.currentPosition;
var message = AcceptJobMessage();
var position = MyApp.currentPosition;
message.reaction = Reaction();
message.reaction.id = job.id.toString();
message.reaction.ver = job.ver.toString();
message.reaction.item = item;
message.reaction.track = Track();
message.reaction.track.time = DateTime.now().toUtc().toIso8601String();
if (position != null) {
message.reaction.track.pos = FinTaskPos();
message.reaction.track.pos!.lat = currentPosition!.latitude;
message.reaction.track.pos!.long = currentPosition.longitude;
message.reaction.track.pos!.acc = currentPosition.accuracy.toInt();
}
MqttCore.instance.publishPayloadMessage("job", message.toJson());
job.state = 9;
await MyApp.isarInstance.writeTxn(() async => { await MyApp.isarInstance.jobs.put(job)});
}