76 lines
1.8 KiB
Swift
76 lines
1.8 KiB
Swift
import SwiftUI
|
|
|
|
|
|
private struct CenteredPost<Content>: View where Content: View {
|
|
|
|
let content: Content
|
|
|
|
init(@ViewBuilder content: () -> Content) {
|
|
self.content = content()
|
|
}
|
|
|
|
var body: some View {
|
|
HorizontalCenter {
|
|
content
|
|
}
|
|
.listRowBackground(PostList.background)
|
|
}
|
|
}
|
|
|
|
struct PostList: View {
|
|
|
|
static let background = Color(r: 2, g: 15, b: 26)
|
|
|
|
@Binding
|
|
var posts: [Post]
|
|
|
|
var body: some View {
|
|
List {
|
|
if posts.isEmpty {
|
|
CenteredPost {
|
|
Text("No posts yet.")
|
|
.padding()
|
|
}
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
CenteredPost {
|
|
Button(action: addNewPost) {
|
|
Text("Add post")
|
|
}
|
|
.padding()
|
|
.listRowSeparator(.hidden)
|
|
}
|
|
ForEach(posts) { post in
|
|
CenteredPost {
|
|
PostView(post: post)
|
|
.frame(maxWidth: 600)
|
|
}
|
|
.listRowSeparator(.hidden)
|
|
.listRowInsets(.init(top: 0, leading: 0, bottom: 30, trailing: 0))
|
|
}
|
|
}
|
|
.listStyle(.plain)
|
|
.background(PostList.background)
|
|
.scrollContentBackground(.hidden)
|
|
}
|
|
|
|
private func addNewPost() {
|
|
let largestId = posts.map { $0.id }.max() ?? 0
|
|
|
|
let post = Post(
|
|
id: largestId + 1,
|
|
isDraft: true,
|
|
startDate: .now,
|
|
endDate: nil,
|
|
title: .init(en: "Title", de: "Titel"),
|
|
text: .init(en: "Text", de: "Text"),
|
|
tags: [],
|
|
images: [])
|
|
posts.insert(post, at: 0)
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
PostList(posts: .constant([.mock, .fullMock]))
|
|
}
|