Page MenuHomeMusing Studio

No OneTemporary

diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift
index 50cfe34..34c34c0 100644
--- a/Shared/Navigation/ContentView.swift
+++ b/Shared/Navigation/ContentView.swift
@@ -1,27 +1,27 @@
import SwiftUI
struct ContentView: View {
@ObservedObject var postStore: PostStore
var body: some View {
NavigationView {
PostList(postStore: postStore)
.frame(maxHeight: .infinity)
.navigationTitle("Posts")
.toolbar {
- NavigationLink(destination: PostEditor(post: Post())) {
+ NavigationLink(destination: PostEditor(post: Post()).environmentObject(self.postStore)) {
Image(systemName: "plus")
}
}
Text("Select a post, or create a new draft.")
.foregroundColor(.secondary)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(postStore: testPostStore)
}
}
diff --git a/Shared/Post/PostCell.swift b/Shared/Post/PostCell.swift
index 298d5c9..86ea0b3 100644
--- a/Shared/Post/PostCell.swift
+++ b/Shared/Post/PostCell.swift
@@ -1,39 +1,40 @@
import SwiftUI
struct PostCell: View {
+ @EnvironmentObject var postStore: PostStore
@ObservedObject var post: Post
var body: some View {
NavigationLink(
- destination: PostEditor(post: post)
+ destination: PostEditor(post: post).environmentObject(self.postStore)
) {
HStack {
VStack(alignment: .leading) {
Text(post.title)
.font(.headline)
.lineLimit(1)
Text(buildDateString(from: post.createdDate))
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(1)
}
Spacer()
PostStatusBadge(post: post)
}
}
}
func buildDateString(from date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .short
return dateFormatter.string(from: date)
}
}
struct PostCell_Previews: PreviewProvider {
static var previews: some View {
PostCell(post: testPost)
}
}
diff --git a/Shared/Post/PostEditor.swift b/Shared/Post/PostEditor.swift
index 61a7236..2908941 100644
--- a/Shared/Post/PostEditor.swift
+++ b/Shared/Post/PostEditor.swift
@@ -1,35 +1,53 @@
import SwiftUI
struct PostEditor: View {
+ @EnvironmentObject var postStore: PostStore
@ObservedObject var post: Post
+ @State private var isNewPost = false
var body: some View {
VStack {
TextEditor(text: $post.title)
.font(.title)
.frame(height: 100)
.onChange(of: post.title) { _ in
if post.status == .published {
post.status = .edited
}
}
TextEditor(text: $post.body)
.font(.body)
.onChange(of: post.body) { _ in
if post.status == .published {
post.status = .edited
}
}
}
.padding()
.toolbar {
PostStatusBadge(post: post)
}
+ .onAppear(perform: checkIfNewPost)
+ .onDisappear(perform: addPostToStore)
+ }
+
+ private func checkIfNewPost() {
+ if !postStore.posts.contains(where: { $0.id == post.id }) {
+ self.isNewPost = true
+ }
+ }
+
+ private func addPostToStore() {
+ if isNewPost {
+ withAnimation {
+ postStore.add(post)
+ }
+ }
}
}
struct PostEditor_Previews: PreviewProvider {
static var previews: some View {
PostEditor(post: testPost)
}
}
diff --git a/Shared/Post/PostList.swift b/Shared/Post/PostList.swift
index 03d2b1c..07bf01c 100644
--- a/Shared/Post/PostList.swift
+++ b/Shared/Post/PostList.swift
@@ -1,21 +1,21 @@
import SwiftUI
struct PostList: View {
@ObservedObject var postStore: PostStore
var body: some View {
List {
Text("\(postStore.posts.count) Posts")
.foregroundColor(.secondary)
ForEach(postStore.posts) { post in
- PostCell(post: post)
+ PostCell(post: post).environmentObject(self.postStore)
}
}
}
}
struct PostList_Previews: PreviewProvider {
static var previews: some View {
PostList(postStore: testPostStore)
}
}
diff --git a/Shared/Post/PostStore.swift b/Shared/Post/PostStore.swift
index 03916ba..2e7aad9 100644
--- a/Shared/Post/PostStore.swift
+++ b/Shared/Post/PostStore.swift
@@ -1,11 +1,15 @@
import Foundation
class PostStore: ObservableObject {
@Published var posts: [Post]
init(posts: [Post] = []) {
self.posts = posts
}
+
+ func add(_ post: Post) {
+ posts.append(post)
+ }
}
let testPostStore = PostStore(posts: testPostData)

File Metadata

Mime Type
text/x-diff
Expires
Sat, May 17, 8:32 AM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3241223

Event Timeline