Skip to content

Commit

Permalink
Improve trends view
Browse files Browse the repository at this point in the history
  • Loading branch information
zapek committed Nov 17, 2024
1 parent 6aba6dd commit 988b03f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import io.xeres.ui.controller.TabActivation;
import io.xeres.ui.support.util.UiUtils;
import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
Expand All @@ -35,26 +38,36 @@
import reactor.core.Disposable;

import java.io.IOException;
import java.time.Instant;
import java.util.LinkedList;

@Component
@FxmlView(value = "/view/file/trend.fxml")
public class FileTrendViewController implements Controller, TabActivation
{
private static final String NAME_CONTAINS_ALL = "NAME CONTAINS ALL ";
private static final int MAXIMUM_BACKLOG = 300;


private final NotificationClient notificationClient;
private Disposable notificationDisposable;

private final ObservableList<TrendResult> trendResult = FXCollections.observableList(new LinkedList<>());

@FXML
private TableView<TrendResult> trendTableView;

// @FXML
// private TableColumn<TrendResult, Integer> tableHits;
//
@FXML
private TableColumn<TrendResult, String> tableFrom;
// XXX: make sure the table is NOT sortable!!

@FXML
private TableColumn<TrendResult, String> tableFrom;

@FXML
private TableColumn<TrendResult, String> tableTerms;

@FXML
private TableColumn<TrendResult, Instant> tableTime;

public FileTrendViewController(NotificationClient notificationClient)
{
this.notificationClient = notificationClient;
Expand All @@ -63,8 +76,12 @@ public FileTrendViewController(NotificationClient notificationClient)
@Override
public void initialize() throws IOException
{
trendTableView.setItems(trendResult);

tableTerms.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().keywords()));
tableFrom.setCellValueFactory(param -> new SimpleStringProperty(param.getValue().senderName()));
tableTime.setCellFactory(param -> new TimeCell());
tableTime.setCellValueFactory(param -> new SimpleObjectProperty<>(param.getValue().when()));

setupFileTrendNotifications();
}
Expand All @@ -75,10 +92,17 @@ private void setupFileTrendNotifications()
.doOnError(UiUtils::showAlertError)
.doOnNext(sse -> Platform.runLater(() -> {
assert sse.data() != null;
trendTableView.getItems().add(new TrendResult(sse.data().keywords(), sse.data().senderName()));
if (trendTableView.getItems().size() > 255) // XXX: maybe not optimal...
var keywords = sse.data().keywords();

if (keywords.startsWith(NAME_CONTAINS_ALL))
{
keywords = keywords.substring(NAME_CONTAINS_ALL.length());
}

trendResult.addFirst(new TrendResult(keywords, sse.data().senderName(), Instant.now()));
if (trendTableView.getItems().size() > MAXIMUM_BACKLOG)
{
trendTableView.getItems().remove(0, 10);
trendTableView.getItems().removeLast();
}
}))
.subscribe();
Expand Down
43 changes: 43 additions & 0 deletions ui/src/main/java/io/xeres/ui/controller/file/TimeCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 by David Gerber - https://zapek.com
*
* This file is part of Xeres.
*
* Xeres is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Xeres 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Xeres. If not, see <http://www.gnu.org/licenses/>.
*/

package io.xeres.ui.controller.file;

import javafx.scene.control.TableCell;

import java.time.Instant;

import static io.xeres.ui.support.util.DateUtils.TIME_DISPLAY_WITH_SECONDS;

class TimeCell extends TableCell<TrendResult, Instant>
{
@Override
protected void updateItem(Instant item, boolean empty)
{
super.updateItem(item, empty);
if (empty)
{
setText(null);
}
else
{
setText(TIME_DISPLAY_WITH_SECONDS.format(item));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package io.xeres.ui.controller.file;

public record TrendResult(String keywords, String senderName)
import java.time.Instant;

record TrendResult(String keywords, String senderName, Instant when)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import static io.xeres.ui.support.util.DateUtils.DATE_TIME_DISPLAY;

public class DateCell extends TreeTableCell<ForumMessage, Instant>
class DateCell extends TreeTableCell<ForumMessage, Instant>
{
public DateCell()
{
Expand Down
4 changes: 4 additions & 0 deletions ui/src/main/java/io/xeres/ui/support/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public final class DateUtils
.withLocale(Locale.ROOT)
.withZone(ZoneId.systemDefault());

public static final DateTimeFormatter TIME_DISPLAY_WITH_SECONDS = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)
.withLocale(Locale.ROOT)
.withZone(ZoneId.systemDefault());

public static final DateTimeFormatter DATE_TIME_FILENAME = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss")
.withZone(ZoneId.systemDefault());

Expand Down
6 changes: 3 additions & 3 deletions ui/src/main/resources/view/file/trend.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<Label text="No trends yet"/>
</placeholder>
<columns>
<!--<TableColumn fx:id="tableHits" prefWidth="60.0" text="Hits"/>-->
<TableColumn fx:id="tableTerms" minWidth="100.0" prefWidth="180.0" text="Terms"/>
<TableColumn fx:id="tableFrom" prefWidth="120.0" text="From"/>
<TableColumn fx:id="tableTerms" minWidth="100.0" prefWidth="180.0" text="Terms" sortable="false"/>
<TableColumn fx:id="tableFrom" prefWidth="120.0" text="From" sortable="false"/>
<TableColumn fx:id="tableTime" prefWidth="100.0" text="Time" sortable="false"/>
</columns>
</TableView>
</VBox>

0 comments on commit 988b03f

Please sign in to comment.