From 6981cfbf8adb3629ae1b831d466b11ebf57b4015 Mon Sep 17 00:00:00 2001 From: Sven Carstensen Date: Wed, 7 Aug 2024 10:00:59 +0200 Subject: [PATCH] Erweiterungen --- lib/popups/accept_jobs_popup.dart | 74 +++++++++++++++++--------- lib/popups/tasks_popups/zip_popup.dart | 2 +- lib/tools/mqtt_core.dart | 17 ++---- pubspec.yaml | 2 +- 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/lib/popups/accept_jobs_popup.dart b/lib/popups/accept_jobs_popup.dart index 0183bfe..9fe2be2 100644 --- a/lib/popups/accept_jobs_popup.dart +++ b/lib/popups/accept_jobs_popup.dart @@ -18,11 +18,14 @@ import 'package:badges/badges.dart' as badges; import '../tools/messages/publish_payload_message.dart'; -Timer? _checkForNewJobsTimer; - -Map _firstDelayTimeMap = {}; +late bool _initTimer; +late Timer _checkForNewJobsTimer; +late Map _firstDelayTimeMap; Future showAcceptJobsPopup(BuildContext context, SharedMapReference mapReference, Function() onFinished) async { + _initTimer = true; + _firstDelayTimeMap = {}; + await showDialog( context: context, builder: (BuildContext context) { @@ -41,7 +44,11 @@ Future showAcceptJobsPopup(BuildContext context, SharedMapReference mapRef shadowColor: Colors.transparent, backgroundColor: Colors.white, content: StatefulBuilder(builder: (BuildContext context, StateSetter setState) { - _startTimer(mapReference, setState); + if (_initTimer) { + _initTimer = false; + + _startTimer(context, mapReference, setState, onFinished); + } return SizedBox( key: GlobalKey(), @@ -51,7 +58,7 @@ Future showAcceptJobsPopup(BuildContext context, SharedMapReference mapRef height: 100, padding: const EdgeInsets.all(5), child: FutureBuilder>( - future: _fetchJobWidgets(mapReference), + future: _fetchJobWidgets(setState, onFinished, mapReference), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Util.getWaitWidget(context); @@ -67,26 +74,28 @@ Future showAcceptJobsPopup(BuildContext context, SharedMapReference mapRef }); } -void _startTimer(SharedMapReference mapReference, StateSetter setState) { - _checkForNewJobsTimer ??= Timer.periodic(const Duration(seconds: 5), (Timer t) async { +void _startTimer(BuildContext context, SharedMapReference mapReference, StateSetter setState, Function() onFinished) { + _checkForNewJobsTimer = Timer.periodic(const Duration(seconds: 5), (Timer t) async { var jobMap = SharedMap.fromSharedReference(mapReference); if (await jobMap.length() > 0) { setState(() { }); + } else { + _stopTimer(); + + Navigator.of(context).pop(); + + onFinished(); } }); } void _stopTimer() { - if (_checkForNewJobsTimer != null) { - _checkForNewJobsTimer!.cancel(); - - _checkForNewJobsTimer = null; - } + _checkForNewJobsTimer.cancel(); } -Future> _fetchJobWidgets(SharedMapReference mapReference) async { +Future> _fetchJobWidgets(StateSetter dialogState, Function() onFinished, SharedMapReference mapReference) async { List widgets = []; var jobMap = SharedMap.fromSharedReference(mapReference); @@ -198,20 +207,40 @@ Future> _fetchJobWidgets(SharedMapReference mapReference) async { Row(children: [ Expanded( child: getDefaultButton(!job.global ? 'Ablehnen' : 'Ignorieren', () { - setState(() { - _sendAnswer(job, !job.global ? 2 : 3); + dialogState(() { + if (jobMap.length() == 1) { + _stopTimer(); + } - //jobs.removeWhere((value) => value.id == job.id); + _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, () { - setState(() { - _sendAnswer(job, 1); + dialogState(() { + if (jobMap.length() == 1) { + _stopTimer(); + } - //jobs.removeWhere((value) => value.id == job.id); + _sendAnswer(mapReference, job, 1); + + jobMap.remove(job.id); + + if (jobMap.length() == 0) { + Navigator.of(context).pop(); + + onFinished(); + } }); }), ) @@ -229,7 +258,7 @@ Future> _fetchJobWidgets(SharedMapReference mapReference) async { return widgets; } -_sendAnswer(Job job, int item) async { +_sendAnswer(SharedMapReference mapReference, Job job, int item) async { var currentPosition = MyApp.currentPosition; var message = Reaction(); @@ -245,9 +274,6 @@ _sendAnswer(Job job, int item) async { await MessageService().publish(Constants.messageTopicPublishPayload, arguments: PublishPayloadMessage(topic: "reaction", message: message.toJson(), qos: MqttQos.exactlyOnce)); - await MyApp.isarInstance.writeTxn(() async { - await MyApp.isarInstance.jobs.delete(job.id); - }); - await MessageService().publish(Constants.messageTopicUpdateBadges, arguments: RefreshBadgesMessage()); + } diff --git a/lib/popups/tasks_popups/zip_popup.dart b/lib/popups/tasks_popups/zip_popup.dart index aea1be3..8f57439 100644 --- a/lib/popups/tasks_popups/zip_popup.dart +++ b/lib/popups/tasks_popups/zip_popup.dart @@ -18,7 +18,7 @@ showZipPopup(BuildContext context, String text, String buttonText, Function(Stri return SizedBox( key: GlobalKey(), - height: 280, + height: 293, width: MediaQuery.of(context).size.width * 0.8, child: Container( height: 100, diff --git a/lib/tools/mqtt_core.dart b/lib/tools/mqtt_core.dart index 0839980..23ac324 100644 --- a/lib/tools/mqtt_core.dart +++ b/lib/tools/mqtt_core.dart @@ -140,11 +140,7 @@ class MqttCore { bool _publishText(String topic, String text, MqttQos qos, {bool enqueueOnError = true}) { try { var buffer = Uint8Buffer(); - if (kDebugMode) { - buffer.addAll(const Utf8Encoder().convert(text)); - } else { - buffer.addAll(GZipEncoder().encode(const Utf8Encoder().convert(text)) as Iterable); - } + buffer.addAll(GZipEncoder().encode(const Utf8Encoder().convert(text)) as Iterable); var messageTopic = "$_toServerTopic$_clientId/$topic"; @@ -178,12 +174,7 @@ class MqttCore { Util.logInfo('message received\ntopic: $topic\n'); Util.logInfo("size in bytes: ${message.payload.message.length.toString()}"); - String payload; - if (kDebugMode) { - payload = utf8.decode(message.payload.message); - } else { - payload = utf8.decode(GZipDecoder().decodeBytes(message.payload.message)); - } + var payload = utf8.decode(GZipDecoder().decodeBytes(message.payload.message)); Util.logInfo('$topic\nmessage: $payload'); @@ -453,8 +444,8 @@ class MqttCore { var bootstrapResponsePayload = BootstrapResponse.fromJson(jsonDecode(responseText)); var brokerUriAndPortArray = bootstrapResponsePayload.broker.replaceFirst("mqtt://", "").split(":"); - _brokerUri = "192.168.180.27"; //brokerUriAndPortArray[0]; - _brokerPort = 1883; //int.parse(brokerUriAndPortArray[1]); + _brokerUri = brokerUriAndPortArray[0]; + _brokerPort = int.parse(brokerUriAndPortArray[1]); _clientId = bootstrapResponsePayload.id; _fromServerTopic = bootstrapResponsePayload.fromServer; _toServerTopic = bootstrapResponsePayload.toServer; diff --git a/pubspec.yaml b/pubspec.yaml index 84acefa..d11ed67 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 319.0.0+319 +version: 320.0.0+320 environment: sdk: '>=2.19.6 <3.0.0'