Set new post ids

This commit is contained in:
Christoph Hagen 2024-11-24 20:32:07 +01:00
parent 64b6d88a41
commit 5b29ee65fb
3 changed files with 70 additions and 3 deletions

View File

@ -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 = "<group>"; };
E218501E2CEE6DAC0090B18B /* ImagePickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImagePickerView.swift; sourceTree = "<group>"; };
E21850222CF10C840090B18B /* TagSelectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagSelectionView.swift; sourceTree = "<group>"; };
E21850242CF38BCE0090B18B /* TextEntrySheet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextEntrySheet.swift; sourceTree = "<group>"; };
E24252022C5163CF0029FF16 /* Importer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Importer.swift; sourceTree = "<group>"; };
E24252052C51684E0029FF16 /* GenericMetadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenericMetadata.swift; sourceTree = "<group>"; };
E24252072C5168750029FF16 /* GenericMetadata+Localized.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GenericMetadata+Localized.swift"; sourceTree = "<group>"; };
@ -310,6 +312,7 @@
E2A21C2B2CB2BB210060935B /* PostList.swift */,
E2A21C002CB16A820060935B /* PostView.swift */,
E2A21C072CB17B810060935B /* TagView.swift */,
E21850242CF38BCE0090B18B /* TextEntrySheet.swift */,
);
path = Posts;
sourceTree = "<group>";
@ -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 */,

View File

@ -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,

View File

@ -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"))
}