Set app state in App entrypoint for macOS, in relevant Views for iOS

This commit is contained in:
Angelo Stavrow 2021-01-21 15:39:28 -05:00
parent 0b19c8461f
commit dcb18c86db
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
3 changed files with 15 additions and 4 deletions

View File

@ -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<Bool>(
@ -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()

View File

@ -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

View File

@ -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)