Set selection of post

This commit is contained in:
Angelo Stavrow 2021-01-19 11:29:41 -05:00
parent 6beee8cf32
commit 0b19c8461f
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
2 changed files with 20 additions and 8 deletions

View File

@ -33,7 +33,7 @@ struct PostListFilteredView: View {
var body: some View {
#if os(iOS)
List {
List(selection: $model.selectedPost) {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
NavigationLink(
destination: PostEditorView(post: post),
@ -67,10 +67,12 @@ struct PostListFilteredView: View {
self.postCount = value
})
.onChange(of: model.selectedPost) { post in
saveSelectedPostURL(post)
if post != fetchSelectedPostFromAppStorage() {
saveSelectedPostURL(post)
}
}
#else
List {
List(selection: $model.selectedPost) {
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
NavigationLink(
destination: PostEditorView(post: post),
@ -124,7 +126,9 @@ struct PostListFilteredView: View {
}
})
.onChange(of: model.selectedPost) { post in
saveSelectedPostURL(post)
if post != fetchSelectedPostFromAppStorage() {
saveSelectedPostURL(post)
}
}
#endif
}
@ -133,6 +137,16 @@ struct PostListFilteredView: View {
self.selectedPostURL = post?.objectID.uriRepresentation()
}
private func fetchSelectedPostFromAppStorage() -> WFAPost? {
guard let objectURL = selectedPostURL else { return nil }
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
guard let object = LocalStorageManager.persistentContainer.viewContext.object(
with: managedObjectID
) as? WFAPost else { return nil }
return object
}
func delete(_ post: WFAPost) {
DispatchQueue.main.async {
if post == model.selectedPost {

View File

@ -36,10 +36,8 @@ struct WriteFreely_MultiPlatformApp: App {
WindowGroup {
ContentView()
.onAppear(perform: {
DispatchQueue.main.async {
self.model.showAllPosts = showAllPostsFlag
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
}
self.model.showAllPosts = showAllPostsFlag
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
DispatchQueue.main.asyncAfter(deadline: .now()) {
self.model.selectedPost = fetchSelectedPostFromAppStorage()
}