mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Set selection of post
This commit is contained in:
parent
6beee8cf32
commit
0b19c8461f
@ -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 {
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user