From 98f59c1383f95e266fd9f0f86d5dbee920ec599b Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 21 Oct 2020 22:14:04 +0200 Subject: [PATCH] Fix bug displaying the double amount of unread messages Additionally remove ChangeHandlers in SettingsItem and show StatusTrayIcon whenever supported --- .../main/java/envoy/client/data/LocalDB.java | 1 + .../java/envoy/client/data/SettingsItem.java | 17 ----------------- .../src/main/java/envoy/client/ui/Startup.java | 14 +++----------- .../java/envoy/client/ui/StatusTrayIcon.java | 8 +++----- .../ui/settings/GeneralSettingsPane.java | 18 +++++++++--------- 5 files changed, 16 insertions(+), 42 deletions(-) diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index e3c4ac7..2ae3570 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -147,6 +147,7 @@ public final class LocalDB implements EventListener { throw new IllegalStateException("Client user is null, cannot initialize user storage"); userFile = new File(dbDir, user.getID() + ".db"); try (var in = new ObjectInputStream(new FileInputStream(userFile))) { + Chat.getTotalUnreadAmount().set(0); chats = FXCollections.observableList((List) in.readObject()); // Some chats have changed and should not be overwritten by the saved values diff --git a/client/src/main/java/envoy/client/data/SettingsItem.java b/client/src/main/java/envoy/client/data/SettingsItem.java index 590f965..b2c9199 100644 --- a/client/src/main/java/envoy/client/data/SettingsItem.java +++ b/client/src/main/java/envoy/client/data/SettingsItem.java @@ -1,7 +1,6 @@ package envoy.client.data; import java.io.Serializable; -import java.util.function.Consumer; import javax.swing.JComponent; @@ -17,8 +16,6 @@ public final class SettingsItem implements Serializable { private T value; private String userFriendlyName, description; - private transient Consumer changeHandler; - private static final long serialVersionUID = 1L; /** @@ -52,8 +49,6 @@ public final class SettingsItem implements Serializable { * @since Envoy Client v0.3-alpha */ public void set(T value) { - if (changeHandler != null && value != this.value) - changeHandler.accept(value); this.value = value; } @@ -82,16 +77,4 @@ public final class SettingsItem implements Serializable { * @since Envoy Client v0.3-alpha */ public void setDescription(String description) { this.description = description; } - - /** - * Sets a {@code ChangeHandler} for this {@link SettingsItem}. It will be invoked with the - * current value once during the registration and every time when the value changes. - * - * @param changeHandler the changeHandler to set - * @since Envoy Client v0.3-alpha - */ - public void setChangeHandler(Consumer changeHandler) { - this.changeHandler = changeHandler; - changeHandler.accept(value); - } } diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index 76fa54b..18468e3 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -237,17 +237,9 @@ public final class Startup extends Application { e.consume(); }); - if (StatusTrayIcon.isSupported()) { - - // Initialize status tray icon - final var trayIcon = new StatusTrayIcon(stage); - Settings.getInstance().getItems().get("hideOnClose").setChangeHandler(c -> { - if ((Boolean) c) - trayIcon.show(); - else - trayIcon.hide(); - }); - } + // Initialize status tray icon + if (StatusTrayIcon.isSupported()) + new StatusTrayIcon(stage).show(); // Start auto save thread localDB.initAutoSave(); diff --git a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java index 7fac539..46a03b0 100644 --- a/client/src/main/java/envoy/client/ui/StatusTrayIcon.java +++ b/client/src/main/java/envoy/client/ui/StatusTrayIcon.java @@ -16,7 +16,7 @@ import envoy.data.Message; import envoy.data.User.UserStatus; import envoy.client.data.*; -import envoy.client.event.OwnStatusChange; +import envoy.client.event.*; import envoy.client.helper.ShutdownHelper; import envoy.client.util.*; @@ -82,10 +82,7 @@ public final class StatusTrayIcon implements EventListener { // Adding the logout menu item final var logoutMenuItem = new MenuItem("Logout"); - logoutMenuItem.addActionListener(evt -> { - hide(); - Platform.runLater(UserUtil::logout); - }); + logoutMenuItem.addActionListener(evt -> Platform.runLater(UserUtil::logout)); popup.add(logoutMenuItem); // Adding the status change items @@ -137,6 +134,7 @@ public final class StatusTrayIcon implements EventListener { * * @since Envoy Client v0.2-beta */ + @Event(eventType = Logout.class) public void hide() { SystemTray.getSystemTray().remove(trayIcon); } diff --git a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java index 3f773fc..16b6f01 100644 --- a/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java +++ b/client/src/main/java/envoy/client/ui/settings/GeneralSettingsPane.java @@ -27,15 +27,15 @@ public final class GeneralSettingsPane extends SettingsPane { final var settingsItems = settings.getItems(); // Add hide on close if supported - if (StatusTrayIcon.isSupported()) { - final var hideOnCloseCheckbox = - new SettingsCheckbox((SettingsItem) settingsItems.get("hideOnClose")); - final var hideOnCloseTooltip = new Tooltip( - "If selected, Envoy will still be present in the task bar when closed."); - hideOnCloseTooltip.setWrapText(true); - hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); - getChildren().add(hideOnCloseCheckbox); - } + final var hideOnCloseCheckbox = + new SettingsCheckbox((SettingsItem) settingsItems.get("hideOnClose")); + final var hideOnCloseTooltip = new Tooltip(StatusTrayIcon.isSupported() + ? "If selected, Envoy will still be present in the task bar when closed." + : "status tray icon is not supported on your system."); + hideOnCloseTooltip.setWrapText(true); + hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); + hideOnCloseCheckbox.setDisable(!StatusTrayIcon.isSupported()); + getChildren().add(hideOnCloseCheckbox); final var enterToSendCheckbox = new SettingsCheckbox((SettingsItem) settingsItems.get("enterToSend"));