From 4419a8d7236dfe0cb109d46b92de5fb2c8b2316e Mon Sep 17 00:00:00 2001
From: reusedname <155286845+reusedname@users.noreply.github.com>
Date: Mon, 24 Feb 2025 22:22:34 +0500
Subject: [PATCH] Add online state column. Fix capitalization

---
 .../src/main/java/ctbrec/ui/JavaFxModel.java  | 24 ++++++++++++++
 .../recorded/AbstractRecordedModelsTab.java   |  2 +-
 .../ui/tabs/recorded/RecordLaterTab.java      |  6 +---
 .../ui/tabs/recorded/RecordedModelsTab.java   | 31 ++++++++++++-------
 4 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/client/src/main/java/ctbrec/ui/JavaFxModel.java b/client/src/main/java/ctbrec/ui/JavaFxModel.java
index 02021f35..ccdd4b3d 100644
--- a/client/src/main/java/ctbrec/ui/JavaFxModel.java
+++ b/client/src/main/java/ctbrec/ui/JavaFxModel.java
@@ -31,6 +31,7 @@ public class JavaFxModel implements Model {
     private final transient SimpleIntegerProperty priorityProperty = new SimpleIntegerProperty();
     private final transient SimpleObjectProperty<Instant> lastSeenProperty = new SimpleObjectProperty<>();
     private final transient SimpleObjectProperty<Instant> lastRecordedProperty = new SimpleObjectProperty<>();
+    private final transient SimpleObjectProperty<State> onlineStateProperty = new SimpleObjectProperty<>();
 
     private final Model delegate;
 
@@ -39,6 +40,21 @@ public class JavaFxModel implements Model {
         setPriority(delegate.getPriority());
         setLastSeen(delegate.getLastSeen());
         setLastRecorded(delegate.getLastRecorded());
+        try { // TODO: split online state updates and reading from cache, so that we don't need try-catch
+            onlineStateProperty.setValue(delegate.getOnlineState(true));
+        } catch (Exception e) {}
+    }
+    
+    public void updateFrom(JavaFxModel other) {
+        this.setSuspended(other.isSuspended());
+        this.setForcePriority(other.isForcePriority());
+        this.getOnlineProperty().set(other.getOnlineProperty().get());
+        this.getRecordingProperty().set(other.getRecordingProperty().get());
+        this.lastRecordedProperty().set(other.lastRecordedProperty().get());
+        this.lastSeenProperty().set(other.lastSeenProperty().get());
+        this.setRecordUntil(other.getRecordUntil());
+        this.setRecordUntilSubsequentAction(other.getRecordUntilSubsequentAction());
+        this.onlineStateProperty().set(other.onlineStateProperty().get());
     }
 
     @Override
@@ -142,6 +158,10 @@ public class JavaFxModel implements Model {
     public boolean isOnline(boolean ignoreCache) throws IOException, ExecutionException, InterruptedException {
         return delegate.isOnline(ignoreCache);
     }
+    
+    public void setOnlineStateProperty(State state) throws IOException, ExecutionException {
+        onlineStateProperty.set(state);
+    }
 
     @Override
     public State getOnlineState(boolean failFast) throws IOException, ExecutionException {
@@ -294,6 +314,10 @@ public class JavaFxModel implements Model {
         return lastRecordedProperty;
     }
 
+    public SimpleObjectProperty<State> onlineStateProperty() {
+        return onlineStateProperty;
+    }
+
     @Override
     public void setLastRecorded(Instant timestamp) {
         delegate.setLastRecorded(timestamp);
diff --git a/client/src/main/java/ctbrec/ui/tabs/recorded/AbstractRecordedModelsTab.java b/client/src/main/java/ctbrec/ui/tabs/recorded/AbstractRecordedModelsTab.java
index a08dc226..bfa36c90 100644
--- a/client/src/main/java/ctbrec/ui/tabs/recorded/AbstractRecordedModelsTab.java
+++ b/client/src/main/java/ctbrec/ui/tabs/recorded/AbstractRecordedModelsTab.java
@@ -327,7 +327,7 @@ public abstract class AbstractRecordedModelsTab extends Tab implements TabSelect
     }
 
     protected void addAddedTimestampColumn(int columnIdx) {
-        TableColumn<JavaFxModel, Instant> tc = addTableColumn("addedTimestamp", "added at", columnIdx, 400);
+        TableColumn<JavaFxModel, Instant> tc = addTableColumn("addedTimestamp", "Added at", columnIdx, 400);
         tc.setCellFactory(new DateTimeCellFactory<>(config.getDateTimeFormatter()));
         tc.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().getAddedTimestamp()));
         tc.setPrefWidth(150);
diff --git a/client/src/main/java/ctbrec/ui/tabs/recorded/RecordLaterTab.java b/client/src/main/java/ctbrec/ui/tabs/recorded/RecordLaterTab.java
index c6324e50..1701b358 100644
--- a/client/src/main/java/ctbrec/ui/tabs/recorded/RecordLaterTab.java
+++ b/client/src/main/java/ctbrec/ui/tabs/recorded/RecordLaterTab.java
@@ -106,11 +106,7 @@ public class RecordLaterTab extends AbstractRecordedModelsTab implements TabSele
             } else {
                 // make sure to update the JavaFX online property, so that the table cell is updated
                 JavaFxModel oldModel = observableModels.get(index);
-                oldModel.setSuspended(updatedModel.isSuspended());
-                oldModel.getOnlineProperty().set(updatedModel.getOnlineProperty().get());
-                oldModel.getRecordingProperty().set(updatedModel.getRecordingProperty().get());
-                oldModel.lastRecordedProperty().set(updatedModel.lastRecordedProperty().get());
-                oldModel.lastSeenProperty().set(updatedModel.lastSeenProperty().get());
+                oldModel.updateFrom(updatedModel);
             }
         }
     }
