-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import Foundation | ||
import Slipstream | ||
|
||
func groupBlogPostsByYearAndMonth(posts: [BlogPost]) -> [Int: [Int: [BlogPost]]] { | ||
var groupedPosts: [Int: [Int: [BlogPost]]] = [:] | ||
|
||
let calendar = Calendar.current | ||
|
||
for post in posts { | ||
let year = calendar.component(.year, from: post.date) | ||
let month = calendar.component(.month, from: post.date) | ||
|
||
if groupedPosts[year] == nil { | ||
groupedPosts[year] = [:] | ||
} | ||
|
||
if groupedPosts[year]?[month] == nil { | ||
groupedPosts[year]?[month] = [] | ||
} | ||
|
||
groupedPosts[year]?[month]?.append(post) | ||
} | ||
|
||
return groupedPosts | ||
} | ||
|
||
private struct YearHeading: View { | ||
let year: Int | ||
var body: some View { | ||
H2("\(year)") | ||
.fontSize(.extraExtraLarge) | ||
.bold() | ||
.fontDesign("rounded") | ||
} | ||
} | ||
|
||
private struct YearGroup<Content: View>: View { | ||
let isLastYear: Bool | ||
@ViewBuilder let content: () -> Content | ||
|
||
var body: some View { | ||
VStack { | ||
content() | ||
} | ||
.margin(.bottom, 16) | ||
if !isLastYear { | ||
HorizontalRule() | ||
} | ||
} | ||
} | ||
|
||
private struct MonthHeading: View { | ||
let month: String | ||
var body: some View { | ||
H3(month) | ||
.fontSize(.large) | ||
.bold() | ||
.fontDesign("rounded") | ||
} | ||
} | ||
|
||
private struct MonthGroup<Content: View>: View { | ||
let isLastMonth: Bool | ||
@ViewBuilder let content: () -> Content | ||
|
||
var body: some View { | ||
if isLastMonth { | ||
List { | ||
content() | ||
} | ||
} else { | ||
List { | ||
content() | ||
} | ||
.margin(.bottom, 16) | ||
} | ||
} | ||
} | ||
|
||
private struct PostLink: View { | ||
let post: BlogPost | ||
|
||
var body: some View { | ||
ListItem { | ||
Link(post.url) { | ||
if post.draft { | ||
Text("Draft: " + (post.title ?? post.slug)) | ||
.textColor(.palette(.red, darkness: 500)) | ||
} else { | ||
Text(post.title ?? post.slug) | ||
} | ||
} | ||
.textColor(.link, darkness: 700) | ||
.textColor(.link, darkness: 400, condition: .dark) | ||
.fontWeight(600) | ||
.underline(condition: .hover) | ||
} | ||
.listStyle(.disc) | ||
.margin(.left, 22) | ||
} | ||
} | ||
|
||
struct BlogList: View { | ||
let posts: [BlogPost] | ||
let groupedPosts: [Int: [Int: [BlogPost]]] | ||
|
||
init(posts: [BlogPost]) { | ||
self.posts = posts | ||
self.groupedPosts = groupBlogPostsByYearAndMonth(posts: posts.reversed()) | ||
} | ||
|
||
let dateFormatter: DateFormatter = { | ||
let dateFormatter = DateFormatter() | ||
dateFormatter.dateFormat = "MMMM" | ||
return dateFormatter | ||
}() | ||
|
||
var body: some View { | ||
Page( | ||
path: "/blog/", | ||
description: "Articles on software design and Apple platforms." | ||
) { | ||
NarrowContainer { | ||
Div { | ||
let sortedYears = groupedPosts.sorted(by: { $0.key > $1.key }) | ||
for (year, months) in sortedYears { | ||
YearHeading(year: year) | ||
YearGroup(isLastYear: sortedYears.last?.key == year) { | ||
let sortedMonths = months.sorted(by: { $0.key > $1.key }) | ||
for (month, posts) in sortedMonths { | ||
let monthName = dateFormatter.monthSymbols[month - 1] | ||
MonthHeading(month: monthName) | ||
MonthGroup(isLastMonth: sortedMonths.last?.key == month) { | ||
for post in posts { | ||
PostLink(post: post) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.textColor(.white) | ||
} | ||
.padding(.bottom, 16) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// | ||
// 2024-09-01-Shaders.swift | ||
// jeffverkoeyen.com | ||
// | ||
// Created by Jeff Verkoeyen on 9/1/24. | ||
// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters