Refresh post list view when app becomes active

This commit is contained in:
Angelo Stavrow 2021-11-28 08:01:54 -05:00
parent 8a3017d46f
commit dff18e9d36
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -6,6 +6,7 @@ struct PostListView: View {
@Environment(\.managedObjectContext) var managedObjectContext @Environment(\.managedObjectContext) var managedObjectContext
@State private var postCount: Int = 0 @State private var postCount: Int = 0
@State private var filteredListViewId: Int = 0
var selectedCollection: WFACollection? var selectedCollection: WFACollection?
var showAllPosts: Bool var showAllPosts: Bool
@ -27,6 +28,7 @@ struct PostListView: View {
showAllPosts: showAllPosts, showAllPosts: showAllPosts,
postCount: $postCount postCount: $postCount
) )
.id(self.filteredListViewId)
.navigationTitle( .navigationTitle(
showAllPosts ? "All Posts" : selectedCollection?.title ?? ( showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts" model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
@ -123,6 +125,13 @@ struct PostListView: View {
.frame(height: frameHeight) .frame(height: frameHeight)
.background(Color(UIColor.systemGray5)) .background(Color(UIColor.systemGray5))
.overlay(Divider(), alignment: .top) .overlay(Divider(), alignment: .top)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
// We use this to invalidate and refresh the view, so that new posts created outside of the app (e.g.,
// in the action extension) show up.
withAnimation {
self.filteredListViewId += 1
}
}
} }
.ignoresSafeArea() .ignoresSafeArea()
.onAppear { .onAppear {