From 66bf3aa53659d48fc611d40d0cbf1bf1cd013397 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 12 Jan 2025 21:22:54 +0100 Subject: [PATCH] Sort posts, improve list update --- .../Views/Posts/PostListView.swift | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/CHDataManagement/Views/Posts/PostListView.swift b/CHDataManagement/Views/Posts/PostListView.swift index 720840c..ca0732a 100644 --- a/CHDataManagement/Views/Posts/PostListView.swift +++ b/CHDataManagement/Views/Posts/PostListView.swift @@ -1,5 +1,45 @@ import SwiftUI +private struct PostListItem: View { + + @Environment(\.language) + private var language + + @ObservedObject + var post: Post + + var body: some View { + HStack { + LocalizedPostListItem(id: post.id, post: post.localized(in: language)) + if post.isDraft { + TextIndicator(text: "Draft", background: .yellow) + } else { + if post.german.text.isEmpty { + TextIndicator(text: "DE", background: .yellow) + } + if post.english.text.isEmpty { + TextIndicator(text: "EN", background: .yellow) + } + } + } + } +} + +private struct LocalizedPostListItem: View { + + let id: String + + @ObservedObject + var post: LocalizedPost + + var body: some View { + HStack { + Text(post.title ?? id) + Spacer() + } + } +} + struct PostListView: View { @Environment(\.language) @@ -25,19 +65,18 @@ struct PostListView: View { return content.posts.filter { $0.contains(searchString) } } + private var filteredAndSortedPosts: [Post] { + filteredPosts.sorted(ascending: false) { $0.startDate } + } + var body: some View { VStack { TextField("", text: $searchString, prompt: Text("Search")) .textFieldStyle(.roundedBorder) .padding(.horizontal, 8) - List(filteredPosts, selection: $selectedPost) { post in - HStack { - Text(post.title(in: language)) - Spacer() - if post.isDraft { - TextIndicator(text: "Draft") - } - }.tag(post) + List(filteredAndSortedPosts, selection: $selectedPost) { post in + PostListItem(post: post) + .tag(post) } }.onAppear { if selectedPost == nil {