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

Introduce Bisq broadcast icon #1887

Merged
merged 11 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,138 @@
/*
* 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.AlertType;
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 javafx.collections.ListChangeListener;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Comparator;
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 final ListChangeListener<AuthorizedAlertData> listChangeListener;
private Pin authorizedAlertDataSetPin;

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

model.getSortedList().setComparator(Comparator.comparing(AuthorizedAlertData::getAlertType).reversed());

listChangeListener = change -> {
change.next();
if (change.wasAdded()) {
AuthorizedAlertData newItem = model.getSortedList().get(0);
AuthorizedAlertData displayed = model.getDisplayedAuthorizedAlertData();
if (displayed == null || newItem.getAlertType().ordinal() >= displayed.getAlertType().ordinal()) {
add(newItem);
}
} else if (change.wasRemoved()) {
change.getRemoved().stream()
.filter(e -> e.equals(model.getDisplayedAuthorizedAlertData()))
.findFirst()
.ifPresent(e -> handleRemove());
}
};
}

@Override
public void onActivate() {
updatePredicate();

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

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

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

model.getSortedList().addListener(listChangeListener);
model.getSortedList().stream().findFirst().ifPresent(this::add);
}

@Override
public void onDeactivate() {
authorizedAlertDataSetPin.unbind();
model.getSortedList().removeListener(listChangeListener);
}

void onClose() {
settingsService.getConsumedAlertIds().add(model.getDisplayedAuthorizedAlertData().getId());
updatePredicate();
handleRemove();
}

private void add(AuthorizedAlertData authorizedAlertData) {
model.setDisplayedAuthorizedAlertData(authorizedAlertData);
Optional<String> optionalMessage = authorizedAlertData.getMessage();

if (optionalMessage.isPresent()) {
log.info("Showing alert with message {}", optionalMessage.get());
model.getHeadline().set(authorizedAlertData.getHeadline().orElseThrow());
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 handleRemove() {
model.reset();
model.getSortedList().stream().findFirst().ifPresent(this::add);
}

private void updatePredicate() {
model.getFilteredList().setPredicate(authorizedAlertData ->
!settingsService.getConsumedAlertIds().contains(authorizedAlertData.getId()) &&
authorizedAlertData.getAlertType() != AlertType.BAN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

@Getter
public class AlertBannerModel implements Model {
private final ObservableList<AuthorizedAlertData> observableList = FXCollections.observableArrayList();
private final FilteredList<AuthorizedAlertData> filteredList = new FilteredList<>(observableList);
private final SortedList<AuthorizedAlertData> sortedList = new SortedList<>(filteredList);
private final Set<AuthorizedAlertData> displayedAlerts = new HashSet<>();

@Setter
private AuthorizedAlertData displayedAuthorizedAlertData;
private final BooleanProperty isAlertVisible = new SimpleBooleanProperty();
private final StringProperty headline = new SimpleStringProperty();
private final StringProperty message = new SimpleStringProperty();
private final ObjectProperty<AlertType> alertType = new SimpleObjectProperty<>();

public AlertBannerModel() {
}

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