From 1f4234e381e6a92429bcca5e4930a217c4abf587 Mon Sep 17 00:00:00 2001 From: Sven Carstensen Date: Mon, 5 Feb 2024 14:46:57 +0100 Subject: [PATCH] Erweiterungen --- lib/content_views/main_page.dart | 9 +-- lib/main.dart | 12 ++-- lib/tools/message_service.dart | 109 ++++++++++--------------------- 3 files changed, 47 insertions(+), 83 deletions(-) diff --git a/lib/content_views/main_page.dart b/lib/content_views/main_page.dart index 97b8d33..21b80ba 100644 --- a/lib/content_views/main_page.dart +++ b/lib/content_views/main_page.dart @@ -97,6 +97,7 @@ class _MainPageState extends State { _exitMessages(); } + @override Widget build(BuildContext context) { return Stack( @@ -252,19 +253,19 @@ class _MainPageState extends State { }*/ _initMessages() { - MessageService().subscribe(Constants.messageTopicStatusInfoUpdate, (statusInfoUpdateMessage) { + MessageService().subscribe(Constants.messageTopicStatusInfoUpdate, this, (statusInfoUpdateMessage) { widget.sidValueNotifier.value = statusInfoUpdateMessage.status.sid; }); - MessageService().subscribe(Constants.messageTopicJobReceived, (JobReceivedMessage jobReceivedMessage) { + MessageService().subscribe(Constants.messageTopicJobReceived, this, (JobReceivedMessage jobReceivedMessage) { _jobReceived(jobReceivedMessage.job); }); } void _exitMessages() { - MessageService().unsubscribe(Constants.messageTopicStatusInfoUpdate); + MessageService().unsubscribe(Constants.messageTopicStatusInfoUpdate, this); - MessageService().unsubscribe(Constants.messageTopicJobReceived); + MessageService().unsubscribe(Constants.messageTopicJobReceived, this); } void _refreshBadges() async { diff --git a/lib/main.dart b/lib/main.dart index 0f71b41..56997fb 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -304,22 +304,22 @@ class _MainState extends State
with SingleTickerProviderStateMixin { } void _initMessages() { - MessageService().subscribe(Constants.messageTopicPushContentView, (ContentViewWidget widget) { + MessageService().subscribe(Constants.messageTopicPushContentView, this, (ContentViewWidget widget) { _pushContentView(widget); }); - MessageService().subscribe(Constants.messageTopicPopContentView, (ContentViewWidget widget) { + MessageService().subscribe(Constants.messageTopicPopContentView, this, (ContentViewWidget widget) { _popContentView(); }); - MessageService().subscribe(Constants.messageTopicReplaceContentView, (ContentViewWidget widget) { + MessageService().subscribe(Constants.messageTopicReplaceContentView, this, (ContentViewWidget widget) { _replaceContentView(widget); }); - MessageService().subscribe(Constants.messageTopicHideRightButton, (hideButton) { + MessageService().subscribe(Constants.messageTopicHideRightButton, this, (hideButton) { _hideRightButton(hideButton); }); - MessageService().subscribe(Constants.messageTopicConnectionStatusChanged, (ConnectionStatus connectionStatus) { + MessageService().subscribe(Constants.messageTopicConnectionStatusChanged, this, (ConnectionStatus connectionStatus) { _connectionStatusChanged(connectionStatus); }); - MessageService().subscribe(Constants.messageTopicJobReceived, (JobReceivedMessage jobReceivedMessage) { + MessageService().subscribe(Constants.messageTopicJobReceived, this, (JobReceivedMessage jobReceivedMessage) { _jobReceived(jobReceivedMessage.job); }); } diff --git a/lib/tools/message_service.dart b/lib/tools/message_service.dart index 8507a82..e14b5d3 100644 --- a/lib/tools/message_service.dart +++ b/lib/tools/message_service.dart @@ -1,11 +1,15 @@ typedef EventHandler = void Function(T args); -/// The Custom Events Service that can publish data -/// from one module/component to another. -class MessageService { - static final MessageService _singleton = new MessageService._internal(); +class _MapEntry { + late dynamic owner; - Map _callbackMaps = Map(); + late dynamic handler; +} + +class MessageService { + static final MessageService _singleton = MessageService._internal(); + + final Map> _callbackMaps = >{}; factory MessageService() { return _singleton; @@ -13,80 +17,39 @@ class MessageService { MessageService._internal(); - /// Subscribe to an event topic. Events that - /// get posted to that topic will trigger - /// the provided handler - /// * [topic] The topic to subscribe to - /// * [handler] The event handler - subscribe(String topic, EventHandler? handler) { - List>? handlers = _callbackMaps[topic]; - if (handler == null) { - _callbackMaps.remove(topic); - return; - } - if (handlers == null) { - handlers = []; - _callbackMaps[topic] = handlers; - } - handlers.add(handler); - } + subscribe(String topic, dynamic owner, EventHandler? handler) { + var entry = _MapEntry(); + entry.owner = owner; + entry.handler = handler; - /// Unsubscribe from the given topic. Your handler - /// will no longer recieve events published to this - /// topic - /// * [topic] The topic to unsubscribe from - /// * [handler] The event handler - unsubscribe(String topic, {EventHandler? 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); - } + if (_callbackMaps.containsKey(topic)) { + _callbackMaps[topic]!.remove(entry); + _callbackMaps[topic]!.add(entry); + } else { + _callbackMaps[topic] = <_MapEntry>{}; + _callbackMaps[topic]!.add(entry); } } - /// 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(String topic, {T? arguments}) { - List>? handlers; - try { - handlers = _callbackMaps[topic]; - } catch (_) { - throw FormatException( - 'Type mismatch between Publisher Arguments and what Subscriber Expects'); - } + unsubscribe(String topic, dynamic owner) { + if (_callbackMaps.containsKey(topic)) { + for (var entry in _callbackMaps[topic]!) { + if (entry.owner == owner) { + _callbackMaps[topic]!.remove(entry); - if (handlers == null || handlers.isEmpty) { - return; - } - - handlers.forEach((EventHandler handler) { - try { - if (arguments != null) { - handler(arguments); - } else { - throw 'arguments null'; + break; } - } catch (_) { - print('Error Occured while executing handler for topic $topic'); - print(_); } - }); + } + } + + publish(String topic, {T? arguments}) { + var entries = _callbackMaps[topic]; + + if (entries != null) { + for (var entry in entries) { + entry.handler(arguments); + } + } } }