Replace WFModel nav state published vars with model.navState for Mac app

This commit is contained in:
Angelo Stavrow 2024-08-03 07:45:40 -04:00
parent 64dfdd63eb
commit 06682d89b6
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
8 changed files with 30 additions and 27 deletions

View File

@ -242,7 +242,7 @@ extension WriteFreelyModel {
DispatchQueue.main.async { DispatchQueue.main.async {
LocalStorageManager.standard.saveContext() LocalStorageManager.standard.saveContext()
#if os(macOS) #if os(macOS)
self.selectedPost = cachedPost self.navState.selectedPost = cachedPost
#endif #endif
cachedPost.status = PostStatus.published.rawValue cachedPost.status = PostStatus.published.rawValue
} }

View File

@ -28,14 +28,14 @@ struct ContentView: View {
Button(action: { Button(action: {
withAnimation { withAnimation {
// Un-set the currently selected post // Un-set the currently selected post
self.model.selectedPost = nil self.model.navState.selectedPost = nil
} }
// Create the new-post managed object // Create the new-post managed object
let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font)
withAnimation { withAnimation {
DispatchQueue.main.async { DispatchQueue.main.async {
// Load the new post in the editor // Load the new post in the editor
self.model.selectedPost = managedPost self.model.navState.selectedPost = managedPost
} }
} }
}, label: { Image(systemName: "square.and.pencil") }) }, label: { Image(systemName: "square.and.pencil") })
@ -45,9 +45,12 @@ struct ContentView: View {
}, },
postList: { postList: {
ZStack { ZStack {
PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts) PostListView(
.withErrorHandling() selectedCollection: model.navState.selectedCollection,
.frame(width: 300) showAllPosts: model.navState.showAllPosts
)
.withErrorHandling()
.frame(width: 300)
if model.isProcessingRequest { if model.isProcessingRequest {
ZStack { ZStack {
Color(NSColor.controlBackgroundColor).opacity(0.75) Color(NSColor.controlBackgroundColor).opacity(0.75)

View File

@ -19,7 +19,7 @@ struct PostEditorStatusToolbarView: View {
model.editor.postToUpdate = post model.editor.postToUpdate = post
model.updateFromServer(post: post) model.updateFromServer(post: post)
DispatchQueue.main.async { DispatchQueue.main.async {
model.selectedPost = nil model.navState.selectedPost = nil
} }
}, label: { }, label: {
Image(systemName: "square.and.arrow.down") Image(systemName: "square.and.arrow.down")
@ -44,7 +44,7 @@ struct PostEditorStatusToolbarView: View {
.font(.callout) .font(.callout)
.foregroundColor(.secondary) .foregroundColor(.secondary)
Button(action: { Button(action: {
model.selectedPost = nil model.navState.selectedPost = nil
DispatchQueue.main.async { DispatchQueue.main.async {
model.posts.remove(post) model.posts.remove(post)
} }

View File

@ -100,7 +100,7 @@ struct PostListFilteredView: View {
}, },
secondaryButton: .destructive(Text("Delete"), action: { secondaryButton: .destructive(Text("Delete"), action: {
if let postToDelete = model.postToDelete { if let postToDelete = model.postToDelete {
model.selectedPost = nil model.navState.selectedPost = nil
DispatchQueue.main.async { DispatchQueue.main.async {
model.editor.clearLastDraft() model.editor.clearLastDraft()
model.posts.remove(postToDelete) model.posts.remove(postToDelete)
@ -111,7 +111,7 @@ struct PostListFilteredView: View {
) )
} }
.onDeleteCommand(perform: { .onDeleteCommand(perform: {
guard let selectedPost = model.selectedPost else { return } guard let selectedPost = model.navState.selectedPost else { return }
if selectedPost.status == PostStatus.local.rawValue { if selectedPost.status == PostStatus.local.rawValue {
model.postToDelete = selectedPost model.postToDelete = selectedPost
model.isPresentingDeleteAlert = true model.isPresentingDeleteAlert = true

View File

@ -157,8 +157,8 @@ struct PostListView: View {
) )
.toolbar { .toolbar {
ToolbarItemGroup(placement: .primaryAction) { ToolbarItemGroup(placement: .primaryAction) {
if model.selectedPost != nil { if model.navState.selectedPost != nil {
ActivePostToolbarView(activePost: model.selectedPost!) ActivePostToolbarView(activePost: model.navState.selectedPost!)
} }
} }
} }
@ -168,8 +168,8 @@ struct PostListView: View {
) )
) )
.onAppear { .onAppear {
model.selectedCollection = selectedCollection model.navState.selectedCollection = selectedCollection
model.showAllPosts = showAllPosts model.navState.showAllPosts = showAllPosts
} }
.onChange(of: model.hasError) { value in .onChange(of: model.hasError) { value in
if value { if value {

View File

@ -35,7 +35,7 @@ struct ActivePostToolbarView: View {
Button(action: { Button(action: {
model.editor.postToUpdate = activePost model.editor.postToUpdate = activePost
model.updateFromServer(post: activePost) model.updateFromServer(post: activePost)
model.selectedPost = nil model.navState.selectedPost = nil
}, label: { }, label: {
Image(systemName: "clock.arrow.circlepath") Image(systemName: "clock.arrow.circlepath")
.accessibilityLabel(Text("Revert post")) .accessibilityLabel(Text("Revert post"))
@ -75,7 +75,7 @@ struct ActivePostToolbarView: View {
}, label: { }, label: {
Label("Publish…", systemImage: "paperplane") Label("Publish…", systemImage: "paperplane")
}) })
.disabled(model.selectedPost?.body.isEmpty ?? true) .disabled(model.navState.selectedPost?.body.isEmpty ?? true)
.help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length .help("Publish the post to the web.\(model.account.isLoggedIn ? "" : " You must be logged in to do this.")") // swiftlint:disable:this line_length
} else { } else {
HStack(spacing: 4) { HStack(spacing: 4) {
@ -115,12 +115,12 @@ struct ActivePostToolbarView: View {
} }
private func createPostUrl() -> [NSURL] { private func createPostUrl() -> [NSURL] {
guard let postId = model.selectedPost?.postId else { return [] } guard let postId = model.navState.selectedPost?.postId else { return [] }
var urlString: String var urlString: String
if let postSlug = model.selectedPost?.slug, if let postSlug = model.navState.selectedPost?.slug,
let postCollectionAlias = model.selectedPost?.collectionAlias { let postCollectionAlias = model.navState.selectedPost?.collectionAlias {
// This post is in a collection, so share the URL as baseURL/postSlug // This post is in a collection, so share the URL as baseURL/postSlug
let urls = collections.filter { $0.alias == postCollectionAlias } let urls = collections.filter { $0.alias == postCollectionAlias }
let baseURL = urls.first?.url ?? "\(model.account.server)/\(postCollectionAlias)/" let baseURL = urls.first?.url ?? "\(model.account.server)/\(postCollectionAlias)/"
@ -135,7 +135,7 @@ struct ActivePostToolbarView: View {
} }
private func publishPost(_ post: WFAPost) { private func publishPost(_ post: WFAPost) {
if post != model.selectedPost { if post != model.navState.selectedPost {
return return
} }
DispatchQueue.main.async { DispatchQueue.main.async {

View File

@ -22,9 +22,9 @@ struct HelpCommands: Commands {
DispatchQueue.main.asyncAfter(deadline: .now()) { DispatchQueue.main.asyncAfter(deadline: .now()) {
// Unset selected post and collection and navigate to local drafts. // Unset selected post and collection and navigate to local drafts.
self.model.selectedPost = nil self.model.navState.selectedPost = nil
self.model.selectedCollection = nil self.model.navState.selectedCollection = nil
self.model.showAllPosts = false self.model.navState.showAllPosts = false
// Create the new log post. // Create the new log post.
let newLogPost = model.editor.generateNewLocalPost(withFont: 2) let newLogPost = model.editor.generateNewLocalPost(withFont: 2)
@ -39,7 +39,7 @@ struct HelpCommands: Commands {
// Hide the spinner in the post list and set the log post as active // Hide the spinner in the post list and set the log post as active
self.model.isProcessingRequest = false self.model.isProcessingRequest = false
self.model.selectedPost = newLogPost self.model.navState.selectedPost = newLogPost
logger.log("Generated local log post.") logger.log("Generated local log post.")
} }

View File

@ -17,14 +17,14 @@ struct PostCommands: Commands {
Group { Group {
Button(action: sendPostUrlToPasteboard, label: { Text("Copy Link To Published Post") }) Button(action: sendPostUrlToPasteboard, label: { Text("Copy Link To Published Post") })
.disabled(model.selectedPost?.status == PostStatus.local.rawValue) .disabled(model.navState.selectedPost?.status == PostStatus.local.rawValue)
} }
.disabled(model.selectedPost == nil || !model.account.isLoggedIn) .disabled(model.navState.selectedPost == nil || !model.account.isLoggedIn)
} }
} }
private func sendPostUrlToPasteboard() { private func sendPostUrlToPasteboard() {
guard let activePost = model.selectedPost else { return } guard let activePost = model.navState.selectedPost else { return }
guard let postId = activePost.postId else { return } guard let postId = activePost.postId else { return }
guard let urlString = activePost.slug != nil ? guard let urlString = activePost.slug != nil ?
"\(model.account.server)/\((activePost.collectionAlias)!)/\((activePost.slug)!)" : "\(model.account.server)/\((activePost.collectionAlias)!)/\((activePost.slug)!)" :