mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Move state-fetching logic to PostEditorModel for iOS app
This commit is contained in:
parent
4ad076ccab
commit
34b14ba46c
@ -2,8 +2,6 @@ import SwiftUI
|
||||
|
||||
struct CollectionListView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
|
||||
@FetchRequest(
|
||||
entity: WFACollection.entity(),
|
||||
@ -79,33 +77,30 @@ struct CollectionListView: View {
|
||||
.listStyle(SidebarListStyle())
|
||||
.onAppear(perform: {
|
||||
#if os(iOS)
|
||||
DispatchQueue.main.async {
|
||||
self.model.showAllPosts = showAllPostsFlag
|
||||
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
|
||||
if model.editor.showAllPostsFlag {
|
||||
DispatchQueue.main.async {
|
||||
self.model.selectedCollection = nil
|
||||
self.model.showAllPosts = true
|
||||
}
|
||||
} else {
|
||||
DispatchQueue.main.async {
|
||||
self.model.selectedCollection = model.editor.fetchSelectedCollectionFromAppStorage()
|
||||
self.model.showAllPosts = false
|
||||
}
|
||||
}
|
||||
#endif
|
||||
})
|
||||
.onChange(of: model.selectedCollection) { collection in
|
||||
if collection != fetchSelectedCollectionFromAppStorage() {
|
||||
self.selectedCollectionURL = collection?.objectID.uriRepresentation()
|
||||
if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
|
||||
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
|
||||
}
|
||||
}
|
||||
.onChange(of: model.showAllPosts) { value in
|
||||
if value != showAllPostsFlag {
|
||||
self.showAllPostsFlag = model.showAllPosts
|
||||
if value != model.editor.showAllPostsFlag {
|
||||
self.model.editor.showAllPostsFlag = model.showAllPosts
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
struct CollectionListView_LoggedOutPreviews: PreviewProvider {
|
||||
|
@ -2,7 +2,6 @@ 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>
|
||||
@ -63,14 +62,14 @@ struct PostListFilteredView: View {
|
||||
.onAppear(perform: {
|
||||
self.postCount = fetchRequest.wrappedValue.count
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
self.model.selectedPost = fetchSelectedPostFromAppStorage()
|
||||
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage()
|
||||
}
|
||||
})
|
||||
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
|
||||
self.postCount = value
|
||||
})
|
||||
.onChange(of: model.selectedPost) { post in
|
||||
if post != fetchSelectedPostFromAppStorage() {
|
||||
if post != model.editor.fetchSelectedPostFromAppStorage() {
|
||||
saveSelectedPostURL(post)
|
||||
}
|
||||
}
|
||||
@ -137,17 +136,7 @@ struct PostListFilteredView: View {
|
||||
}
|
||||
|
||||
private func saveSelectedPostURL(_ post: WFAPost?) {
|
||||
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
|
||||
self.model.editor.selectedPostURL = post?.objectID.uriRepresentation()
|
||||
}
|
||||
|
||||
func delete(_ post: WFAPost) {
|
||||
|
Loading…
Reference in New Issue
Block a user