Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10669430
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Subscribers
None
View Options
diff --git a/Shared/Post/PostCell.swift b/Shared/Post/PostCell.swift
index 36d17d0..706ba04 100644
--- a/Shared/Post/PostCell.swift
+++ b/Shared/Post/PostCell.swift
@@ -1,36 +1,37 @@
import SwiftUI
struct PostCell: View {
@EnvironmentObject var postStore: PostStore
+
@ObservedObject var post: Post
var body: some View {
HStack {
VStack(alignment: .leading) {
Text(post.title)
.font(.headline)
.lineLimit(1)
Text(buildDateString(from: post.createdDate))
.font(.caption)
.lineLimit(1)
}
Spacer()
PostStatusBadge(post: post)
}
.padding(5)
}
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 57fe7fb..1641f84 100644
--- a/Shared/Post/PostEditor.swift
+++ b/Shared/Post/PostEditor.swift
@@ -1,71 +1,73 @@
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 {
ToolbarItem(placement: .status) {
PostStatusBadge(post: post)
}
ToolbarItem(placement: .primaryAction) {
Button(action: {
post.status = .published
}, label: {
Image(systemName: "paperplane")
})
}
}
.onAppear(perform: {
checkIfNewPost()
if self.isNewPost {
addNewPostToStore()
}
})
}
private func checkIfNewPost() {
self.isNewPost = !postStore.posts.contains(where: { $0.id == post.id })
}
private func addNewPostToStore() {
withAnimation {
postStore.add(post)
self.isNewPost = false
}
}
}
struct PostEditor_NewDraftPreviews: PreviewProvider {
static var previews: some View {
PostEditor(post: Post())
.environmentObject(testPostStore)
}
}
struct PostEditor_ExistingPostPreviews: PreviewProvider {
static var previews: some View {
PostEditor(post: testPostData[0])
.environmentObject(testPostStore)
}
}
diff --git a/Shared/Post/PostList.swift b/Shared/Post/PostList.swift
index ab4102c..75fae23 100644
--- a/Shared/Post/PostList.swift
+++ b/Shared/Post/PostList.swift
@@ -1,107 +1,108 @@
import SwiftUI
struct PostList: View {
@EnvironmentObject var postStore: PostStore
+
@State var selectedCollection: PostCollection
@State var isPresentingSettings = false
var body: some View {
#if os(iOS)
GeometryReader { geometry in
List {
ForEach(showPosts(for: selectedCollection)) { post in
NavigationLink(
destination: PostEditor(post: post)
) {
PostCell(
post: post
)
}
}
}
.navigationTitle(selectedCollection.title)
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
let post = Post()
postStore.add(post)
}, label: {
Image(systemName: "square.and.pencil")
})
}
ToolbarItem(placement: .bottomBar) {
HStack {
Button(action: {
isPresentingSettings = true
}, label: {
Image(systemName: "gear")
}).sheet(
isPresented: $isPresentingSettings,
onDismiss: {
isPresentingSettings = false
},
content: {
SettingsView(isPresented: $isPresentingSettings)
}
)
Spacer()
Text(pluralizedPostCount(for: showPosts(for: selectedCollection)))
.foregroundColor(.secondary)
}
.padding()
.frame(width: geometry.size.width)
}
}
}
#else //if os(macOS)
List {
ForEach(showPosts(for: selectedCollection)) { post in
NavigationLink(
destination: PostEditor(post: post)
) {
PostCell(
post: post
)
}
}
}
.navigationTitle(selectedCollection.title)
.navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection)))
.toolbar {
Button(action: {
let post = Post()
postStore.add(post)
}, label: {
Image(systemName: "square.and.pencil")
})
}
#endif
}
private func pluralizedPostCount(for posts: [Post]) -> String {
if posts.count == 1 {
return "1 post"
} else {
return "\(posts.count) posts"
}
}
private func showPosts(for collection: PostCollection) -> [Post] {
if collection == allPostsCollection {
return postStore.posts
} else {
return postStore.posts.filter {
$0.collection.title == collection.title
}
}
}
}
struct PostList_Previews: PreviewProvider {
static var previews: some View {
Group {
PostList(selectedCollection: allPostsCollection)
.environmentObject(testPostStore)
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, May 16, 7:42 AM (1 d, 12 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3240012
Attached To
rWFSUI WriteFreely SwiftUI
Event Timeline
Log In to Comment