Use published properties for selectedCollection and showAllPosts

This commit is contained in:
Angelo Stavrow 2020-12-04 17:08:32 -05:00
parent c87309d6b0
commit a0b1218473
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
5 changed files with 55 additions and 39 deletions

View File

@ -14,6 +14,8 @@ class WriteFreelyModel: ObservableObject {
@Published var isProcessingRequest: Bool = false @Published var isProcessingRequest: Bool = false
@Published var hasNetworkConnection: Bool = true @Published var hasNetworkConnection: Bool = true
@Published var selectedPost: WFAPost? @Published var selectedPost: WFAPost?
@Published var selectedCollection: WFACollection?
@Published var showAllPosts: Bool = true
@Published var isPresentingDeleteAlert: Bool = false @Published var isPresentingDeleteAlert: Bool = false
@Published var isPresentingLoginErrorAlert: Bool = false @Published var isPresentingLoginErrorAlert: Bool = false
@Published var isPresentingNetworkErrorAlert: Bool = false @Published var isPresentingNetworkErrorAlert: Bool = false

View File

@ -51,25 +51,7 @@ struct ContentView: View {
#endif #endif
#if os(macOS) #if os(macOS)
PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn) PostListView(selectedCollection: model.selectedCollection, showAllPosts: model.showAllPosts)
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
if let selectedPost = model.selectedPost {
ActivePostToolbarView(activePost: selectedPost)
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
Alert(
title: Text("Connection Error"),
message: Text("""
There is no internet connection at the moment. Please reconnect or try again later.
"""),
dismissButton: .default(Text("OK"), action: {
model.isPresentingNetworkErrorAlert = false
})
)
})
}
}
}
#else #else
PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn) PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn)
#endif #endif

View File

@ -5,21 +5,33 @@ struct PostListFilteredView: View {
@Binding var postCount: Int @Binding var postCount: Int
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection> @FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
var fetchRequest: FetchRequest<WFAPost> var fetchRequest: FetchRequest<WFAPost>
var showAllPosts: Bool
init(filter: String?, showAllPosts: Bool, postCount: Binding<Int>) { var showAllPosts: Bool {
didSet {
model.showAllPosts = showAllPosts
}
}
var selectedCollection: WFACollection? {
didSet {
model.selectedCollection = selectedCollection
}
}
init(collection: WFACollection?, showAllPosts: Bool, postCount: Binding<Int>) {
self.showAllPosts = showAllPosts self.showAllPosts = showAllPosts
self.selectedCollection = collection
if showAllPosts { if showAllPosts {
fetchRequest = FetchRequest<WFAPost>( fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(), entity: WFAPost.entity(),
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)] sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)]
) )
} else { } else {
if let filter = filter { if let collectionAlias = collection?.alias {
fetchRequest = FetchRequest<WFAPost>( fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(), entity: WFAPost.entity(),
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)],
predicate: NSPredicate(format: "collectionAlias == %@", filter) predicate: NSPredicate(format: "collectionAlias == %@", collectionAlias)
) )
} else { } else {
fetchRequest = FetchRequest<WFAPost>( fetchRequest = FetchRequest<WFAPost>(
@ -105,12 +117,6 @@ struct PostListFilteredView: View {
}) })
) )
} }
.onAppear(perform: {
self.postCount = fetchRequest.wrappedValue.count
})
.onChange(of: fetchRequest.wrappedValue.count, perform: { value in
self.postCount = value
})
.onDeleteCommand(perform: { .onDeleteCommand(perform: {
guard let selectedPost = model.selectedPost else { return } guard let selectedPost = model.selectedPost else { return }
if selectedPost.status == PostStatus.local.rawValue { if selectedPost.status == PostStatus.local.rawValue {
@ -134,6 +140,6 @@ struct PostListFilteredView: View {
struct PostListFilteredView_Previews: PreviewProvider { struct PostListFilteredView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
return PostListFilteredView(filter: nil, showAllPosts: false, postCount: .constant(999)) return PostListFilteredView(collection: nil, showAllPosts: false, postCount: .constant(999))
} }
} }

View File

@ -12,7 +12,7 @@ struct PostListView: View {
var body: some View { var body: some View {
#if os(iOS) #if os(iOS)
GeometryReader { geometry in GeometryReader { geometry in
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount) PostListFilteredView(collection: selectedCollection, showAllPosts: showAllPosts, postCount: $postCount)
.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"
@ -79,13 +79,39 @@ struct PostListView: View {
} }
} }
#else //if os(macOS) #else //if os(macOS)
PostListFilteredView(filter: selectedCollection?.alias, showAllPosts: showAllPosts, postCount: $postCount) PostListFilteredView(
.navigationTitle( collection: selectedCollection,
showAllPosts ? "All Posts" : selectedCollection?.title ?? ( showAllPosts: showAllPosts,
model.account.server == "https://write.as" ? "Anonymous" : "Drafts" postCount: $postCount
) )
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
if let selectedPost = model.selectedPost {
ActivePostToolbarView(activePost: selectedPost)
.alert(isPresented: $model.isPresentingNetworkErrorAlert, content: {
Alert(
title: Text("Connection Error"),
message: Text("""
There is no internet connection at the moment. Please reconnect or try again later.
"""),
dismissButton: .default(Text("OK"), action: {
model.isPresentingNetworkErrorAlert = false
})
)
})
}
}
}
.onDisappear {
DispatchQueue.main.async {
model.selectedPost = nil
}
}
.navigationTitle(
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
) )
.navigationSubtitle(postCount == 1 ? "\(postCount) post" : "\(postCount) posts") )
#endif #endif
} }
} }

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key> <key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>1</integer> <integer>0</integer>
</dict> </dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key> <key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>0</integer> <integer>1</integer>
</dict> </dict>
</dict> </dict>
</dict> </dict>