diff --git a/client/src/main/java/ctbrec/ui/tabs/recorded/RecordedModelsTab.java b/client/src/main/java/ctbrec/ui/tabs/recorded/RecordedModelsTab.java
index 5613ee2b..de0595b1 100644
--- a/client/src/main/java/ctbrec/ui/tabs/recorded/RecordedModelsTab.java
+++ b/client/src/main/java/ctbrec/ui/tabs/recorded/RecordedModelsTab.java
@@ -121,7 +121,7 @@ public class RecordedModelsTab extends AbstractRecordedModelsTab implements TabS
         priority.setUserData(idx++);
         columns.add(priority);
         table.getColumns().add(priority);
-        TableColumn<JavaFxModel, Instant> lastSeen = new TableColumn<>("last seen");
+        TableColumn<JavaFxModel, Instant> lastSeen = new TableColumn<>("Last seen");
         lastSeen.setCellValueFactory(cdf -> cdf.getValue().lastSeenProperty());
         lastSeen.setCellFactory(new DateTimeCellFactory<>(config.getDateTimeFormatter()));
         lastSeen.setPrefWidth(150);
@@ -131,7 +131,7 @@ public class RecordedModelsTab extends AbstractRecordedModelsTab implements TabS
         lastSeen.setStyle(STYLE_ALIGN_CENTER);
         columns.add(lastSeen);
         table.getColumns().add(lastSeen);
-        TableColumn<JavaFxModel, Instant> lastRecorded = new TableColumn<>("last recorded");
+        TableColumn<JavaFxModel, Instant> lastRecorded = new TableColumn<>("Last recorded");
         lastRecorded.setCellValueFactory(cdf -> cdf.getValue().lastRecordedProperty());
         lastRecorded.setCellFactory(new DateTimeCellFactory<>(config.getDateTimeFormatter()));
         lastRecorded.setPrefWidth(150);
@@ -140,8 +140,19 @@ public class RecordedModelsTab extends AbstractRecordedModelsTab implements TabS
         lastRecorded.setUserData(idx++);
         lastRecorded.setStyle(STYLE_ALIGN_CENTER);
         columns.add(lastRecorded);
-        table.getColumns().add(lastRecorded);
-
+        table.getColumns().add(lastRecorded);        
+        TableColumn<JavaFxModel, Model.State> onlineState = new TableColumn<>("State");
+        onlineState.setCellValueFactory(cdf -> cdf.getValue().onlineStateProperty());
+        onlineState.setCellFactory(new ClickableCellFactory<>());
+        onlineState.setPrefWidth(150);
+        onlineState.setEditable(false);
+        onlineState.setId("onlineState");
+        onlineState.setUserData(idx++);
+        onlineState.setStyle(STYLE_ALIGN_CENTER);
+        onlineState.setVisible(false);
+        columns.add(onlineState);
+        table.getColumns().add(onlineState);
+        
         addAddedTimestampColumn(idx++);
         addNotesColumn(idx);
 
@@ -261,14 +272,7 @@ public class RecordedModelsTab extends AbstractRecordedModelsTab implements TabS
             } else {
                 // make sure to update the JavaFX online property, so that the table cell is updated
                 JavaFxModel oldModel = observableModels.get(index);
-                oldModel.setSuspended(updatedModel.isSuspended());
-                oldModel.setForcePriority(updatedModel.isForcePriority());
-                oldModel.getOnlineProperty().set(updatedModel.getOnlineProperty().get());
-                oldModel.getRecordingProperty().set(updatedModel.getRecordingProperty().get());
-                oldModel.lastRecordedProperty().set(updatedModel.lastRecordedProperty().get());
-                oldModel.lastSeenProperty().set(updatedModel.lastSeenProperty().get());
-                oldModel.setRecordUntil(updatedModel.getRecordUntil());
-                oldModel.setRecordUntilSubsequentAction(updatedModel.getRecordUntilSubsequentAction());
+                oldModel.updateFrom(updatedModel);
             }
         }
     }
@@ -322,6 +326,9 @@ public class RecordedModelsTab extends AbstractRecordedModelsTab implements TabS
                             for (Model onlineModel : onlineModels) {
                                 if (Objects.equals(onlineModel, fxm)) {
                                     fxm.setOnlineProperty(true);
+                                    try {
+                                        fxm.setOnlineStateProperty(onlineModel.getOnlineState(true));
+                                    } catch (Exception e) {}
                                     break;
                                 }
                             }