mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Save/restore navigation state to last selected post and collection
This commit is contained in:
parent
9b06eb34a8
commit
deadfb8509
@ -2,6 +2,8 @@ import SwiftUI
|
||||
|
||||
struct CollectionListView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
|
||||
@FetchRequest(
|
||||
entity: WFACollection.entity(),
|
||||
@ -75,6 +77,9 @@ struct CollectionListView: View {
|
||||
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
|
||||
)
|
||||
.listStyle(SidebarListStyle())
|
||||
.onChange(of: model.selectedCollection) { collection in
|
||||
self.selectedCollectionURL = collection?.objectID.uriRepresentation()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ import SwiftUI
|
||||
|
||||
struct PostListFilteredView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@AppStorage("selectedPostURL") var selectedPostURL: URL?
|
||||
@Binding var postCount: Int
|
||||
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
|
||||
var fetchRequest: FetchRequest<WFAPost>
|
||||
@ -65,6 +66,9 @@ struct PostListFilteredView: View {
|
||||
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
|
||||
self.postCount = value
|
||||
})
|
||||
.onChange(of: model.selectedPost) { post in
|
||||
saveSelectedPostURL(post)
|
||||
}
|
||||
#else
|
||||
List {
|
||||
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
|
||||
@ -119,9 +123,16 @@ struct PostListFilteredView: View {
|
||||
model.isPresentingDeleteAlert = true
|
||||
}
|
||||
})
|
||||
.onChange(of: model.selectedPost) { post in
|
||||
saveSelectedPostURL(post)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func saveSelectedPostURL(_ post: WFAPost?) {
|
||||
self.selectedPostURL = post?.objectID.uriRepresentation()
|
||||
}
|
||||
|
||||
func delete(_ post: WFAPost) {
|
||||
DispatchQueue.main.async {
|
||||
if post == model.selectedPost {
|
||||
|
@ -22,6 +22,9 @@ struct CheckForDebugModifier {
|
||||
|
||||
struct WriteFreely_MultiPlatformApp: App {
|
||||
@StateObject private var model = WriteFreelyModel()
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
@AppStorage("selectedPostURL") var selectedPostURL: URL?
|
||||
|
||||
#if os(macOS)
|
||||
// swiftlint:disable:next weak_delegate
|
||||
@ -33,10 +36,12 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.onAppear(perform: {
|
||||
if let lastDraft = model.editor.fetchLastDraftFromUserDefaults() {
|
||||
self.model.selectedPost = lastDraft
|
||||
} else {
|
||||
createNewLocalPost()
|
||||
DispatchQueue.main.async {
|
||||
self.model.showAllPosts = showAllPostsFlag
|
||||
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now()) {
|
||||
self.model.selectedPost = fetchSelectedPostFromAppStorage()
|
||||
}
|
||||
})
|
||||
.environmentObject(model)
|
||||
@ -146,4 +151,24 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
private func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
|
||||
guard let objectURL = selectedCollectionURL 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? WFACollection else { return nil }
|
||||
return object
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user