Fix bug displaying the double amount of unread messages

Additionally remove ChangeHandlers in SettingsItem and show
StatusTrayIcon whenever supported
This commit is contained in:
Leon Hofmeister 2020-10-21 22:14:04 +02:00
parent db28f02505
commit 98f59c1383
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
5 changed files with 16 additions and 42 deletions

View File

@ -147,6 +147,7 @@ public final class LocalDB implements EventListener {
throw new IllegalStateException("Client user is null, cannot initialize user storage"); throw new IllegalStateException("Client user is null, cannot initialize user storage");
userFile = new File(dbDir, user.getID() + ".db"); userFile = new File(dbDir, user.getID() + ".db");
try (var in = new ObjectInputStream(new FileInputStream(userFile))) { try (var in = new ObjectInputStream(new FileInputStream(userFile))) {
Chat.getTotalUnreadAmount().set(0);
chats = FXCollections.observableList((List<Chat>) in.readObject()); chats = FXCollections.observableList((List<Chat>) in.readObject());
// Some chats have changed and should not be overwritten by the saved values // Some chats have changed and should not be overwritten by the saved values

View File

@ -1,7 +1,6 @@
package envoy.client.data; package envoy.client.data;
import java.io.Serializable; import java.io.Serializable;
import java.util.function.Consumer;
import javax.swing.JComponent; import javax.swing.JComponent;
@ -17,8 +16,6 @@ public final class SettingsItem<T> implements Serializable {
private T value; private T value;
private String userFriendlyName, description; private String userFriendlyName, description;
private transient Consumer<T> changeHandler;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
@ -52,8 +49,6 @@ public final class SettingsItem<T> implements Serializable {
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public void set(T value) { public void set(T value) {
if (changeHandler != null && value != this.value)
changeHandler.accept(value);
this.value = value; this.value = value;
} }
@ -82,16 +77,4 @@ public final class SettingsItem<T> implements Serializable {
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public void setDescription(String description) { this.description = description; } 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<T> changeHandler) {
this.changeHandler = changeHandler;
changeHandler.accept(value);
}
} }

View File

@ -237,17 +237,9 @@ public final class Startup extends Application {
e.consume(); e.consume();
}); });
if (StatusTrayIcon.isSupported()) { // Initialize status tray icon
if (StatusTrayIcon.isSupported())
// Initialize status tray icon new StatusTrayIcon(stage).show();
final var trayIcon = new StatusTrayIcon(stage);
Settings.getInstance().getItems().get("hideOnClose").setChangeHandler(c -> {
if ((Boolean) c)
trayIcon.show();
else
trayIcon.hide();
});
}
// Start auto save thread // Start auto save thread
localDB.initAutoSave(); localDB.initAutoSave();

View File

@ -16,7 +16,7 @@ import envoy.data.Message;
import envoy.data.User.UserStatus; import envoy.data.User.UserStatus;
import envoy.client.data.*; import envoy.client.data.*;
import envoy.client.event.OwnStatusChange; import envoy.client.event.*;
import envoy.client.helper.ShutdownHelper; import envoy.client.helper.ShutdownHelper;
import envoy.client.util.*; import envoy.client.util.*;
@ -82,10 +82,7 @@ public final class StatusTrayIcon implements EventListener {
// Adding the logout menu item // Adding the logout menu item
final var logoutMenuItem = new MenuItem("Logout"); final var logoutMenuItem = new MenuItem("Logout");
logoutMenuItem.addActionListener(evt -> { logoutMenuItem.addActionListener(evt -> Platform.runLater(UserUtil::logout));
hide();
Platform.runLater(UserUtil::logout);
});
popup.add(logoutMenuItem); popup.add(logoutMenuItem);
// Adding the status change items // Adding the status change items
@ -137,6 +134,7 @@ public final class StatusTrayIcon implements EventListener {
* *
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
@Event(eventType = Logout.class)
public void hide() { public void hide() {
SystemTray.getSystemTray().remove(trayIcon); SystemTray.getSystemTray().remove(trayIcon);
} }

View File

@ -27,15 +27,15 @@ public final class GeneralSettingsPane extends SettingsPane {
final var settingsItems = settings.getItems(); final var settingsItems = settings.getItems();
// Add hide on close if supported // Add hide on close if supported
if (StatusTrayIcon.isSupported()) { final var hideOnCloseCheckbox =
final var hideOnCloseCheckbox = new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("hideOnClose"));
new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("hideOnClose")); final var hideOnCloseTooltip = new Tooltip(StatusTrayIcon.isSupported()
final var hideOnCloseTooltip = new Tooltip( ? "If selected, Envoy will still be present in the task bar when closed."
"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); hideOnCloseTooltip.setWrapText(true);
hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip); hideOnCloseCheckbox.setTooltip(hideOnCloseTooltip);
getChildren().add(hideOnCloseCheckbox); hideOnCloseCheckbox.setDisable(!StatusTrayIcon.isSupported());
} getChildren().add(hideOnCloseCheckbox);
final var enterToSendCheckbox = final var enterToSendCheckbox =
new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("enterToSend")); new SettingsCheckbox((SettingsItem<Boolean>) settingsItems.get("enterToSend"));