From 5b29ee65fb969de3a5e9cb4118e1a200a741dd41 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Sun, 24 Nov 2024 20:32:07 +0100 Subject: [PATCH] Set new post ids --- CHDataManagement.xcodeproj/project.pbxproj | 4 ++ CHDataManagement/Views/Posts/PostList.swift | 24 ++++++++-- .../Views/Posts/TextEntrySheet.swift | 45 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 CHDataManagement/Views/Posts/TextEntrySheet.swift diff --git a/CHDataManagement.xcodeproj/project.pbxproj b/CHDataManagement.xcodeproj/project.pbxproj index 07998c0..fc8da88 100644 --- a/CHDataManagement.xcodeproj/project.pbxproj +++ b/CHDataManagement.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ E218501D2CEE6CB60090B18B /* VerticalCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E218501C2CEE6CB30090B18B /* VerticalCenter.swift */; }; E218501F2CEE6DAC0090B18B /* ImagePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E218501E2CEE6DAC0090B18B /* ImagePickerView.swift */; }; E21850232CF10C850090B18B /* TagSelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E21850222CF10C840090B18B /* TagSelectionView.swift */; }; + E21850252CF38BCE0090B18B /* TextEntrySheet.swift in Sources */ = {isa = PBXBuildFile; fileRef = E21850242CF38BCE0090B18B /* TextEntrySheet.swift */; }; E24252012C50E0A40029FF16 /* HighlightedTextEditor in Frameworks */ = {isa = PBXBuildFile; productRef = E24252002C50E0A40029FF16 /* HighlightedTextEditor */; }; E24252032C5163CF0029FF16 /* Importer.swift in Sources */ = {isa = PBXBuildFile; fileRef = E24252022C5163CF0029FF16 /* Importer.swift */; }; E24252062C51684E0029FF16 /* GenericMetadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = E24252052C51684E0029FF16 /* GenericMetadata.swift */; }; @@ -91,6 +92,7 @@ E218501C2CEE6CB30090B18B /* VerticalCenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VerticalCenter.swift; sourceTree = ""; }; E218501E2CEE6DAC0090B18B /* ImagePickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePickerView.swift; sourceTree = ""; }; E21850222CF10C840090B18B /* TagSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagSelectionView.swift; sourceTree = ""; }; + E21850242CF38BCE0090B18B /* TextEntrySheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextEntrySheet.swift; sourceTree = ""; }; E24252022C5163CF0029FF16 /* Importer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Importer.swift; sourceTree = ""; }; E24252052C51684E0029FF16 /* GenericMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericMetadata.swift; sourceTree = ""; }; E24252072C5168750029FF16 /* GenericMetadata+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GenericMetadata+Localized.swift"; sourceTree = ""; }; @@ -310,6 +312,7 @@ E2A21C2B2CB2BB210060935B /* PostList.swift */, E2A21C002CB16A820060935B /* PostView.swift */, E2A21C072CB17B810060935B /* TagView.swift */, + E21850242CF38BCE0090B18B /* TextEntrySheet.swift */, ); path = Posts; sourceTree = ""; @@ -458,6 +461,7 @@ E2A37D192CEA36A90000979F /* LocalizedTag.swift in Sources */, E24252062C51684E0029FF16 /* GenericMetadata.swift in Sources */, E2A21C462CBAE2E60060935B /* FeedEntryContent.swift in Sources */, + E21850252CF38BCE0090B18B /* TextEntrySheet.swift in Sources */, E2A37D1D2CEA922D0000979F /* LocalizedPost.swift in Sources */, E21850172CEE55FC0090B18B /* FileType.swift in Sources */, E2A37D112CE537800000979F /* PageFile.swift in Sources */, diff --git a/CHDataManagement/Views/Posts/PostList.swift b/CHDataManagement/Views/Posts/PostList.swift index c6762f0..4b7f4bb 100644 --- a/CHDataManagement/Views/Posts/PostList.swift +++ b/CHDataManagement/Views/Posts/PostList.swift @@ -5,6 +5,12 @@ struct PostList: View { @EnvironmentObject private var content: Content + @State + private var showNewPostIdSheet = false + + @State + private var newPostId = "" + var body: some View { List { if content.posts.isEmpty { @@ -15,7 +21,7 @@ struct PostList: View { .listRowSeparator(.hidden) } HorizontalCenter { - Button(action: addNewPost) { + Button(action: { showNewPostIdSheet = true }) { Text("Add post") } .padding() @@ -31,12 +37,24 @@ struct PostList: View { } } .listStyle(.plain) - //.scrollContentBackground(.hidden) + .sheet(isPresented: $showNewPostIdSheet, onDismiss: addNewPost) { + TextEntrySheet(title: "Enter the new post id", text: $newPostId) + } } private func addNewPost() { + let id = newPostId.trimmingCharacters(in: .whitespacesAndNewlines) + guard id != "" else { + return + } + + guard !content.posts.contains(where: { $0.id == id }) else { + print("ID \(id) already exists") + return + } + let post = Post( - id: "new", + id: id, isDraft: true, createdDate: .now, startDate: .now, diff --git a/CHDataManagement/Views/Posts/TextEntrySheet.swift b/CHDataManagement/Views/Posts/TextEntrySheet.swift new file mode 100644 index 0000000..10bb674 --- /dev/null +++ b/CHDataManagement/Views/Posts/TextEntrySheet.swift @@ -0,0 +1,45 @@ +import SwiftUI + +struct TextEntrySheet: View { + + let title: String + + @Binding + var text: String + + @Environment(\.dismiss) + var dismiss: DismissAction + + @State + private var enteredText: String = "" + + var body: some View { + VStack { + Text(title) + .foregroundStyle(.secondary) + TextField("Text", text: $enteredText) + HStack { + Button(action: submit) { + Text("Submit") + } + Button(role: .cancel, action: cancel) { + Text("Cancel") + } + } + } + .padding() + } + + private func submit() { + text = enteredText + dismiss() + } + + private func cancel() { + dismiss() + } +} + +#Preview { + TextEntrySheet(title: "Enter the id for the new post", text: .constant("new")) +}