280 lines
11 KiB
Dart
280 lines
11 KiB
Dart
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:expandable/expandable.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:mqtt_client/mqtt_client.dart';
|
|
import 'package:shared_map/shared_map.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/refresh_badges_message.dart';
|
|
import 'package:stadtbote/tools/util.dart';
|
|
import 'package:badges/badges.dart' as badges;
|
|
|
|
import '../tools/messages/publish_payload_message.dart';
|
|
|
|
late bool _initTimer;
|
|
late Timer _checkForNewJobsTimer;
|
|
late Map<int, DateTime> _firstDelayTimeMap;
|
|
|
|
Future<void> showAcceptJobsPopup(BuildContext context, SharedMapReference mapReference, Function() onFinished) async {
|
|
_initTimer = true;
|
|
_firstDelayTimeMap = {};
|
|
|
|
await showDialog<void>(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return WillPopScope(
|
|
onWillPop: () async {
|
|
_stopTimer();
|
|
|
|
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 (_initTimer) {
|
|
_initTimer = false;
|
|
|
|
_startTimer(context, mapReference, setState, onFinished);
|
|
}
|
|
|
|
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: FutureBuilder<List<Widget>>(
|
|
future: _fetchJobWidgets(setState, onFinished, mapReference),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return Util.getWaitWidget(context);
|
|
} else {
|
|
return ListView(
|
|
children: snapshot.requireData,
|
|
);
|
|
}
|
|
},
|
|
),
|
|
));
|
|
})));
|
|
});
|
|
}
|
|
|
|
void _startTimer(BuildContext context, SharedMapReference mapReference, StateSetter setState, Function() onFinished) {
|
|
_checkForNewJobsTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) async {
|
|
var jobMap = SharedMap<int, Job>.fromSharedReference(mapReference);
|
|
|
|
if (await jobMap.length() > 0) {
|
|
setState(() { });
|
|
} else {
|
|
_stopTimer();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
onFinished();
|
|
}
|
|
});
|
|
}
|
|
|
|
void _stopTimer() {
|
|
_checkForNewJobsTimer.cancel();
|
|
}
|
|
|
|
|
|
Future<List<Widget>> _fetchJobWidgets(StateSetter dialogState, Function() onFinished, SharedMapReference mapReference) async {
|
|
List<Widget> widgets = [];
|
|
|
|
var jobMap = SharedMap<int, Job>.fromSharedReference(mapReference);
|
|
|
|
var entries = await jobMap.entries();
|
|
for (var jobEntry in entries) {
|
|
var job = jobEntry.value;
|
|
|
|
int enableDelay = 3; //job.delayOrderTime;
|
|
if (!_firstDelayTimeMap.containsKey(job.id)) {
|
|
_firstDelayTimeMap[job.id] = DateTime.now();
|
|
} else {
|
|
DateTime firstDisplayTime = _firstDelayTimeMap[job.id]!;
|
|
int diffInSeconds = DateTime.now().difference(firstDisplayTime).inSeconds;
|
|
enableDelay = min(0, diffInSeconds);
|
|
}
|
|
|
|
widgets.add(StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
|
|
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(job.rem),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 20),
|
|
Row(children: [
|
|
Expanded(
|
|
child: getDefaultButton(!job.global ? 'Ablehnen' : 'Ignorieren', () {
|
|
dialogState(() {
|
|
if (jobMap.length() == 1) {
|
|
_stopTimer();
|
|
}
|
|
|
|
_sendAnswer(mapReference, job, !job.global ? 2 : 3);
|
|
|
|
jobMap.remove(job.id);
|
|
|
|
if (jobMap.length() == 0) {
|
|
Navigator.of(context).pop();
|
|
|
|
onFinished();
|
|
}
|
|
});
|
|
}),
|
|
),
|
|
const SizedBox(width: 20),
|
|
Expanded(
|
|
child: getDefaultButton('Annehmen', enabledTimerActive: enableDelay > 0, enabledTimerDelay: enableDelay, () {
|
|
dialogState(() {
|
|
if (jobMap.length() == 1) {
|
|
_stopTimer();
|
|
}
|
|
|
|
_sendAnswer(mapReference, job, 1);
|
|
|
|
jobMap.remove(job.id);
|
|
|
|
if (jobMap.length() == 0) {
|
|
Navigator.of(context).pop();
|
|
|
|
onFinished();
|
|
}
|
|
});
|
|
}),
|
|
)
|
|
])
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}));
|
|
}
|
|
|
|
return widgets;
|
|
}
|
|
|
|
_sendAnswer(SharedMapReference mapReference, Job job, int item) async {
|
|
var currentPosition = MyApp.currentPosition;
|
|
|
|
var message = Reaction();
|
|
message.id = job.id;
|
|
message.ver = job.ver.toString();
|
|
message.item = item;
|
|
message.track = Track();
|
|
message.track.time = DateTime.now().toUtc().toIso8601String();
|
|
message.track.pos = FinTaskPos();
|
|
message.track.pos!.lat = currentPosition != null ? currentPosition.latitude : 0.0;
|
|
message.track.pos!.long = currentPosition != null ? currentPosition.longitude : 0.0;
|
|
message.track.pos!.acc = currentPosition != null ? currentPosition.accuracy.toInt() : 0;
|
|
|
|
await MessageService().publish<PublishPayloadMessage>(Constants.messageTopicPublishPayload, arguments: PublishPayloadMessage(topic: "reaction", message: message.toJson(), qos: MqttQos.exactlyOnce));
|
|
|
|
await MessageService().publish(Constants.messageTopicUpdateBadges, arguments: RefreshBadgesMessage());
|
|
|
|
}
|