627 lines
23 KiB
Dart
627 lines
23 KiB
Dart
import 'package:dart_mq/dart_mq.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:isar/isar.dart';
|
|
import 'package:mqtt5_client/mqtt5_client.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/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/popups/infos_popup.dart';
|
|
import 'package:stadtbote/protocoll_messages/logout_message.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<MainPage> createState() => _MainPageState();
|
|
|
|
@override
|
|
Future<Widget> getHeaderWidget(BuildContext context) async {
|
|
await Future.delayed(const Duration(microseconds: 0));
|
|
|
|
return _HeaderWidget();
|
|
}
|
|
}
|
|
|
|
class _MainPageState extends State<MainPage> with ConsumerMixin, ProducerMixin {
|
|
final ValueNotifier<int> _assignedJobsValueNotifier = ValueNotifier<int>(0);
|
|
final ValueNotifier<int> _acceptedJobsValueNotifier = ValueNotifier<int>(0);
|
|
final ValueNotifier<int> _messagesValueNotifier = ValueNotifier<int>(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<int>(
|
|
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<ContentViewWidget>(Constants.messageTopicPushContentView, arguments: const AssignedJobsPage())
|
|
}
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
SizedBox(height: _loggedOut == true ? 0 : 20),
|
|
Visibility(
|
|
visible: !_loggedOut,
|
|
child: ValueListenableBuilder<int>(
|
|
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<ContentViewWidget>(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<ContentViewWidget>(Constants.messageTopicPushContentView, arguments: const FinishedJobsPage())
|
|
}
|
|
),
|
|
),
|
|
SizedBox(height: _loggedOut == true ? 0 : 20),
|
|
Visibility(
|
|
visible: !_loggedOut,
|
|
child: ValueListenableBuilder<int>(
|
|
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<ContentViewWidget>(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<ContentViewWidget>(Constants.messageTopicPushContentView, arguments: const ApproachAreaPage())
|
|
}),
|
|
),
|
|
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);
|
|
},
|
|
);
|
|
/*MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, this, (JobReceivedMessage jobReceivedMessage) {
|
|
_jobReceived(jobReceivedMessage.job);
|
|
});*/
|
|
|
|
subscribe(
|
|
queueId: Constants.messageTopicMessageReceived,
|
|
callback: (message) {
|
|
_refreshBadges();
|
|
},
|
|
);
|
|
/*MessageService().subscribe<MessageReceivedMessage>(Constants.messageTopicMessageReceived, this, (MessageReceivedMessage mesage) {
|
|
_refreshBadges();
|
|
});*/
|
|
|
|
subscribe(
|
|
queueId: Constants.messageTopicJobDeleted,
|
|
callback: (message) {
|
|
_refreshBadges();
|
|
},
|
|
);
|
|
/*MessageService().subscribe<JobDeletedMessage>(Constants.messageTopicJobDeleted, this, (JobDeletedMessage job) {
|
|
_refreshBadges();
|
|
});*/
|
|
|
|
subscribe(
|
|
queueId: Constants.messageTopicUpdateBadges,
|
|
callback: (message) {
|
|
_refreshBadges();
|
|
},
|
|
);
|
|
/*MessageService().subscribe<RefreshBadgesMessage>(Constants.messageTopicUpdateBadges, this, (RefreshBadgesMessage 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 {
|
|
// TODO MESSAGING
|
|
sendMessage(
|
|
routingKey: Constants.messageTopicConnectClient,
|
|
payload: ConnectClientMessage(),
|
|
);
|
|
|
|
//await MessageService().publish<ConnectClientMessage>(Constants.messageTopicConnectClient, arguments: 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 {
|
|
// TODO MESSAGING
|
|
sendMessage(
|
|
routingKey: Constants.messageTopicDisconnectClient,
|
|
payload: DisconnectClientMessage(),
|
|
);
|
|
|
|
//await MessageService().publish<DisconnectClientMessage>(Constants.messageTopicDisconnectClient, arguments: DisconnectClientMessage());
|
|
|
|
Future.delayed(const Duration(seconds: 3), () async {
|
|
// TODO MESSAGING
|
|
sendMessage(
|
|
routingKey: Constants.messageTopicDisconnectClient,
|
|
payload: DisconnectClientMessage(),
|
|
);
|
|
|
|
//await MessageService().publish<DisconnectClientMessage>(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(), qos: MqttQos.exactlyOnce),
|
|
);
|
|
|
|
//await MessageService().publish<PublishPayloadMessage>(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<String> _zipValueNotifier = ValueNotifier("-----");
|
|
final ValueNotifier<String> _zipRealValueNotifier = ValueNotifier("-----");
|
|
final ValueNotifier<String> _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: <Widget>[
|
|
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: <Widget>[
|
|
SizedBox(
|
|
width: cellWidth,
|
|
child: ValueListenableBuilder<String>(
|
|
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<String>(
|
|
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<String>(
|
|
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<SessionStatusInfoUpdate>(Constants.messageTopicSessionStatusInfoUpdate, this, (sessionStatusInfoUpdateMessage) {
|
|
_updateHeader();
|
|
});*/
|
|
|
|
subscribe(
|
|
queueId: Constants.messageTopicLocationStatusUpdated,
|
|
callback: (message) {
|
|
_updateHeader();
|
|
},
|
|
);
|
|
|
|
/*MessageService().subscribe<LocationStatusUpdate>(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());
|
|
});
|
|
*/
|
|
}
|
|
}
|