Merge pull request #2 from writeas/refactor-post-ui

Refactor Post-related UI code
This commit is contained in:
Angelo Stavrow 2020-08-07 17:45:51 -04:00 committed by GitHub
commit bc289fba8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 47 additions and 22 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Post editor now has a Publish button to change post status
### Changed
- Updated license from AGPLv3 to GPLv3

View File

@ -5,11 +5,11 @@ struct ContentView: View {
var body: some View {
NavigationView {
PostList(postStore: postStore)
PostList()
.frame(maxHeight: .infinity)
.navigationTitle("Posts")
.toolbar {
NavigationLink(destination: PostEditor(post: Post()).environmentObject(self.postStore)) {
NavigationLink(destination: PostEditor(post: Post())) {
Image(systemName: "plus")
}
}
@ -17,6 +17,7 @@ struct ContentView: View {
Text("Select a post, or create a new draft.")
.foregroundColor(.secondary)
}
.environmentObject(postStore)
}
}

View File

@ -6,7 +6,7 @@ struct PostCell: View {
var body: some View {
NavigationLink(
destination: PostEditor(post: post).environmentObject(self.postStore)
destination: PostEditor(post: post)
) {
HStack {
VStack(alignment: .leading) {

View File

@ -25,29 +25,47 @@ struct PostEditor: View {
}
.padding()
.toolbar {
PostStatusBadge(post: post)
ToolbarItem(placement: .status) {
PostStatusBadge(post: post)
}
ToolbarItem(placement: .primaryAction) {
Button(action: {
post.status = .published
}, label: {
Image(systemName: "paperplane")
})
}
}
.onAppear(perform: checkIfNewPost)
.onDisappear(perform: addPostToStore)
.onAppear(perform: {
checkIfNewPost()
if self.isNewPost {
addNewPostToStore()
}
})
}
private func checkIfNewPost() {
if !postStore.posts.contains(where: { $0.id == post.id }) {
self.isNewPost = true
}
self.isNewPost = !postStore.posts.contains(where: { $0.id == post.id })
}
private func addPostToStore() {
if isNewPost {
withAnimation {
postStore.add(post)
}
private func addNewPostToStore() {
withAnimation {
postStore.add(post)
self.isNewPost = false
}
}
}
struct PostEditor_Previews: PreviewProvider {
struct PostEditor_NewDraftPreviews: PreviewProvider {
static var previews: some View {
PostEditor(post: testPost)
PostEditor(post: Post())
.environmentObject(testPostStore)
}
}
struct PostEditor_ExistingPostPreviews: PreviewProvider {
static var previews: some View {
PostEditor(post: testPostData[0])
.environmentObject(testPostStore)
}
}

View File

@ -1,14 +1,14 @@
import SwiftUI
struct PostList: View {
@ObservedObject var postStore: PostStore
@EnvironmentObject var postStore: PostStore
var body: some View {
List {
Text("\(postStore.posts.count) Posts")
.foregroundColor(.secondary)
ForEach(postStore.posts) { post in
PostCell(post: post).environmentObject(self.postStore)
PostCell(post: post)
}
}
}
@ -16,6 +16,7 @@ struct PostList: View {
struct PostList_Previews: PreviewProvider {
static var previews: some View {
PostList(postStore: testPostStore)
PostList()
.environmentObject(testPostStore)
}
}

View File

@ -3,6 +3,7 @@ import SwiftUI
@main
struct WriteFreely_MultiPlatformApp: App {
@StateObject private var store = PostStore()
var body: some Scene {
WindowGroup {
ContentView(postStore: store)

View File

@ -146,7 +146,7 @@
17DF32D024C8B75C00BCE2E3 /* Model */,
1756AE7F24CB841200FD7257 /* Extensions */,
17DF32CC24C8B72300BCE2E3 /* Navigation */,
17DF32D224C8B78D00BCE2E3 /* Collection */,
17DF32D224C8B78D00BCE2E3 /* PostCollection */,
17DF32D124C8B78500BCE2E3 /* Post */,
17DF32D324C8C9F600BCE2E3 /* Components */,
);
@ -234,11 +234,11 @@
path = Post;
sourceTree = "<group>";
};
17DF32D224C8B78D00BCE2E3 /* Collection */ = {
17DF32D224C8B78D00BCE2E3 /* PostCollection */ = {
isa = PBXGroup;
children = (
);
path = Collection;
path = PostCollection;
sourceTree = "<group>";
};
17DF32D324C8C9F600BCE2E3 /* Components */ = {