Erweiterungen

This commit is contained in:
Sven committed 2024-02-05 14:46:57 +01:00
1 parent 6af8cb63e6
commit 1f4234e381
3 files changed
+50 -86

No files matched your search

+5 -4
View File
@@ -97,6 +97,7 @@ class _MainPageState extends State<MainPage> {
_exitMessages(); _exitMessages();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Stack( return Stack(
@@ -252,19 +253,19 @@ class _MainPageState extends State<MainPage> {
}*/ }*/
_initMessages() { _initMessages() {
MessageService().subscribe<StatusInfoUpdate>(Constants.messageTopicStatusInfoUpdate, (statusInfoUpdateMessage) { MessageService().subscribe<StatusInfoUpdate>(Constants.messageTopicStatusInfoUpdate, this, (statusInfoUpdateMessage) {
widget.sidValueNotifier.value = statusInfoUpdateMessage.status.sid; widget.sidValueNotifier.value = statusInfoUpdateMessage.status.sid;
}); });
MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, (JobReceivedMessage jobReceivedMessage) { MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, this, (JobReceivedMessage jobReceivedMessage) {
_jobReceived(jobReceivedMessage.job); _jobReceived(jobReceivedMessage.job);
}); });
} }
void _exitMessages() { void _exitMessages() {
MessageService().unsubscribe<StatusInfoUpdate>(Constants.messageTopicStatusInfoUpdate); MessageService().unsubscribe<StatusInfoUpdate>(Constants.messageTopicStatusInfoUpdate, this);
MessageService().unsubscribe<StatusInfoUpdate>(Constants.messageTopicJobReceived); MessageService().unsubscribe<StatusInfoUpdate>(Constants.messageTopicJobReceived, this);
} }
void _refreshBadges() async { void _refreshBadges() async {
+6 -6
View File
@@ -304,22 +304,22 @@ class _MainState extends State<Main> with SingleTickerProviderStateMixin {
} }
void _initMessages() { void _initMessages() {
MessageService().subscribe<ContentViewWidget>(Constants.messageTopicPushContentView, (ContentViewWidget widget) { MessageService().subscribe<ContentViewWidget>(Constants.messageTopicPushContentView, this, (ContentViewWidget widget) {
_pushContentView(widget); _pushContentView(widget);
}); });
MessageService().subscribe<ContentViewWidget>(Constants.messageTopicPopContentView, (ContentViewWidget widget) { MessageService().subscribe<ContentViewWidget>(Constants.messageTopicPopContentView, this, (ContentViewWidget widget) {
_popContentView(); _popContentView();
}); });
MessageService().subscribe<ContentViewWidget>(Constants.messageTopicReplaceContentView, (ContentViewWidget widget) { MessageService().subscribe<ContentViewWidget>(Constants.messageTopicReplaceContentView, this, (ContentViewWidget widget) {
_replaceContentView(widget); _replaceContentView(widget);
}); });
MessageService().subscribe<bool>(Constants.messageTopicHideRightButton, (hideButton) { MessageService().subscribe<bool>(Constants.messageTopicHideRightButton, this, (hideButton) {
_hideRightButton(hideButton); _hideRightButton(hideButton);
}); });
MessageService().subscribe<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, (ConnectionStatus connectionStatus) { MessageService().subscribe<ConnectionStatus>(Constants.messageTopicConnectionStatusChanged, this, (ConnectionStatus connectionStatus) {
_connectionStatusChanged(connectionStatus); _connectionStatusChanged(connectionStatus);
}); });
MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, (JobReceivedMessage jobReceivedMessage) { MessageService().subscribe<JobReceivedMessage>(Constants.messageTopicJobReceived, this, (JobReceivedMessage jobReceivedMessage) {
_jobReceived(jobReceivedMessage.job); _jobReceived(jobReceivedMessage.job);
}); });
} }
+39 -76
View File
@@ -1,11 +1,15 @@
typedef EventHandler<T> = void Function(T args); typedef EventHandler<T> = void Function(T args);
/// The Custom Events Service that can publish data class _MapEntry {
/// from one module/component to another. late dynamic owner;
class MessageService {
static final MessageService _singleton = new MessageService._internal();
Map<String, dynamic> _callbackMaps = Map<String, dynamic>(); late dynamic handler;
}
class MessageService {
static final MessageService _singleton = MessageService._internal();
final Map<String, Set<_MapEntry>> _callbackMaps = <String, Set<_MapEntry>>{};
factory MessageService() { factory MessageService() {
return _singleton; return _singleton;
@@ -13,80 +17,39 @@ class MessageService {
MessageService._internal(); MessageService._internal();
/// Subscribe to an event topic. Events that subscribe<T>(String topic, dynamic owner, EventHandler<T>? handler) {
/// get posted to that topic will trigger var entry = _MapEntry();
/// the provided handler entry.owner = owner;
/// * [topic] The topic to subscribe to entry.handler = handler;
/// * [handler] The event handler
subscribe<T>(String topic, EventHandler<T>? handler) {
List<EventHandler<T>>? handlers = _callbackMaps[topic];
if (handler == null) {
_callbackMaps.remove(topic);
return;
}
if (handlers == null) {
handlers = [];
_callbackMaps[topic] = handlers;
}
handlers.add(handler);
}
/// Unsubscribe from the given topic. Your handler if (_callbackMaps.containsKey(topic)) {
/// will no longer recieve events published to this _callbackMaps[topic]!.remove(entry);
/// topic _callbackMaps[topic]!.add(entry);
/// * [topic] The topic to unsubscribe from
/// * [handler] The event handler
unsubscribe<T>(String topic, {EventHandler<T>? handler}) {
if (handler == null) {
_callbackMaps.remove(topic);
return;
}
final handlers = _callbackMaps[topic];
if (handlers == null) {
return;
}
// We now need to remove specific handler
final handlerIndex = handlers.indexOf(handler);
if (handlerIndex >= 0) {
handlers.removeAt(handlerIndex);
if (handlers.length == 0) {
_callbackMaps.remove(topic);
}
}
}
/// Publish an Event to given topic.
/// * [topic] The topic to publish to
/// * [arguments] The arguments to pass to
/// the handlers. It's better if custom
/// Structure is used.
publish<T>(String topic, {T? arguments}) {
List<EventHandler<T>>? handlers;
try {
handlers = _callbackMaps[topic];
} catch (_) {
throw FormatException(
'Type mismatch between Publisher Arguments and what Subscriber Expects');
}
if (handlers == null || handlers.isEmpty) {
return;
}
handlers.forEach((EventHandler<T> handler) {
try {
if (arguments != null) {
handler(arguments);
} else { } else {
throw 'arguments null'; _callbackMaps[topic] = <_MapEntry>{};
_callbackMaps[topic]!.add(entry);
}
}
unsubscribe<T>(String topic, dynamic owner) {
if (_callbackMaps.containsKey(topic)) {
for (var entry in _callbackMaps[topic]!) {
if (entry.owner == owner) {
_callbackMaps[topic]!.remove(entry);
break;
}
}
}
}
publish<T>(String topic, {T? arguments}) {
var entries = _callbackMaps[topic];
if (entries != null) {
for (var entry in entries) {
entry.handler(arguments);
} }
} catch (_) {
print('Error Occured while executing handler for topic $topic');
print(_);
} }
});
} }
} }