From dcb18c86db1cb375e8a94706fa6e462e343ac575 Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Thu, 21 Jan 2021 15:39:28 -0500 Subject: [PATCH] Set app state in App entrypoint for macOS, in relevant Views for iOS --- Shared/PostCollection/CollectionListView.swift | 10 +++++++++- Shared/PostList/PostListFilteredView.swift | 3 +++ Shared/WriteFreely_MultiPlatformApp.swift | 6 +++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index aebe15f..8096584 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -48,7 +48,7 @@ struct CollectionListView: View { Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts") }) Section(header: Text("Your Blogs")) { - ForEach(collections, id: \.alias) { collection in + ForEach(collections, id: \.self) { collection in NavigationLink( destination: PostListView(), isActive: Binding( @@ -77,6 +77,14 @@ struct CollectionListView: View { model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" ) .listStyle(SidebarListStyle()) + .onAppear(perform: { + #if os(iOS) + DispatchQueue.main.async { + self.model.showAllPosts = showAllPostsFlag + self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() + } + #endif + }) .onChange(of: model.selectedCollection) { collection in if collection != fetchSelectedCollectionFromAppStorage() { self.selectedCollectionURL = collection?.objectID.uriRepresentation() diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index 6848ce6..d6b35ac 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -62,6 +62,9 @@ struct PostListFilteredView: View { } .onAppear(perform: { self.postCount = fetchRequest.wrappedValue.count + DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { + self.model.selectedPost = fetchSelectedPostFromAppStorage() + } }) .onChange(of: fetchRequest.wrappedValue.count, perform: { value in self.postCount = value diff --git a/Shared/WriteFreely_MultiPlatformApp.swift b/Shared/WriteFreely_MultiPlatformApp.swift index 9171cf3..b7dc143 100644 --- a/Shared/WriteFreely_MultiPlatformApp.swift +++ b/Shared/WriteFreely_MultiPlatformApp.swift @@ -36,11 +36,11 @@ struct WriteFreely_MultiPlatformApp: App { WindowGroup { ContentView() .onAppear(perform: { + #if os(macOS) self.model.showAllPosts = showAllPostsFlag self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() - DispatchQueue.main.asyncAfter(deadline: .now()) { - self.model.selectedPost = fetchSelectedPostFromAppStorage() - } + self.model.selectedPost = fetchSelectedPostFromAppStorage() + #endif }) .environmentObject(model) .environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)