Skip to content

Commit

Permalink
Improve date usage
Browse files Browse the repository at this point in the history
There's now a utility class to store all those formatters.
  • Loading branch information
zapek committed Oct 6, 2024
1 parent 6ae36a9 commit 60a641a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,19 @@
import javafx.scene.layout.StackPane;
import net.rgielen.fxweaver.core.FxmlView;
import org.kordamp.ikonli.javafx.FontIcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

import static io.xeres.common.rest.PathConfig.IDENTITIES_PATH;
import static io.xeres.ui.support.util.DateUtils.DATE_TIME_DISPLAY;

@Component
@FxmlView(value = "/view/contact/contactview.fxml")
public class ContactViewController implements Controller
{
private static final Logger log = LoggerFactory.getLogger(ContactViewController.class);

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm") // XXX: put that in some utility class. has to be used everywhere
.withZone(ZoneId.systemDefault());

@FXML
private TableView<Contact> contactTableView;

Expand Down Expand Up @@ -224,7 +216,7 @@ private String getLastConnection(Location location)
}
else
{
return DATE_TIME_FORMATTER.format(lastConnected);
return DATE_TIME_DISPLAY.format(lastConnected);
}
}
}
Expand All @@ -245,7 +237,6 @@ private void changeSelectedContact(Contact contact)
return;
}

log.debug("Set contact to {}", contact);
detailsView.setVisible(true);
nameLabel.setText(contact.name());
if (contact.profileId() != 0L)
Expand All @@ -271,7 +262,7 @@ else if (contact.identityId() != 0L)
identityClient.findById(contact.identityId())
.doOnSuccess(identity -> Platform.runLater(() -> {
idLabel.setText(Id.toString(identity.getGxsId()));
updatedLabel.setText(DATE_TIME_FORMATTER.format(identity.getUpdated()));
updatedLabel.setText(DATE_TIME_DISPLAY.format(identity.getUpdated()));
}))
.subscribe();

Expand Down
9 changes: 3 additions & 6 deletions ui/src/main/java/io/xeres/ui/controller/forum/DateCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@
import javafx.scene.control.TreeTableCell;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

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

public class DateCell extends TreeTableCell<ForumMessage, Instant>
{
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
.withZone(ZoneId.systemDefault());

public DateCell()
{
super();
Expand All @@ -46,7 +43,7 @@ protected void updateItem(Instant item, boolean empty)
}
else
{
setText(formatter.format(item));
setText(DATE_TIME_DISPLAY.format(item));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@
import reactor.core.Disposable;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Stream;

import static io.xeres.ui.support.util.DateUtils.DATE_TIME_PRECISE_DISPLAY;
import static javafx.scene.control.Alert.AlertType.WARNING;
import static javafx.scene.control.TreeTableColumn.SortType.DESCENDING;

Expand All @@ -82,9 +81,6 @@ public class ForumViewController implements Controller
private static final String UNSUBSCRIBE_MENU_ID = "unsubscribe";
private static final String COPY_LINK_MENU_ID = "copyLink";

private static final DateTimeFormatter messageDateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());

@FXML
private TreeView<ForumGroup> forumTree;

Expand Down Expand Up @@ -543,7 +539,7 @@ private void changeSelectedForumMessage(ForumMessage forumMessage)
messageContent.getChildren().addAll(contents.stream()
.map(Content::getNode).toList());
messageAuthor.setText(forumMessage.getAuthorName());
messageDate.setText(messageDateFormatter.format(forumMessage.getPublished()));
messageDate.setText(DATE_TIME_PRECISE_DISPLAY.format(forumMessage.getPublished()));
messageSubject.setText(forumMessage.getName());
messageHeader.setVisible(true);
forumClient.updateForumMessagesRead(Map.of(message.getId(), true))
Expand Down
12 changes: 3 additions & 9 deletions ui/src/main/java/io/xeres/ui/custom/ChatListCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@
import javafx.scene.text.TextFlow;
import org.fxmisc.flowless.Cell;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.List;
import java.util.Locale;

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

public class ChatListCell implements Cell<ChatLine, TextFlow>
{
private static final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(Locale.ROOT)
.withZone(ZoneId.systemDefault());

private static final PseudoClass passivePseudoClass = PseudoClass.getPseudoClass("passive");

private static final List<String> allColors = ColorGenerator.getAllColors();
Expand Down Expand Up @@ -90,7 +84,7 @@ public void updateItem(ChatLine line)
{
isRich = line.isRich();

time.setText(formatter.format(line.getInstant()));
time.setText(TIME_DISPLAY.format(line.getInstant()));

action.setText(line.getAction());
action.getStyleClass().removeAll(allColors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

import static io.xeres.ui.support.util.DateUtils.DATE_TIME_FILENAME;
import static io.xeres.ui.support.util.UiUtils.getWindow;
import static javafx.scene.control.Alert.AlertType.ERROR;

public class ContentImage implements Content
{
private static final DateTimeFormatter SAVE_IMAGE_FILE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmmss")
.withZone(ZoneId.systemDefault());

private static final ContextMenu contextMenu;

static
Expand Down Expand Up @@ -116,7 +112,7 @@ private static void saveAs(ActionEvent event)
fileChooser.setTitle(I18nUtils.getString("file-requester.save-image-title"));
fileChooser.setInitialDirectory(new File(AppDirsFactory.getInstance().getUserDownloadsDir(null, null, null)));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter(saveFormat.format(), saveFormat.extensions()));
fileChooser.setInitialFileName("Image_" + SAVE_IMAGE_FILE_FORMATTER.format(Instant.now()) + saveFormat.getPrimaryExtension());
fileChooser.setInitialFileName("Image_" + DATE_TIME_FILENAME.format(Instant.now()) + saveFormat.getPrimaryExtension());

var selectedFile = fileChooser.showSaveDialog(getWindow(event));
if (selectedFile != null)
Expand Down
46 changes: 46 additions & 0 deletions ui/src/main/java/io/xeres/ui/support/util/DateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.support.util;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public final class DateUtils
{
public static final DateTimeFormatter DATE_TIME_DISPLAY = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
.withZone(ZoneId.systemDefault());

public static final DateTimeFormatter DATE_TIME_PRECISE_DISPLAY = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());

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

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

private DateUtils()
{
throw new UnsupportedOperationException("Utility class");
}
}

0 comments on commit 60a641a

Please sign in to comment.