Set new post ids
This commit is contained in:
@ -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,
|
||||
|
45
CHDataManagement/Views/Posts/TextEntrySheet.swift
Normal file
45
CHDataManagement/Views/Posts/TextEntrySheet.swift
Normal 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"))
|
||||
}
|
Reference in New Issue
Block a user