Skip to content

Commit fda760d

Browse files
committed
Separate posts by year
1 parent 0c6bd6b commit fda760d

4 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/main/java/zone/nox/components/Components.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,4 @@ public static PostContent postContent(Post post) {
2626
return new PostContent(post);
2727
}
2828

29-
private static final LocalDateTime FIRST_DAY_2024 = LocalDate.of(2024, 4, 24).atTime(0, 0);
30-
private static final DateTimeFormatter ABSOLUTE_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd / HH:mm 'NTZ'");
31-
private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("HH:mm 'NTZ'");
32-
33-
public static String format(LocalDateTime dateTime) {
34-
return dateTime.isBefore(FIRST_DAY_2024)
35-
? ABSOLUTE_DATE.format(dateTime)
36-
: "Day " + (dateTime.getDayOfMonth() - FIRST_DAY_2024.getDayOfMonth() + 1) + " / " + TIME.format(dateTime);
37-
}
38-
3929
}

src/main/java/zone/nox/components/PageHeader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import static dev.nipafx.ginevra.html.HtmlElement.div;
1515
import static dev.nipafx.ginevra.html.HtmlElement.h1;
1616
import static dev.nipafx.ginevra.html.HtmlElement.p;
17-
import static zone.nox.components.Components.format;
17+
import static zone.nox.data.Neotropolis.format;
1818

1919
public record PageHeader(String title, String summary, Optional<String> dateLine) implements Component {
2020

src/main/java/zone/nox/components/PostBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import static dev.nipafx.ginevra.html.HtmlElement.a;
1212
import static dev.nipafx.ginevra.html.HtmlElement.div;
1313
import static dev.nipafx.ginevra.html.HtmlElement.span;
14-
import static zone.nox.components.Components.format;
14+
import static zone.nox.data.Neotropolis.format;
1515

1616
public record PostBlock(Post post) implements Component {
1717

src/main/java/zone/nox/templates/LandingPage.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@
1212
import zone.nox.data.Post;
1313

1414
import java.util.List;
15+
import java.util.Map.Entry;
16+
import java.util.stream.Collectors;
17+
import java.util.stream.Stream;
1518

1619
import static dev.nipafx.ginevra.html.HtmlElement.div;
20+
import static dev.nipafx.ginevra.html.HtmlElement.span;
1721
import static java.util.Comparator.comparing;
1822
import static zone.nox.components.Components.layout;
1923
import static zone.nox.components.Components.postBlock;
2024

2125
public class LandingPage implements Template {
2226

23-
public record Style(Classes posts, Classes post, Css css) implements CssStyle { }
27+
public record Style(Classes posts, Classes year, Classes post, Css css) implements CssStyle { }
2428

2529
@StyledWith
2630
public static final Style STYLE = Css.parse(Style.class, """
@@ -32,6 +36,13 @@ public record Style(Classes posts, Classes post, Css css) implements CssStyle {
3236
gap: var(--gap);
3337
}
3438
39+
.year {
40+
font-family: var(--alt-font), "sans-serif";
41+
font-size: var(--font-size-small);
42+
color: var(--yellow);
43+
text-align: center;
44+
}
45+
3546
.post {
3647
width: 100%
3748
}
@@ -43,18 +54,29 @@ public HtmlPage compose(@ForAllIn("posts") List<Post> posts) {
4354
}
4455

4556
private Element composePage(List<Post> posts) {
57+
var postsByYear = posts.stream().collect(Collectors.groupingBy(post -> post.date().getYear()));
4658
return layout
4759
.title("Radio Nox")
4860
.description("News from the Shadows of Neotropolis.")
4961
.content(div
5062
.classes(STYLE.posts)
51-
.children(posts.stream()
52-
.sorted(comparing(Post::index).reversed())
53-
.map(post -> div
54-
.classes(STYLE.post)
55-
.children(postBlock(post)))
63+
.children(postsByYear
64+
.entrySet().stream()
65+
.sorted(Entry.<Integer, List<Post>> comparingByKey().reversed())
66+
.flatMap(entry -> composeYearWithPosts(entry.getKey(), entry.getValue()))
5667
.toList())
5768
);
5869
}
5970

71+
private Stream<Element> composeYearWithPosts(int year, List<Post> posts) {
72+
return Stream.concat(
73+
Stream.of(span.classes(STYLE.year).text("Y" + year)),
74+
posts.stream()
75+
.sorted(comparing(Post::index).reversed())
76+
.map(post -> div
77+
.classes(STYLE.post)
78+
.children(postBlock(post)))
79+
);
80+
}
81+
6082
}

0 commit comments

Comments
 (0)