import 'package:dart_mq/dart_mq.dart'; import 'package:flutter/material.dart'; import 'package:isar/isar.dart'; import 'package:stadtbote/content_views/accepted_jobs_page.dart'; import 'package:stadtbote/content_views/approach_area_page.dart'; import 'package:stadtbote/content_views/assigned_jobs_page.dart'; import 'package:stadtbote/content_views/finished_jobs_page.dart'; import 'package:stadtbote/content_views/messages_page.dart'; import 'package:stadtbote/content_views/set_desired_zip_page.dart'; import 'package:stadtbote/content_views/utils/content_view_widget.dart'; import 'package:stadtbote/main.dart'; import 'package:stadtbote/model/job.dart'; import 'package:stadtbote/model/location_status.dart'; import 'package:stadtbote/model/chat_message.dart'; import 'package:stadtbote/model/session_status.dart'; import 'package:stadtbote/network_messages/logout_message.dart'; import 'package:stadtbote/popups/infos_popup.dart'; import 'package:stadtbote/tools/constants.dart'; import 'package:stadtbote/tools/messages/job_received_message.dart'; import 'package:stadtbote/tools/messages/publish_payload_message.dart'; import 'package:stadtbote/tools/messages/connect_client_message.dart'; import 'package:stadtbote/tools/messages/disconnect_client_message.dart'; import 'package:badges/badges.dart' as badges; import 'package:stadtbote/tools/util.dart'; class MainPage extends ContentViewWidget { const MainPage({super.key}); @override State createState() => _MainPageState(); @override Future getHeaderWidget(BuildContext context) async { await Future.delayed(const Duration(microseconds: 0)); return _HeaderWidget(); } } class _MainPageState extends State with ConsumerMixin, ProducerMixin { final ValueNotifier _assignedJobsValueNotifier = ValueNotifier(0); final ValueNotifier _acceptedJobsValueNotifier = ValueNotifier(0); final ValueNotifier _messagesValueNotifier = ValueNotifier(0); late bool _loggedOut = false; @override void initState() { super.initState(); _refreshBadges(); _initMessages(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { return Stack( children: [ Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Visibility( visible: !_loggedOut, child: ValueListenableBuilder( valueListenable: _assignedJobsValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: -12, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Zugewiesene Aufträge', image: Image.asset('assets/zugewiesene_auftraege.png', width: 30, height: 30), () async => { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const AssignedJobsPage(), ) //await MessageService().publish(Constants.messageTopicPushContentView, arguments: const AssignedJobsPage()) } ), ); }, ), ), SizedBox(height: _loggedOut == true ? 0 : 20), Visibility( visible: !_loggedOut, child: ValueListenableBuilder( valueListenable: _acceptedJobsValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: -12, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Angenommene Aufträge', image: Image.asset('assets/angenommene_auftraege.png', width: 30, height: 30), () async => { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const AcceptedJobsPage(), ) //await MessageService().publish(Constants.messageTopicPushContentView, arguments: const AcceptedJobsPage()) } ) ); }, ), ), SizedBox(height: _loggedOut == true ? 0 : 20), Visibility( visible: !_loggedOut, child: getDefaultButton( 'Erledigte Aufträge', image: Image.asset('assets/erledigte_auftraege.png', width: 30, height: 30), () async => { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const FinishedJobsPage(), ) //await MessageService().publish(Constants.messageTopicPushContentView, arguments: const FinishedJobsPage()) } ), ), SizedBox(height: _loggedOut == true ? 0 : 20), Visibility( visible: !_loggedOut, child: ValueListenableBuilder( valueListenable: _messagesValueNotifier, builder: (context, value, child) { return badges.Badge( showBadge: value > 0, position: badges.BadgePosition.topEnd(top: -12, end: -5), badgeContent: Text( value.toString(), style: const TextStyle( color: Colors.white, fontSize: 13.0, ), ), badgeStyle: badges.BadgeStyle( badgeColor: Colors.red, padding: const EdgeInsets.all(10), borderRadius: BorderRadius.circular(4), borderSide: const BorderSide(color: Colors.white, width: 0), elevation: 0, ), child: getDefaultButton( 'Nachrichten', image: Image.asset('assets/nachrichten.png', width: 30, height: 30), () async => { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const MessagesPage(), ) //await MessageService().publish(Constants.messageTopicPushContentView, arguments: const MessagesPage()) } ) ); }, ), ), SizedBox(height: _loggedOut == true ? 0 : 20), Visibility( visible: !_loggedOut, child: getDefaultButton( 'Anfahrtsbereich', image: Image.asset('assets/anfahrtsbereich.png', width: 30, height: 30), () async => { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const ApproachAreaPage(), ) //await MessageService().publish(Constants.messageTopicPushContentView, arguments: const ApproachAreaPage()) }), ), SizedBox(height: _loggedOut == true ? 0 : 20), Visibility( visible: !_loggedOut, child: getDefaultButton( 'Wunschziel für heute', image: Image.asset('assets/wunsch_plz.png', width: 30, height: 30), () async => { sendMessage( routingKey: Constants.messageTopicPushContentView, payload: const SetDesiredZipPage(), ) }), ), SizedBox(height: _loggedOut == true ? 0 : 20), getDefaultButton(_loggedOut ? 'Anmelden' : 'Abmelden', image: Image.asset(_loggedOut ? 'assets/anmelden.png' : 'assets/abmelden.png', width: 30, height: 30), () async { _logout(context); }), Expanded( child: Container( decoration: BoxDecoration( color: Colors.transparent, border: Border.all( color: Colors.transparent, width: 2.0, ), ), ), ), Image.asset( 'assets/sb_logo.png', width: 100, height: 100, ), ], ), ), ], ); } _initMessages() { subscribe( queueId: Constants.messageTopicRemoveJob, callback: (message) { _refreshBadges(); }, ); subscribe( queueId: Constants.messageTopicJobReceived, callback: (message) { _jobReceived((message.payload as JobReceivedMessage).job); }, ); subscribe( queueId: Constants.messageTopicMessageReceived, callback: (message) { _refreshBadges(); }, ); subscribe( queueId: Constants.messageTopicJobDeleted, callback: (message) { _refreshBadges(); }, ); subscribe( queueId: Constants.messageTopicUpdateBadges, callback: (message) { _refreshBadges(); }, ); } void _refreshBadges() async { _assignedJobsValueNotifier.value = await MyApp.isarInstance.jobs.filter().stateEqualTo(0).count(); _acceptedJobsValueNotifier.value = await MyApp.isarInstance.jobs.filter().stateEqualTo(1).count(); _messagesValueNotifier.value = await MyApp.isarInstance.chatMessages.filter().readEqualTo(false).count(); } void _jobReceived(Job job) { _refreshBadges(); } _logout(context) async { if (!_loggedOut) { showLogoutAlertDialog(context); } else { sendMessage( routingKey: Constants.messageTopicConnectClient, payload: ConnectClientMessage(), ); _loggedOut = false; setState(() {}); } } showLogoutAlertDialog(BuildContext context) async { Widget yesButton = TextButton( child: const Text("Ja"), onPressed: () async { Navigator.of(context).pop(); showMessageDialog(context, "Information", "Sie werden abgemeldet.\nBitte warten..."); _sendLogoutMessage(); _loggedOut = true; Future.delayed(const Duration(seconds: 2), () async { sendMessage( routingKey: Constants.messageTopicDisconnectClient, payload: DisconnectClientMessage(), ); Future.delayed(const Duration(seconds: 3), () async { // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicDisconnectClient, payload: DisconnectClientMessage(), ); //await MessageService().publish(Constants.messageTopicDisconnectClient, arguments: DisconnectClientMessage()); Navigator.of(MyApp.appNavigatorKey.currentContext!).pop(); setState(() {}); }); }); }, ); Widget noButton = TextButton( child: const Text("Nein"), onPressed: () { Navigator.of(context).pop(); }, ); // set up the AlertDialog AlertDialog alert = AlertDialog( title: const Text("Warnung"), content: const Text("Wollen Sie sich wirklich abmelden?"), actions: [ yesButton, noButton, ], ); // show the dialog showDialog( context: context, builder: (BuildContext context) { return alert; }, ); } _sendLogoutMessage() async { var position = MyApp.currentPosition; var logoutMessage = LogoutMessage(); if (position != null) { logoutMessage.pos = LogoutMessagePosition(); logoutMessage.pos!.time = position.timestamp.toIso8601String(); logoutMessage.pos!.lat = position.latitude; logoutMessage.pos!.long = position.longitude; logoutMessage.pos!.acc = position.accuracy.toInt(); } // TODO MESSAGING sendMessage( routingKey: Constants.messageTopicPublishPayload, payload: PublishPayloadMessage(topic: "logout", message: logoutMessage.toJson(), acknowledgeRequired: false), ); //await MessageService().publish(Constants.messageTopicPublishPayload, arguments: PublishPayloadMessage(topic: "logout", message: logoutMessage.toJson(), qos: MqttQos.exactlyOnce)); } } class _HeaderWidget extends StatefulWidget { @override State<_HeaderWidget> createState() => _HeaderWidgetState(); } class _HeaderWidgetState extends State<_HeaderWidget> with ConsumerMixin { final ValueNotifier _zipValueNotifier = ValueNotifier("-----"); final ValueNotifier _zipRealValueNotifier = ValueNotifier("-----"); final ValueNotifier _sidValueNotifier = ValueNotifier("-----"); @override void initState() { super.initState(); _updateHeader(); _initMessages(); } @override Widget build(BuildContext context) { var cellWidth = (MediaQuery.of(context).size.width - 60 - 60 - 25) / 2; return Padding( padding: const EdgeInsets.all(8), child: Stack( alignment: Alignment.center, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Visibility( visible: false, child: Container( width: 60, height: 60, decoration: const BoxDecoration( shape: BoxShape.circle, color: Colors.white, ), child: FittedBox( child: GestureDetector( onTap: () async { }, child: const CircleAvatar( radius: 10, backgroundColor: Constants.colorPrimary, child: Padding(padding: EdgeInsets.all(1.0), child: Icon(Icons.info_outline, size: 20.0, color: Colors.white)), ), ), ), ), ), Visibility( visible: true, child: Container( width: 60, height: 60, decoration: const BoxDecoration( shape: BoxShape.circle, color: Colors.red, ), child: FittedBox( child: GestureDetector( onTap: () async { var sessionStatus = await MyApp.isarInstance.sessionStatus.where().findFirst(); showInfosPopup(sessionStatus); }, child: const CircleAvatar( backgroundColor: Constants.colorPrimary, radius: 10, child: Padding(padding: EdgeInsets.all(1.0), child: Icon(Icons.info_outline, size: 20.0, color: Colors.white))), ), ), ), ), ], ), SizedBox( height: 100, width: cellWidth * 2, child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, // Verteilt die Texte gleichmäßig in der Zeile children: [ SizedBox( width: cellWidth, child: const Text( 'Meldung', textAlign: TextAlign.center, style: TextStyle(fontSize: 11, color: Colors.white), ), ), SizedBox( width: cellWidth, child: const Text( 'Ortung', textAlign: TextAlign.center, style: TextStyle(fontSize: 11, color: Colors.white), ), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, // Verteilt die Texte gleichmäßig in der Zeile children: [ SizedBox( width: cellWidth, child: ValueListenableBuilder( valueListenable: _zipValueNotifier, builder: (context, value, child) { return Text( textAlign: TextAlign.center, value, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.white), ); }, ) ), SizedBox( width: cellWidth, child: ValueListenableBuilder( valueListenable: _zipRealValueNotifier, builder: (context, value, child) { return Text( textAlign: TextAlign.center, value, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.white), ); }, ), ), ], ), Column( children: [ const Text( 'Kurierkennung', textAlign: TextAlign.center, style: TextStyle(fontSize: 11, color: Colors.white), ), ValueListenableBuilder( valueListenable: _sidValueNotifier, builder: (context, value, child) { return Text( textAlign: TextAlign.center, value, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: Colors.white), ); }, ), ], ) ], ), ), ], ), ); } _initMessages() { subscribe( queueId: Constants.messageTopicSessionStatusInfoUpdate, callback: (message) { _updateHeader(); }, ); /*MessageService().subscribe(Constants.messageTopicSessionStatusInfoUpdate, this, (sessionStatusInfoUpdateMessage) { _updateHeader(); });*/ subscribe( queueId: Constants.messageTopicLocationStatusUpdated, callback: (message) { _updateHeader(); }, ); /*MessageService().subscribe(Constants.messageTopicLocationStatusUpdated, this, (statusInfoUpdateMessage) { _updateHeader(); });*/ } _updateHeader() async { var status = await MyApp.isarInstance.locationStatus.where().findFirst(); var sessionStatus = await MyApp.isarInstance.sessionStatus.where().findFirst(); if (status != null) { _zipValueNotifier.value = status.zip; _zipRealValueNotifier.value = status.zipReal; } else if (sessionStatus != null) { _zipValueNotifier.value = sessionStatus.locZip; _zipRealValueNotifier.value = '---'; } else { _zipValueNotifier.value = '---'; _zipRealValueNotifier.value = '---'; } if (sessionStatus != null) { _sidValueNotifier.value = sessionStatus.sid; } /* rightButtonImage: Util.fetchFutureIcon(const Icon(Icons.info_outline, size: 20.0, color: Colors.white)), rightButtonOnPressed: () async { showInfosPopup(await MyApp.isarInstance.sessionStatus.where().findFirst()); }); */ } }