mirror of
https://github.com/writeas/writefreely-swiftui-multiplatform.git
synced 2024-11-15 01:11:02 +00:00
Fix post-count label to fetch info from managed object context
This commit is contained in:
parent
3c81b5cd68
commit
06510f181e
@ -2,12 +2,12 @@ import SwiftUI
|
||||
|
||||
struct PostListFilteredView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
|
||||
@Binding var postCount: Int
|
||||
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
|
||||
var fetchRequest: FetchRequest<WFAPost>
|
||||
var showAllPosts: Bool
|
||||
|
||||
init(filter: String?, showAllPosts: Bool) {
|
||||
init(filter: String?, showAllPosts: Bool, postCount: Binding<Int>) {
|
||||
self.showAllPosts = showAllPosts
|
||||
if showAllPosts {
|
||||
fetchRequest = FetchRequest<WFAPost>(
|
||||
@ -29,6 +29,7 @@ struct PostListFilteredView: View {
|
||||
)
|
||||
}
|
||||
}
|
||||
_postCount = postCount
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
@ -60,6 +61,12 @@ struct PostListFilteredView: View {
|
||||
}
|
||||
})
|
||||
}
|
||||
.onAppear(perform: {
|
||||
self.postCount = fetchRequest.wrappedValue.count
|
||||
})
|
||||
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
|
||||
self.postCount = value
|
||||
})
|
||||
#else
|
||||
List {
|
||||
ForEach(fetchRequest.wrappedValue, id: \.self) { post in
|
||||
@ -79,6 +86,12 @@ struct PostListFilteredView: View {
|
||||
}
|
||||
})
|
||||
}
|
||||
.onAppear(perform: {
|
||||
self.postCount = fetchRequest.wrappedValue.count
|
||||
})
|
||||
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
|
||||
self.postCount = value
|
||||
})
|
||||
.onDeleteCommand(perform: {
|
||||
guard let selectedPost = model.selectedPost else { return }
|
||||
if selectedPost.status == PostStatus.local.rawValue {
|
||||
@ -96,6 +109,6 @@ struct PostListFilteredView: View {
|
||||
|
||||
struct PostListFilteredView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
return PostListFilteredView(filter: nil, showAllPosts: false)
|
||||
return PostListFilteredView(filter: nil, showAllPosts: false, postCount: .constant(999))
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
struct PostListView: View {
|
||||
@EnvironmentObject var model: WriteFreelyModel
|
||||
@ -6,11 +7,12 @@ struct PostListView: View {
|
||||
|
||||
@State var selectedCollection: WFACollection?
|
||||
@State var showAllPosts: Bool = false
|
||||
@State private var postCount: Int = 0
|
||||
|
||||
var body: some View {
|
||||
#if os(iOS)
|
||||
GeometryReader { geometry in
|
||||
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts)
|
||||
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount)
|
||||
.navigationTitle(
|
||||
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
|
||||
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
|
||||
@ -32,7 +34,7 @@ struct PostListView: View {
|
||||
Image(systemName: "gear")
|
||||
})
|
||||
Spacer()
|
||||
Text(pluralizedPostCount(for: showPosts(for: selectedCollection)))
|
||||
Text(postCount == 1 ? "\(postCount) post" : "\(postCount) posts")
|
||||
.foregroundColor(.secondary)
|
||||
Spacer()
|
||||
if model.isProcessingRequest {
|
||||
@ -52,47 +54,27 @@ struct PostListView: View {
|
||||
}
|
||||
}
|
||||
#else //if os(macOS)
|
||||
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts)
|
||||
.navigationTitle(
|
||||
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
|
||||
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
|
||||
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount)
|
||||
.navigationTitle(
|
||||
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
|
||||
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
|
||||
)
|
||||
)
|
||||
)
|
||||
.navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection)))
|
||||
.toolbar {
|
||||
Button(action: {
|
||||
createNewLocalDraft()
|
||||
}, label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
})
|
||||
Button(action: {
|
||||
reloadFromServer()
|
||||
}, label: {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
})
|
||||
.disabled(!model.account.isLoggedIn)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func pluralizedPostCount(for posts: [WFAPost]) -> String {
|
||||
if posts.count == 1 {
|
||||
return "1 post"
|
||||
} else {
|
||||
return "\(posts.count) posts"
|
||||
}
|
||||
}
|
||||
|
||||
private func showPosts(for collection: WFACollection?) -> [WFAPost] {
|
||||
if showAllPosts {
|
||||
return model.posts.userPosts
|
||||
} else {
|
||||
if let selectedCollection = collection {
|
||||
return model.posts.userPosts.filter { $0.collectionAlias == selectedCollection.alias }
|
||||
} else {
|
||||
return model.posts.userPosts.filter { $0.collectionAlias == nil }
|
||||
.navigationSubtitle(postCount == 1 ? "\(postCount) post" : "\(postCount) posts")
|
||||
.toolbar {
|
||||
Button(action: {
|
||||
createNewLocalDraft()
|
||||
}, label: {
|
||||
Image(systemName: "square.and.pencil")
|
||||
})
|
||||
Button(action: {
|
||||
reloadFromServer()
|
||||
}, label: {
|
||||
Image(systemName: "arrow.clockwise")
|
||||
})
|
||||
.disabled(!model.account.isLoggedIn)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func reloadFromServer() {
|
||||
|
Loading…
Reference in New Issue
Block a user