Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
relativisticelectron committed Jun 20, 2022
1 parent 4e74d88 commit 2819986
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
28 changes: 16 additions & 12 deletions src/cryptoadvance/specter/notifications/notification_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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'
Expand All @@ -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

Expand All @@ -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"],
)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/cryptoadvance/specter/notifications/ui_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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"


Expand Down
32 changes: 16 additions & 16 deletions src/cryptoadvance/specter/templates/includes/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}});
}


Expand Down Expand Up @@ -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()}
Expand All @@ -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") {
Expand All @@ -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 {
Expand All @@ -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.
Expand All @@ -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'];
Expand All @@ -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')
}


Expand All @@ -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){
Expand Down
8 changes: 2 additions & 6 deletions src/cryptoadvance/specter/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2819986

Please sign in to comment.