From 281998640cc7a218222011ee9081f5635df6aee2 Mon Sep 17 00:00:00 2001 From: relativistic electron Date: Mon, 20 Jun 2022 06:28:32 +0200 Subject: [PATCH] refactoring and https://github.com/cryptoadvance/specter-desktop/pull/1766#discussion_r901090315 --- .../notifications/notification_manager.py | 28 +++++++++------- .../specter/notifications/ui_notifications.py | 4 +-- .../templates/includes/notifications.js | 32 +++++++++---------- src/cryptoadvance/specter/user.py | 8 ++--- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/cryptoadvance/specter/notifications/notification_manager.py b/src/cryptoadvance/specter/notifications/notification_manager.py index 24de9c311f..d4ac99500e 100644 --- a/src/cryptoadvance/specter/notifications/notification_manager.py +++ b/src/cryptoadvance/specter/notifications/notification_manager.py @@ -61,7 +61,11 @@ def set_notification_shown(self, notification_id, target_ui): notification.set_shown(target_ui) def treat_internal_message(self, internal_notification): - "treat an internal_notification" + """ + Notifications with the title='internal_notification' are not displayed to the user, but used for things like: + - handling callbacks (like on_close or on_show) + - messaging back that a target ui is unavailable (the notification is then also rebroadcasted) + """ if "internal_notification" not in internal_notification["target_uis"]: return internal_notification logger.debug(f"treat_internal_message {internal_notification}") @@ -70,7 +74,10 @@ def treat_internal_message(self, internal_notification): internal_notification["data"]["id"] ) - if internal_notification["title"] == "notification_target_ui_unavailable": + if ( + internal_notification["title"] + == "notification_deactivate_target_ui_and_rebroadcast" + ): # deactivate target_ui and rebroadcast logger.debug( f'{internal_notification["data"]["target_ui"]} is unavailable, now deactivating this target_ui and rebroadcasting' @@ -80,13 +87,13 @@ def treat_internal_message(self, internal_notification): return self.show(referenced_notification) - if internal_notification["title"] == "notification_shown": + if internal_notification["title"] == "on_show": self.set_notification_shown( referenced_notification["id"], internal_notification["data"]["target_ui"], ) - if internal_notification["title"] == "callback_notification_close": + if internal_notification["title"] == "on_close": if not referenced_notification: return @@ -96,13 +103,10 @@ def treat_internal_message(self, internal_notification): if ui_notification.name in referenced_notification["target_uis"]: matching_ui_notifications.append(ui_notification) - # call all callback_notification_close functions of matching_ui_notifications + # call all on_close functions of matching_ui_notifications for ui_notification in matching_ui_notifications: - if ( - "callback_notification_close" in dir(ui_notification) - and ui_notification.callback_notification_close - ): - ui_notification.callback_notification_close( + if "on_close" in dir(ui_notification) and ui_notification.on_close: + ui_notification.on_close( referenced_notification["id"], internal_notification["data"]["target_ui"], ) @@ -162,12 +166,12 @@ def find_notification(self, notification_id): if notification["id"] == notification_id: return notification - def callback_notification_close(self, notification_id, target_ui=None): + def on_close(self, notification_id, target_ui=None): "Deletes the notification. It does not wait until the last of the target_uis was closed." notification = self.find_notification(notification_id) if not notification: logging.debug( - f"callback_notification_close: Notification with id {notification_id} not found. Perhaps it was closed already in a different target_ui?" + f"on_close: Notification with id {notification_id} not found. Perhaps it was closed already in a different target_ui?" ) return diff --git a/src/cryptoadvance/specter/notifications/ui_notifications.py b/src/cryptoadvance/specter/notifications/ui_notifications.py index c6d891041c..9a7843902d 100644 --- a/src/cryptoadvance/specter/notifications/ui_notifications.py +++ b/src/cryptoadvance/specter/notifications/ui_notifications.py @@ -7,7 +7,7 @@ class BaseUINotifications: - "A base class defining functions that every user fased UI Notification display system should have" + "A base class defining functions that every user faced UI Notification display system should have" def __init__(self): self.compatible_notification_types = { @@ -123,7 +123,7 @@ def __init__(self): NotificationTypes.error, NotificationTypes.exception, } - self.callback_notification_close = None + self.on_close = None self.name = "js_message_box" diff --git a/src/cryptoadvance/specter/templates/includes/notifications.js b/src/cryptoadvance/specter/templates/includes/notifications.js index 1c6b1a71e4..c69602ea6e 100644 --- a/src/cryptoadvance/specter/templates/includes/notifications.js +++ b/src/cryptoadvance/specter/templates/includes/notifications.js @@ -30,20 +30,20 @@ async function requestCreateNotification(title, options){ -function callback_notification_close(id, target_ui){ +function on_close(id, target_ui){ //console.log('closed message') - requestCreateNotification('callback_notification_close', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui}}); + requestCreateNotification('on_close', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui}}); } -function notification_shown(id, target_ui, success=true){ +function on_show(id, target_ui, success=true){ //console.log('closed message') - requestCreateNotification('notification_shown', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui, 'success':success}}); + requestCreateNotification('on_show', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui, 'success':success}}); } -function notification_target_ui_unavailable(id, target_ui) { - requestCreateNotification('notification_target_ui_unavailable', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui}}); +function notification_deactivate_target_ui_and_rebroadcast(id, target_ui) { + requestCreateNotification('notification_deactivate_target_ui_and_rebroadcast', {target_uis:['internal_notification'], data:{'id':id, 'target_ui':target_ui}}); } @@ -90,11 +90,11 @@ function webapi_notification(js_notification, retries_if_permission_default=2) { // do something }); notification.onclose = (() => { - callback_notification_close(js_notification['id'], 'WebAPI') + on_close(js_notification['id'], 'WebAPI') }); notification.onshow = (() => { // do something - notification_shown(js_notification['id'], 'WebAPI') + on_show(js_notification['id'], 'WebAPI') }); function closeNotification(){notification.close()} @@ -106,7 +106,7 @@ function webapi_notification(js_notification, retries_if_permission_default=2) { if (!("Notification" in window)) { console.log("This browser does not support desktop notification"); - notification_target_ui_unavailable(js_notification['id'], 'WebAPI'); + notification_deactivate_target_ui_and_rebroadcast(js_notification['id'], 'WebAPI'); } // Let's check whether notification permissions have already been granted else if (Notification.permission === "granted") { @@ -115,7 +115,7 @@ function webapi_notification(js_notification, retries_if_permission_default=2) { } else if (Notification.permission === "denied") { // If it's okay let's create a notification console.log(`Notification.requestPermission() = ${Notification.permission}`); - notification_target_ui_unavailable(js_notification['id'], 'WebAPI'); + notification_deactivate_target_ui_and_rebroadcast(js_notification['id'], 'WebAPI'); } // Otherwise "default", we need to ask the user for permission else { @@ -126,7 +126,7 @@ function webapi_notification(js_notification, retries_if_permission_default=2) { } else if (permission === "denied") { // not granted console.log(`Notification.requestPermission() = ${permission}`); - notification_target_ui_unavailable(js_notification['id'], 'WebAPI'); + notification_deactivate_target_ui_and_rebroadcast(js_notification['id'], 'WebAPI'); } else { // permission is probably "default", meaning the user has neither granted nor blocked the Notifications. // The user can afterwards allow or block notifications. The notification needs reboradcasting. @@ -152,8 +152,8 @@ function webapi_notification(js_notification, retries_if_permission_default=2) { function javascript_popup_message(js_notification){ - function callback_notification_close_id(){ - callback_notification_close(js_notification['id'], 'js_message_box') + function on_close_id(){ + on_close(js_notification['id'], 'js_message_box') } var message = js_notification['title']; @@ -166,12 +166,12 @@ function javascript_popup_message(js_notification){ }); msgbox.show( message, - callback_notification_close_id, + on_close_id, 'Close', image=js_notification['options']['image'], ); - notification_shown(js_notification['id'], 'js_message_box') + on_show(js_notification['id'], 'js_message_box') } @@ -185,7 +185,7 @@ function js_logging_notification(js_notification){ } else { console.log(js_notification); } - notification_shown(js_notification['id'], 'js_console') + on_show(js_notification['id'], 'js_console') } async function show_notification(ui_name, js_notification){ diff --git a/src/cryptoadvance/specter/user.py b/src/cryptoadvance/specter/user.py index 77b6359ac2..16272fa9ee 100644 --- a/src/cryptoadvance/specter/user.py +++ b/src/cryptoadvance/specter/user.py @@ -106,12 +106,8 @@ def __init__( ui_notifications.PrintNotifications(), ] ) - js_notifications.callback_notification_close = ( - self.notification_manager.callback_notification_close - ) - webapi_notifications.callback_notification_close = ( - self.notification_manager.callback_notification_close - ) + js_notifications.on_close = self.notification_manager.on_close + webapi_notifications.on_close = self.notification_manager.on_close # TODO: User obj instantiation belongs in UserManager @classmethod