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 7ca448fc11
commit c65875a43f
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 {
LocalStorageManager.standard.saveContext()
#if os(macOS)
self.selectedPost = cachedPost
self.navState.selectedPost = cachedPost
#endif
cachedPost.status = PostStatus.published.rawValue
}

View File

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

View File

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

View File

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

View File

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

View File

@ -35,7 +35,7 @@ struct ActivePostToolbarView: View {
Button(action: {
model.editor.postToUpdate = activePost
model.updateFromServer(post: activePost)
model.selectedPost = nil
model.navState.selectedPost = nil
}, label: {
Image(systemName: "clock.arrow.circlepath")
.accessibilityLabel(Text("Revert post"))
@ -75,7 +75,7 @@ struct ActivePostToolbarView: View {
}, label: {
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
} else {
HStack(spacing: 4) {
@ -115,12 +115,12 @@ struct ActivePostToolbarView: View {
}
private func createPostUrl() -> [NSURL] {
guard let postId = model.selectedPost?.postId else { return [] }
guard let postId = model.navState.selectedPost?.postId else { return [] }
var urlString: String
if let postSlug = model.selectedPost?.slug,
let postCollectionAlias = model.selectedPost?.collectionAlias {
if let postSlug = model.navState.selectedPost?.slug,
let postCollectionAlias = model.navState.selectedPost?.collectionAlias {
// This post is in a collection, so share the URL as baseURL/postSlug
let urls = collections.filter { $0.alias == postCollectionAlias }
let baseURL = urls.first?.url ?? "\(model.account.server)/\(postCollectionAlias)/"
@ -135,7 +135,7 @@ struct ActivePostToolbarView: View {
}
private func publishPost(_ post: WFAPost) {
if post != model.selectedPost {
if post != model.navState.selectedPost {
return
}
DispatchQueue.main.async {

View File

@ -22,9 +22,9 @@ struct HelpCommands: Commands {
DispatchQueue.main.asyncAfter(deadline: .now()) {
// Unset selected post and collection and navigate to local drafts.
self.model.selectedPost = nil
self.model.selectedCollection = nil
self.model.showAllPosts = false
self.model.navState.selectedPost = nil
self.model.navState.selectedCollection = nil
self.model.navState.showAllPosts = false
// Create the new log post.
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
self.model.isProcessingRequest = false
self.model.selectedPost = newLogPost
self.model.navState.selectedPost = newLogPost
logger.log("Generated local log post.")
}

View File

@ -17,14 +17,14 @@ struct PostCommands: Commands {
Group {
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() {
guard let activePost = model.selectedPost else { return }
guard let activePost = model.navState.selectedPost else { return }
guard let postId = activePost.postId else { return }
guard let urlString = activePost.slug != nil ?
"\(model.account.server)/\((activePost.collectionAlias)!)/\((activePost.slug)!)" :