Replace WFModel nav state published vars with model.navState

This commit is contained in:
Angelo Stavrow 2024-08-03 07:29:10 -04:00
parent 62e781c52e
commit 64dfdd63eb
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
12 changed files with 47 additions and 43 deletions

View File

@ -150,7 +150,7 @@ extension WriteFreelyModel {
// We're starting the network request. // We're starting the network request.
DispatchQueue.main.async { DispatchQueue.main.async {
#if os(iOS) #if os(iOS)
self.selectedPost = post self.navState.selectedPost = post
#endif #endif
self.isProcessingRequest = true self.isProcessingRequest = true
} }
@ -175,7 +175,7 @@ extension WriteFreelyModel {
self.isProcessingRequest = true self.isProcessingRequest = true
} }
selectedPost = post navState.selectedPost = post
post.collectionAlias = newCollection?.alias post.collectionAlias = newCollection?.alias
loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler) loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler)
} }

View File

@ -140,8 +140,8 @@ extension WriteFreelyModel {
if managedPost.collectionAlias != fetchedPost.collectionAlias { if managedPost.collectionAlias != fetchedPost.collectionAlias {
// The post has been moved so we update the managed post's collectionAlias property. // The post has been moved so we update the managed post's collectionAlias property.
DispatchQueue.main.async { DispatchQueue.main.async {
if self.selectedPost == managedPost { if self.navState.selectedPost == managedPost {
self.selectedPost = nil self.navState.selectedPost = nil
} }
managedPost.collectionAlias = fetchedPost.collectionAlias managedPost.collectionAlias = fetchedPost.collectionAlias
} }
@ -233,7 +233,7 @@ extension WriteFreelyModel {
do { do {
let fetchedPost = try result.get() let fetchedPost = try result.get()
#if os(iOS) #if os(iOS)
guard let cachedPost = self.selectedPost else { return } guard let cachedPost = self.navState.selectedPost else { return }
#else #else
guard let cachedPost = self.editor.postToUpdate else { return } guard let cachedPost = self.editor.postToUpdate else { return }
#endif #endif
@ -259,7 +259,7 @@ extension WriteFreelyModel {
do { do {
let succeeded = try result.get() let succeeded = try result.get()
if succeeded { if succeeded {
if let post = selectedPost { if let post = navState.selectedPost {
updateFromServer(post: post) updateFromServer(post: post)
} else { } else {
return return

View File

@ -78,8 +78,11 @@ struct ContentView: View {
.withErrorHandling() .withErrorHandling()
}, },
postList: { postList: {
PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts) PostListView(
.withErrorHandling() selectedCollection: model.navState.selectedCollection,
showAllPosts: model.navState.showAllPosts
)
.withErrorHandling()
}, },
postDetail: { postDetail: {
NoSelectedPostView(isConnected: $model.hasNetworkConnection) NoSelectedPostView(isConnected: $model.hasNetworkConnection)

View File

@ -32,16 +32,16 @@ struct CollectionListView: View {
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
) )
.listStyle(SidebarListStyle()) .listStyle(SidebarListStyle())
.onChange(of: model.selectedCollection) { collection in .onChange(of: model.navState.selectedCollection) { collection in
model.selectedPost = nil model.navState.selectedPost = nil
if collection != model.editor.fetchSelectedCollectionFromAppStorage() { if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation() self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
} }
} }
.onChange(of: model.showAllPosts) { value in .onChange(of: model.navState.showAllPosts) { value in
model.selectedPost = nil model.navState.selectedPost = nil
if value != model.editor.showAllPostsFlag { if value != model.editor.showAllPostsFlag {
self.model.editor.showAllPostsFlag = model.showAllPosts self.model.editor.showAllPostsFlag = model.navState.showAllPosts
} }
} }
.onChange(of: model.hasError) { value in .onChange(of: model.hasError) { value in

View File

@ -39,7 +39,7 @@ struct PostEditorModel {
managedPost.title = "" managedPost.title = ""
managedPost.body = "" managedPost.body = ""
managedPost.status = PostStatus.local.rawValue managedPost.status = PostStatus.local.rawValue
managedPost.collectionAlias = WriteFreelyModel.shared.selectedCollection?.alias managedPost.collectionAlias = WriteFreelyModel.shared.navState.selectedCollection?.alias
switch appearance { switch appearance {
case 1: case 1:
managedPost.appearance = "sans" managedPost.appearance = "sans"

View File

@ -10,7 +10,7 @@ struct DeprecatedListView: View {
var onDelete: (WFAPost) -> Void var onDelete: (WFAPost) -> Void
var body: some View { var body: some View {
List(selection: $model.selectedPost) { List(selection: $model.navState.selectedPost) {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in ForEach(fetchRequest.wrappedValue, id: \.self) { post in
if !searchString.isEmpty && if !searchString.isEmpty &&
!post.title.localizedCaseInsensitiveContains(searchString) && !post.title.localizedCaseInsensitiveContains(searchString) &&
@ -20,9 +20,9 @@ struct DeprecatedListView: View {
NavigationLink( NavigationLink(
destination: PostEditorView(post: post), destination: PostEditorView(post: post),
tag: post, tag: post,
selection: $model.selectedPost, selection: $model.navState.selectedPost,
label: { label: {
if model.showAllPosts { if model.navState.showAllPosts {
if let collection = collections.filter({ $0.alias == post.collectionAlias }).first { if let collection = collections.filter({ $0.alias == post.collectionAlias }).first {
PostCellView(post: post, collectionName: collection.title) PostCellView(post: post, collectionName: collection.title)
} else { } else {

View File

@ -52,8 +52,8 @@ struct PostCellView: View {
private func didTapDeleteContextMenuItem() { private func didTapDeleteContextMenuItem() {
guard post.status == PostStatus.local.rawValue else { return } guard post.status == PostStatus.local.rawValue else { return }
if post === model.selectedPost { if post === model.navState.selectedPost {
model.selectedPost = nil model.navState.selectedPost = nil
model.editor.clearLastDraft() model.editor.clearLastDraft()
} }

View File

@ -48,14 +48,14 @@ struct PostListFilteredView: View {
self.postCount = value self.postCount = value
}) })
} else { } else {
List(selection: $model.selectedPost) { List(selection: $model.navState.selectedPost) {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in ForEach(fetchRequest.wrappedValue, id: \.self) { post in
NavigationLink( NavigationLink(
destination: PostEditorView(post: post), destination: PostEditorView(post: post),
tag: post, tag: post,
selection: $model.selectedPost, selection: $model.navState.selectedPost,
label: { label: {
if model.showAllPosts { if model.navState.showAllPosts {
if let collection = collections.filter({ $0.alias == post.collectionAlias }).first { if let collection = collections.filter({ $0.alias == post.collectionAlias }).first {
PostCellView(post: post, collectionName: collection.title) PostCellView(post: post, collectionName: collection.title)
} else { } else {
@ -122,8 +122,8 @@ struct PostListFilteredView: View {
func delete(_ post: WFAPost) { func delete(_ post: WFAPost) {
DispatchQueue.main.async { DispatchQueue.main.async {
if post == model.selectedPost { if post == model.navState.selectedPost {
model.selectedPost = nil model.navState.selectedPost = nil
model.editor.clearLastDraft() model.editor.clearLastDraft()
} }
model.posts.remove(post) model.posts.remove(post)

View File

@ -46,9 +46,9 @@ struct PostListView: View {
Button(action: { Button(action: {
let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font) let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font)
withAnimation { withAnimation {
self.model.showAllPosts = false self.model.navState.showAllPosts = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.model.selectedPost = managedPost self.model.navState.selectedPost = managedPost
} }
} }
}, label: { }, label: {
@ -130,8 +130,8 @@ struct PostListView: View {
.ignoresSafeArea(.all, edges: .bottom) .ignoresSafeArea(.all, edges: .bottom)
.onAppear { .onAppear {
// Set the selected collection and whether or not we want to show all posts // Set the selected collection and whether or not we want to show all posts
model.selectedCollection = selectedCollection model.navState.selectedCollection = selectedCollection
model.showAllPosts = showAllPosts model.navState.showAllPosts = showAllPosts
// We use this to invalidate and refresh the view, so that new posts created outside of the app (e.g., // We use this to invalidate and refresh the view, so that new posts created outside of the app (e.g.,
// in the action extension) show up. // in the action extension) show up.

View File

@ -16,7 +16,7 @@ struct SearchablePostListFilteredView: View {
var body: some View { var body: some View {
if #available(iOS 16, macOS 13, *) { if #available(iOS 16, macOS 13, *) {
NavigationStack { NavigationStack {
List(fetchRequest.wrappedValue, id: \.self, selection: $model.selectedPost) { post in List(fetchRequest.wrappedValue, id: \.self, selection: $model.navState.selectedPost) { post in
NavigationLink( NavigationLink(
"\(post.title.isEmpty ? "UNTITLED" : post.title)", "\(post.title.isEmpty ? "UNTITLED" : post.title)",
destination: PostEditorView(post: post) destination: PostEditorView(post: post)

View File

@ -40,14 +40,15 @@ struct WriteFreely_MultiPlatformApp: App {
.onAppear(perform: { .onAppear(perform: {
if model.editor.showAllPostsFlag { if model.editor.showAllPostsFlag {
DispatchQueue.main.async { DispatchQueue.main.async {
self.model.selectedCollection = nil self.model.navState.selectedCollection = nil
self.model.showAllPosts = true self.model.navState.showAllPosts = true
showLastDraftOrCreateNewLocalPost() showLastDraftOrCreateNewLocalPost()
} }
} else { } else {
DispatchQueue.main.async { DispatchQueue.main.async {
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage() self.model.navState.selectedCollection =
self.model.showAllPosts = false model.editor.fetchSelectedCollectionFromAppStorage()
self.model.navState.showAllPosts = false
showLastDraftOrCreateNewLocalPost() showLastDraftOrCreateNewLocalPost()
} }
} }
@ -146,7 +147,7 @@ struct WriteFreely_MultiPlatformApp: App {
private func showLastDraftOrCreateNewLocalPost() { private func showLastDraftOrCreateNewLocalPost() {
if model.editor.lastDraftURL != nil { if model.editor.lastDraftURL != nil {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage() self.model.navState.selectedPost = model.editor.fetchLastDraftFromAppStorage()
} }
} else { } else {
createNewLocalPost() createNewLocalPost()
@ -156,14 +157,14 @@ struct WriteFreely_MultiPlatformApp: App {
private func createNewLocalPost() { private func createNewLocalPost() {
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 {
// Set it as the selectedPost // Set it as the selectedPost
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
self.model.selectedPost = managedPost self.model.navState.selectedPost = managedPost
} }
} }
} }
@ -174,9 +175,9 @@ struct WriteFreely_MultiPlatformApp: App {
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)
@ -189,7 +190,7 @@ struct WriteFreely_MultiPlatformApp: App {
postBody.append(contentsOf: logger.fetchLogs()) postBody.append(contentsOf: logger.fetchLogs())
newLogPost.body = postBody.joined(separator: "\n") newLogPost.body = postBody.joined(separator: "\n")
self.model.selectedPost = newLogPost self.model.navState.selectedPost = newLogPost
} }
logger.log("Generated local log post.") logger.log("Generated local log post.")

View File

@ -75,9 +75,9 @@ struct SettingsView: View {
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)