Skip to content

Commit

Permalink
webadmin: display toast notifications via Frontend
Browse files Browse the repository at this point in the history
Before, in order to display a toast notification, the component needed
direct access to NotificationPresenterWidget.
With this patch this restriction has been removed. The notification
handler registers itself in the Frontend which allows public access.
  • Loading branch information
rszwajko committed Jul 14, 2022
1 parent 03cab35 commit 7bbbd92
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
*/
public class Frontend implements HasHandlers {

private NotificationHandler notificationHandler;

/**
* Provides static access to {@code Frontend} singleton instance.
*/
Expand Down Expand Up @@ -1109,4 +1111,20 @@ public WebAdminSettings getWebAdminSettings() {
}
return webAdminSettings;
}

public void setNotificationHandler(NotificationHandler handler) {
this.notificationHandler = handler;
}

/**
* Display a toast notification using registered {@linkplain NotificationHandler} (if available).
*/
public void showToast(String text, NotificationStatus status) {
// use local reference for async action
NotificationHandler handler = notificationHandler;
if (handler != null) {
Scheduler.get().scheduleDeferred(() -> handler.showToast(text, status));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.ovirt.engine.ui.frontend;

/**
* Generic notification handler that can be used across all modules.
*/
public interface NotificationHandler {
void showToast(String text, NotificationStatus status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ovirt.engine.ui.frontend;

/**
* Version without any dependencies to be used across all modules.
*/
public enum NotificationStatus {
INFO,
SUCCESS,
WARNING,
DANGER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public enum NotificationStatus {
this.iconStyleName = iconStyleName;
}

public static NotificationStatus from(org.ovirt.engine.ui.frontend.NotificationStatus status) {
switch (status){
case DANGER:
return DANGER;
case SUCCESS:
return SUCCESS;
case WARNING:
return WARNING;
case INFO:
default:
return INFO;
}
}

public String getStyleName() {
return styleName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.ovirt.engine.core.common.job.Job;
import org.ovirt.engine.core.common.job.JobExecutionStatus;
import org.ovirt.engine.ui.common.widget.uicommon.tasks.ToastNotification.NotificationStatus;
import org.ovirt.engine.ui.frontend.Frontend;
import org.ovirt.engine.ui.frontend.NotificationHandler;
import org.ovirt.engine.ui.uicommonweb.models.events.TaskListModel;
import org.ovirt.engine.ui.uicompat.EnumTranslator;
import org.ovirt.engine.ui.webadmin.section.main.presenter.AbstractOverlayPresenterWidget;
Expand All @@ -23,10 +25,16 @@
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;

public class TasksPresenterWidget extends AbstractOverlayPresenterWidget<TasksPresenterWidget.ViewDef> {
public class TasksPresenterWidget extends AbstractOverlayPresenterWidget<TasksPresenterWidget.ViewDef>
implements NotificationHandler {

protected final Logger log = Logger.getLogger(TasksPresenterWidget.class.getName());

@Override
public void showToast(String text, org.ovirt.engine.ui.frontend.NotificationStatus status) {
notificationPresenterWidget.createNotification(text, NotificationStatus.from(status));
}

public interface ViewDef extends AbstractOverlayPresenterWidget.ViewDef {
void updateTaskStatus(TaskListModel taskListModel);
}
Expand Down Expand Up @@ -71,6 +79,8 @@ protected void onReveal() {
@Override
public void onBind() {
super.onBind();
Frontend.getInstance().setNotificationHandler(this);
registerHandler(() -> Frontend.getInstance().setNotificationHandler(null));
taskModelProvider.getModel().getItemsChangedEvent().addListener((ev, sender, args) -> {
getView().updateTaskStatus(taskModelProvider.getModel());
if (lastSpecialTaskEndTime == NOT_INITIALIZED_END_TIME) {
Expand Down

0 comments on commit 7bbbd92

Please sign in to comment.