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.
DispatchQueue.main.async {
#if os(iOS)
self.selectedPost = post
self.navState.selectedPost = post
#endif
self.isProcessingRequest = true
}
@ -175,7 +175,7 @@ extension WriteFreelyModel {
self.isProcessingRequest = true
}
selectedPost = post
navState.selectedPost = post
post.collectionAlias = newCollection?.alias
loggedInClient.movePost(postId: postId, to: newCollection?.alias, completion: movePostHandler)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -46,9 +46,9 @@ struct PostListView: View {
Button(action: {
let managedPost = model.editor.generateNewLocalPost(withFont: model.preferences.font)
withAnimation {
self.model.showAllPosts = false
self.model.navState.showAllPosts = false
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.model.selectedPost = managedPost
self.model.navState.selectedPost = managedPost
}
}
}, label: {
@ -130,8 +130,8 @@ struct PostListView: View {
.ignoresSafeArea(.all, edges: .bottom)
.onAppear {
// Set the selected collection and whether or not we want to show all posts
model.selectedCollection = selectedCollection
model.showAllPosts = showAllPosts
model.navState.selectedCollection = selectedCollection
model.navState.showAllPosts = showAllPosts
// 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.

View File

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

View File

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

View File

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