mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Use same code path to set iOS and Mac app state on launch
This commit is contained in:
parent
34b14ba46c
commit
900ef269ba
@ -75,21 +75,6 @@ struct CollectionListView: View {
|
||||
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
|
||||
)
|
||||
.listStyle(SidebarListStyle())
|
||||
.onAppear(perform: {
|
||||
#if os(iOS)
|
||||
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 != model.editor.fetchSelectedCollectionFromAppStorage() {
|
||||
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
|
||||
|
@ -10,8 +10,7 @@ enum PostAppearance: String {
|
||||
struct PostEditorModel {
|
||||
@AppStorage("showAllPostsFlag") var showAllPostsFlag: Bool = false
|
||||
@AppStorage("selectedCollectionURL") var selectedCollectionURL: URL?
|
||||
@AppStorage("selectedPostURL") var selectedPostURL: URL?
|
||||
@AppStorage("lastDraftURL") private var lastDraftURL: URL?
|
||||
@AppStorage("lastDraftURL") var lastDraftURL: URL?
|
||||
|
||||
func saveLastDraft(_ post: WFAPost) {
|
||||
self.lastDraftURL = post.status != PostStatus.published.rawValue ? post.objectID.uriRepresentation() : nil
|
||||
@ -21,15 +20,9 @@ struct PostEditorModel {
|
||||
self.lastDraftURL = nil
|
||||
}
|
||||
|
||||
func fetchLastDraftFromUserDefaults() -> WFAPost? {
|
||||
func fetchLastDraftFromAppStorage() -> WFAPost? {
|
||||
guard let postURL = lastDraftURL else { return nil }
|
||||
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let postManagedObjectID = coordinator.managedObjectID(forURIRepresentation: postURL) else { return nil }
|
||||
guard let post = LocalStorageManager.persistentContainer.viewContext.object(
|
||||
with: postManagedObjectID
|
||||
) as? WFAPost else { return nil }
|
||||
|
||||
guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil }
|
||||
return post
|
||||
}
|
||||
|
||||
@ -55,22 +48,16 @@ struct PostEditorModel {
|
||||
return managedPost
|
||||
}
|
||||
|
||||
func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
|
||||
guard let collectionURL = selectedCollectionURL else { return nil }
|
||||
guard let collection = fetchManagedObject(from: collectionURL) as? WFACollection else { return nil }
|
||||
return collection
|
||||
}
|
||||
|
||||
private func fetchManagedObject(from objectURL: URL) -> NSManagedObject? {
|
||||
let coordinator = LocalStorageManager.persistentContainer.persistentStoreCoordinator
|
||||
guard let managedObjectID = coordinator.managedObjectID(forURIRepresentation: objectURL) else { return nil }
|
||||
let object = LocalStorageManager.persistentContainer.viewContext.object(with: managedObjectID)
|
||||
return object
|
||||
}
|
||||
|
||||
func fetchSelectedPostFromAppStorage() -> WFAPost? {
|
||||
guard let postURL = selectedPostURL else { return nil }
|
||||
guard let post = fetchManagedObject(from: postURL) as? WFAPost else { return nil }
|
||||
return post
|
||||
}
|
||||
|
||||
func fetchSelectedCollectionFromAppStorage() -> WFACollection? {
|
||||
guard let collectionURL = selectedCollectionURL else { return nil }
|
||||
guard let collection = fetchManagedObject(from: collectionURL) as? WFACollection else { return nil }
|
||||
return collection
|
||||
}
|
||||
}
|
||||
|
@ -61,18 +61,10 @@ struct PostListFilteredView: View {
|
||||
}
|
||||
.onAppear(perform: {
|
||||
self.postCount = fetchRequest.wrappedValue.count
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
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 != model.editor.fetchSelectedPostFromAppStorage() {
|
||||
saveSelectedPostURL(post)
|
||||
}
|
||||
}
|
||||
#else
|
||||
List(selection: $model.selectedPost) {
|
||||
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
|
||||
@ -127,18 +119,9 @@ struct PostListFilteredView: View {
|
||||
model.isPresentingDeleteAlert = true
|
||||
}
|
||||
})
|
||||
.onChange(of: model.selectedPost) { post in
|
||||
if post != fetchSelectedPostFromAppStorage() {
|
||||
saveSelectedPostURL(post)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func saveSelectedPostURL(_ post: WFAPost?) {
|
||||
self.model.editor.selectedPostURL = post?.objectID.uriRepresentation()
|
||||
}
|
||||
|
||||
func delete(_ post: WFAPost) {
|
||||
DispatchQueue.main.async {
|
||||
if post == model.selectedPost {
|
||||
|
@ -33,7 +33,6 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
.onAppear(perform: {
|
||||
#if os(macOS)
|
||||
if model.editor.showAllPostsFlag {
|
||||
DispatchQueue.main.async {
|
||||
self.model.selectedCollection = nil
|
||||
@ -46,9 +45,12 @@ struct WriteFreely_MultiPlatformApp: App {
|
||||
}
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
|
||||
self.model.selectedPost = model.editor.fetchSelectedPostFromAppStorage()
|
||||
if model.editor.lastDraftURL != nil {
|
||||
self.model.selectedPost = model.editor.fetchLastDraftFromAppStorage()
|
||||
} else {
|
||||
createNewLocalPost()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
})
|
||||
.environmentObject(model)
|
||||
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)
|
||||
|
Loading…
Reference in New Issue
Block a user