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

Handle consumed alert state #1881

Merged
merged 9 commits into from
Mar 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
import bisq.application.ApplicationService;
import bisq.bisq_easy.NavigationTarget;
import bisq.bonded_roles.security_manager.alert.AlertService;
import bisq.bonded_roles.security_manager.alert.AuthorizedAlertData;
import bisq.common.observable.collection.CollectionObserver;
import bisq.desktop.ServiceProvider;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Controller;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.common.view.NavigationController;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.main.alert.AlertBannerController;
import bisq.desktop.main.content.ContentController;
import bisq.desktop.main.left.LeftNavController;
import bisq.desktop.main.notification.NotificationPanelController;
Expand All @@ -49,28 +48,30 @@ public class MainController extends NavigationController {
private final MainView view;
private final ServiceProvider serviceProvider;
private final LeftNavController leftNavController;
private final AlertService alertService;
private final SettingsService settingsService;
private final UpdaterService updaterService;
private final ApplicationService.Config config;
private final AlertBannerController alertBannerController;

public MainController(ServiceProvider serviceProvider) {
super(NavigationTarget.MAIN);

this.serviceProvider = serviceProvider;
settingsService = serviceProvider.getSettingsService();
alertService = serviceProvider.getBondedRolesService().getAlertService();
AlertService alertService = serviceProvider.getBondedRolesService().getAlertService();
updaterService = serviceProvider.getUpdaterService();
config = serviceProvider.getConfig();

leftNavController = new LeftNavController(serviceProvider);
TopPanelController topPanelController = new TopPanelController(serviceProvider);
NotificationPanelController notificationPanelController = new NotificationPanelController(serviceProvider);
alertBannerController = new AlertBannerController(settingsService, alertService);
view = new MainView(model,
this,
leftNavController.getView().getRoot(),
topPanelController.getView().getRoot(),
notificationPanelController.getView().getRoot());
notificationPanelController.getView().getRoot(),
alertBannerController.getView().getRoot());
}

@Override
Expand All @@ -93,43 +94,6 @@ public void onActivate() {
}
}

alertService.getAuthorizedAlertDataSet().addObserver(new CollectionObserver<>() {
@Override
public void add(AuthorizedAlertData authorizedAlertData) {
if (authorizedAlertData == null) {
return;
}
UIThread.run(() -> {
if (settingsService.getConsumedAlertIds().contains(authorizedAlertData.getId())) {
return;
}
settingsService.getConsumedAlertIds().add(authorizedAlertData.getId());
Optional<String> optionalMessage = authorizedAlertData.getMessage();
switch (authorizedAlertData.getAlertType()) {
case INFO:
optionalMessage.ifPresentOrElse(message -> new Popup().attention(message).show(),
() -> log.warn("optionalMessage not present"));
break;
case WARN:
case EMERGENCY:
optionalMessage.ifPresentOrElse(message -> new Popup().warning(message).show(),
() -> log.warn("optionalMessage not present"));
break;
case BAN:
break;
}
});
}

@Override
public void remove(Object element) {
}

@Override
public void clear() {
}
});

updaterService.getReleaseNotification().addObserver(releaseNotification -> {
if (releaseNotification == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public MainView(MainModel model,
MainController controller,
AnchorPane leftNav,
HBox topPanel,
BorderPane notificationPanel) {
BorderPane notificationPanel,
BorderPane alertBanner) {
super(new HBox(), model, controller);

anchorPane = new AnchorPane();
VBox.setVgrow(anchorPane, Priority.ALWAYS);
VBox vBox = new VBox(topPanel, notificationPanel, anchorPane);
VBox vBox = new VBox(topPanel, notificationPanel, alertBanner, anchorPane);
vBox.setFillWidth(true);
HBox.setHgrow(vBox, Priority.ALWAYS);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.main.alert;

import bisq.bonded_roles.security_manager.alert.AlertService;
import bisq.bonded_roles.security_manager.alert.AuthorizedAlertData;
import bisq.common.observable.Pin;
import bisq.common.observable.collection.CollectionObserver;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Controller;
import bisq.settings.SettingsService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

@Slf4j
public class AlertBannerController implements Controller {
private final AlertBannerModel model;
@Getter
private final AlertBannerView view;
private final SettingsService settingsService;
private final AlertService alertService;
private Pin authorizedAlertDataSetPin;

public AlertBannerController(SettingsService settingsService, AlertService alertService) {
this.settingsService = settingsService;
this.alertService = alertService;
model = new AlertBannerModel();
view = new AlertBannerView(model, this);
}

@Override
public void onActivate() {
authorizedAlertDataSetPin = alertService.getAuthorizedAlertDataSet().addObserver(new CollectionObserver<>() {
@Override
public void add(AuthorizedAlertData authorizedAlertData) {
if (authorizedAlertData == null) {
return;
}
UIThread.run(() -> addAlert(authorizedAlertData));
}

@Override
public void remove(Object element) {
if (element instanceof AuthorizedAlertData) {
UIThread.run(() -> removeAlert((AuthorizedAlertData) element));
}
}

@Override
public void clear() {
UIThread.run(() -> removeAllAlerts());
}
});

settingsService.getTradeRulesConfirmed().addObserver(e -> {

});
}


@Override
public void onDeactivate() {
authorizedAlertDataSetPin.unbind();
}


private void addAlert(AuthorizedAlertData authorizedAlertData) {
if (settingsService.getConsumedAlertIds().contains(authorizedAlertData.getId())) {
model.getIsAlertVisible().set(false);
return;
}

model.setDisplayedAuthorizedAlertData(authorizedAlertData);
Optional<String> optionalMessage = authorizedAlertData.getMessage();

if (optionalMessage.isPresent()) {
log.info("Showing alert with message {}", optionalMessage.get());
model.getMessage().set(authorizedAlertData.getMessage().orElseThrow());
model.getAlertType().set(authorizedAlertData.getAlertType());
model.getIsAlertVisible().set(true);
} else {
log.warn("optionalMessage not present");
model.getIsAlertVisible().set(false);
}
}

private void removeAlert(AuthorizedAlertData data) {
model.reset();
}

private void removeAllAlerts() {
model.reset();
}

void onClose() {
settingsService.getConsumedAlertIds().add(model.getDisplayedAuthorizedAlertData().getId());
model.reset();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.main.alert;

import bisq.bonded_roles.security_manager.alert.AlertType;
import bisq.bonded_roles.security_manager.alert.AuthorizedAlertData;
import bisq.desktop.common.view.Model;
import javafx.beans.property.*;
import lombok.Getter;
import lombok.Setter;

@Getter
public class AlertBannerModel implements Model {
@Setter
private AuthorizedAlertData displayedAuthorizedAlertData;
private final BooleanProperty isAlertVisible = new SimpleBooleanProperty();
private final StringProperty message = new SimpleStringProperty();
private final ObjectProperty<AlertType> alertType = new SimpleObjectProperty<>();

public AlertBannerModel() {
}

void reset() {
displayedAuthorizedAlertData = null;
isAlertVisible.set(false);
message.set(null);
alertType.set(null);
}
}
Loading
Loading