Add button to remove post

This commit is contained in:
Christoph Hagen
2025-05-04 11:48:09 +02:00
parent d6502fb09c
commit 329519e15b
6 changed files with 56 additions and 0 deletions

View File

@ -16,4 +16,10 @@ final class SelectedContent: ObservableObject {
@Published
var file: FileResource?
func remove(_ post: Post) {
if self.post == post {
self.post = nil
}
}
}

View File

@ -113,6 +113,8 @@ final class Content: ObservableObject {
loadFromDisk(callback: callback)
}
// MARK: Removing items
func remove(_ file: FileResource) {
files.remove(file)
for post in posts {
@ -129,6 +131,10 @@ final class Content: ObservableObject {
settings.remove(file)
}
func remove(_ post: Post) {
posts.remove(post)
}
func file(withOutputPath: String) -> FileResource? {
files.first { $0.absoluteUrl == withOutputPath }
}

View File

@ -186,6 +186,16 @@ final class Storage: ObservableObject {
return contentScope.move(postFilePath(post: postId), to: postFilePath(post: newId))
}
/**
Completely delete a post file from the content folder
*/
func delete(post postId: String) -> Bool {
guard let contentScope else {
return false
}
return contentScope.deleteFile(at: postFilePath(post: postId))
}
// MARK: Tags
private func tagFileName(tagId: String) -> String {

View File

@ -0,0 +1,20 @@
import SwiftUI
struct DeleteButton: View {
let action: () -> Void
var body: some View {
Button(action: action) {
HStack {
Spacer()
Image(systemSymbol: .trash)
Text("Delete")
.padding(.vertical, 8)
Spacer()
}
.foregroundStyle(Color.white)
.background(RoundedRectangle(cornerRadius: 8).fill(Color.red))
}.buttonStyle(.plain)
}
}

View File

@ -76,6 +76,7 @@ struct PostDetailView: View {
LocalizedPostDetailView(
post: post.localized(in: language),
transferImage: transferImage)
DeleteButton(action: deletePost)
}
.padding()
}
@ -90,6 +91,15 @@ struct PostDetailView: View {
selection.tab = .pages
}
}
private func deletePost() {
guard content.storage.delete(post: post.id) else {
print("Post '\(post.id)': Failed to delete file in content folder")
return
}
content.remove(post)
selection.remove(post)
}
}
extension PostDetailView: MainContentView {