Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webadmin: display toast notifications via Frontend #129

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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