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") Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")
}) })
Section(header: Text("Your Blogs")) { Section(header: Text("Your Blogs")) {
ForEach(collections, id: \.alias) { collection in ForEach(collections, id: \.self) { collection in
NavigationLink( NavigationLink(
destination: PostListView(), destination: PostListView(),
isActive: Binding<Bool>( isActive: Binding<Bool>(
@ -77,6 +77,14 @@ struct CollectionListView: View {
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely" model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
) )
.listStyle(SidebarListStyle()) .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 .onChange(of: model.selectedCollection) { collection in
if collection != fetchSelectedCollectionFromAppStorage() { if collection != fetchSelectedCollectionFromAppStorage() {
self.selectedCollectionURL = collection?.objectID.uriRepresentation() self.selectedCollectionURL = collection?.objectID.uriRepresentation()

View File

@ -62,6 +62,9 @@ struct PostListFilteredView: View {
} }
.onAppear(perform: { .onAppear(perform: {
self.postCount = fetchRequest.wrappedValue.count self.postCount = fetchRequest.wrappedValue.count
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.model.selectedPost = fetchSelectedPostFromAppStorage()
}
}) })
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in .onChange(of: fetchRequest.wrappedValue.count, perform: { value in
self.postCount = value self.postCount = value

View File

@ -36,11 +36,11 @@ struct WriteFreely_MultiPlatformApp: App {
WindowGroup { WindowGroup {
ContentView() ContentView()
.onAppear(perform: { .onAppear(perform: {
#if os(macOS)
self.model.showAllPosts = showAllPostsFlag self.model.showAllPosts = showAllPostsFlag
self.model.selectedCollection = fetchSelectedCollectionFromAppStorage() self.model.selectedCollection = fetchSelectedCollectionFromAppStorage()
DispatchQueue.main.asyncAfter(deadline: .now()) { self.model.selectedPost = fetchSelectedPostFromAppStorage()
self.model.selectedPost = fetchSelectedPostFromAppStorage() #endif
}
}) })
.environmentObject(model) .environmentObject(model)
.environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext) .environment(\.managedObjectContext, LocalStorageManager.persistentContainer.viewContext)