Skip to content

Commit

Permalink
refactor: Move EventBusListener from BooksView to BooksPresenter
Browse files Browse the repository at this point in the history
  • Loading branch information
TatuJLund committed Nov 2, 2024
1 parent 3aa2fab commit da8e3ab
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import org.vaadin.tatu.vaadincreate.backend.data.Category;
import org.vaadin.tatu.vaadincreate.backend.data.Product;
import org.vaadin.tatu.vaadincreate.backend.data.User.Role;
import org.vaadin.tatu.vaadincreate.crud.BooksPresenter.BooksChanged.BookChange;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus.EventBusListener;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects.LockingEvent;

/**
* This class provides an interface for the logical operations between the CRUD
Expand All @@ -30,7 +33,7 @@
* data.
*/
@SuppressWarnings("serial")
public class BooksPresenter implements Serializable {
public class BooksPresenter implements Serializable, EventBusListener {

private BooksView view;
private transient CompletableFuture<Void> future;
Expand All @@ -48,6 +51,7 @@ public class BooksPresenter implements Serializable {
*/
public BooksPresenter(BooksView booksView) {
view = booksView;
getEventBus().registerEventBusListener(this);
}

// This method is used to load products asynchronously.
Expand Down Expand Up @@ -107,6 +111,7 @@ public void requestUpdateProducts() {
*/
public void cancelUpdateProducts() {
unlockBook();
getEventBus().unregisterEventBusListener(this);
if (future != null) {
boolean cancelled = future.cancel(true);
future = null;
Expand Down Expand Up @@ -389,6 +394,21 @@ public Product getDraft() {
return draft;
}

@Override
public void eventFired(Object event) {
if (event instanceof LockingEvent) {
var id = ((LockingEvent) event).getId();
view.refreshProductAsync(id);
}
if (event instanceof BooksChanged) {
var product = ((BooksChanged) event).getProduct();
if (((BooksChanged) event).getChange() != BookChange.SAVE) {
return;
}
view.refreshProductAsync(product);
}
}

private LockedObjects getLockedBooks() {
return LockedObjects.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
import org.vaadin.tatu.vaadincreate.backend.data.Category;
import org.vaadin.tatu.vaadincreate.backend.data.Product;
import org.vaadin.tatu.vaadincreate.backend.data.User.Role;
import org.vaadin.tatu.vaadincreate.crud.BooksPresenter.BooksChanged;
import org.vaadin.tatu.vaadincreate.crud.BooksPresenter.BooksChanged.BookChange;
import org.vaadin.tatu.vaadincreate.crud.form.BookForm;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus;
import org.vaadin.tatu.vaadincreate.eventbus.EventBus.EventBusListener;
import org.vaadin.tatu.vaadincreate.i18n.HasI18N;
import org.vaadin.tatu.vaadincreate.i18n.I18n;
import org.vaadin.tatu.vaadincreate.locking.LockedObjects.LockingEvent;
import org.vaadin.tatu.vaadincreate.util.Utils;

import com.vaadin.data.provider.ListDataProvider;
Expand Down Expand Up @@ -49,8 +44,7 @@
*/
@SuppressWarnings({ "serial", "java:S2160" })
@RolesPermitted({ Role.USER, Role.ADMIN })
public class BooksView extends CssLayout
implements View, HasI18N, EventBusListener {
public class BooksView extends CssLayout implements View, HasI18N {

public static final String VIEW_NAME = "inventory";

Expand Down Expand Up @@ -113,7 +107,6 @@ public BooksView() {
addComponent(barAndGridLayout);
addComponent(form);

getEventBus().registerEventBusListener(this);
presenter.init();
}

Expand Down Expand Up @@ -450,7 +443,6 @@ public void editProduct(Product product) {
@Override
public void detach() {
super.detach();
getEventBus().unregisterEventBusListener(this);
// If detach happens before completion of data fetch, cancel the fetch
presenter.cancelUpdateProducts();
if (getUI().getSession().getSession() == null) {
Expand Down Expand Up @@ -515,31 +507,41 @@ private ConfirmDialog createDiscardChangesConfirmDialog() {
return dialog;
}

@Override
public void eventFired(Object event) {
if (event instanceof LockingEvent) {
var bookEvent = (LockingEvent) event;
Utils.access(ui, () -> {
if (canPush()) {
dataProvider.getItems().stream().filter(
book -> book.getId().equals(bookEvent.getId()))
.findFirst().ifPresent(dataProvider::refreshItem);
}
});
}
if (event instanceof BooksChanged) {
var bookEvent = (BooksChanged) event;
Utils.access(ui, () -> {
if (canPush() && bookEvent.getChange() == BookChange.SAVE
&& grid.getEdited() != bookEvent.getProduct().getId()) {
logger.debug("Refreshing item ({}) {}",
bookEvent.getProduct().getId(),
bookEvent.getProduct().getProductName());
grid.setEdited(bookEvent.getProduct());
dataProvider.refreshItem(bookEvent.getProduct());
}
});
}
/**
* Refreshes the given product asynchronously.
*
* @param product
* the product to be refreshed
*/
public void refreshProductAsync(Product product) {
Utils.access(ui, () -> {
if (canPush() && grid.getEdited() != product.getId()) {
logger.debug("Refreshing item ({}) {}", product.getId(),
product.getProductName());
grid.setEdited(product);
dataProvider.refreshItem(product);
}
});
}

/**
* Refreshes the product asynchronously by its ID. This method uses the
* provided UI access utility to ensure that the refresh operation is
* performed in a thread-safe manner. If the UI can push updates, it filters
* the data provider's items to find the book with the specified ID and
* refreshes that item.
*
* @param id
* the ID of the product to refresh
*/
public void refreshProductAsync(Integer id) {
Utils.access(ui, () -> {
if (canPush()) {
dataProvider.getItems().stream()
.filter(book -> book.getId().equals(id)).findFirst()
.ifPresent(dataProvider::refreshItem);
}
});
}

private boolean canPush() {
Expand All @@ -561,10 +563,6 @@ private Product refreshProduct(Product product) {
return updatedProduct;
}

private EventBus getEventBus() {
return EventBus.get();
}

private static Logger logger = LoggerFactory.getLogger(BooksView.class);

}

0 comments on commit da8e3ab

Please sign in to comment.