Handle selectedCollection in CollectionView

This commit is contained in:
Angelo Stavrow 2021-01-13 17:03:42 -05:00
parent d51fa4672a
commit 1acd25ad42
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 73 additions and 63 deletions

View File

@ -42,6 +42,8 @@ struct ContentView: View {
}
withAnimation {
DispatchQueue.main.async {
self.model.showAllPosts = false
self.model.selectedCollection = nil
self.model.selectedPost = managedPost
}
}
@ -54,7 +56,7 @@ struct ContentView: View {
#if os(macOS)
ZStack {
PostListView(selectedCollection: nil, showAllPosts: false) //model.account.isLoggedIn)
PostListView()
if model.isProcessingRequest {
ZStack {
Color(NSColor.controlBackgroundColor).opacity(0.75)
@ -63,7 +65,7 @@ struct ContentView: View {
}
}
#else
PostListView(selectedCollection: nil, showAllPosts: model.account.isLoggedIn)
PostListView()
#endif
Text("Select a post, or create a new local draft.")

View File

@ -9,21 +9,48 @@ struct CollectionListView: View {
) var collections: FetchedResults<WFACollection>
var body: some View {
List {
List(selection: $model.selectedCollection) {
if model.account.isLoggedIn {
NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: true)) {
NavigationLink(
destination: PostListView(),
isActive: Binding<Bool>(
get: { () -> Bool in
model.selectedCollection == nil && model.showAllPosts
}, set: { newValue in
if newValue {
self.model.selectedCollection = nil
self.model.showAllPosts = true
} else {
// No-op
}
}
)) {
Text("All Posts")
}
NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) {
NavigationLink(
destination: PostListView(),
isActive: Binding<Bool>(
get: { () -> Bool in
model.selectedCollection == nil && !model.showAllPosts
}, set: { newValue in
if newValue {
self.model.selectedCollection = nil
self.model.showAllPosts = false
} else {
// No-op
}
}
)) {
Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts")
}
Section(header: Text("Your Blogs")) {
ForEach(collections, id: \.alias) { collection in
NavigationLink(
destination: PostListView(selectedCollection: collection, showAllPosts: false)
) {
Text(collection.title)
}
collection.title,
destination: PostListView(),
tag: collection,
selection: $model.selectedCollection
)
}
}
} else {

View File

@ -6,21 +6,7 @@ struct PostListFilteredView: View {
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
var fetchRequest: FetchRequest<WFAPost>
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.selectedCollection = collection
if showAllPosts {
fetchRequest = FetchRequest<WFAPost>(
entity: WFAPost.entity(),
@ -51,20 +37,20 @@ struct PostListFilteredView: View {
NavigationLink(
destination: PostEditorView(post: post),
tag: post,
selection: $model.selectedPost
) {
if showAllPosts {
if let collection = collections.filter { $0.alias == post.collectionAlias }.first {
PostCellView(post: post, collectionName: collection.title)
selection: $model.selectedPost,
label: {
if model.showAllPosts {
if let collection = collections.filter { $0.alias == post.collectionAlias }.first {
PostCellView(post: post, collectionName: collection.title)
} else {
let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
PostCellView(post: post, collectionName: collectionName)
}
} else {
let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
PostCellView(post: post, collectionName: collectionName)
PostCellView(post: post)
}
} else {
PostCellView(post: post)
}
}
.deleteDisabled(post.status != PostStatus.local.rawValue)
})
.deleteDisabled(post.status != PostStatus.local.rawValue)
}
.onDelete(perform: { indexSet in
for index in indexSet {
@ -85,20 +71,20 @@ struct PostListFilteredView: View {
NavigationLink(
destination: PostEditorView(post: post),
tag: post,
selection: $model.selectedPost
) {
if showAllPosts {
if let collection = collections.filter { $0.alias == post.collectionAlias }.first {
PostCellView(post: post, collectionName: collection.title)
selection: $model.selectedPost,
label: {
if model.showAllPosts {
if let collection = collections.filter { $0.alias == post.collectionAlias }.first {
PostCellView(post: post, collectionName: collection.title)
} else {
let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
PostCellView(post: post, collectionName: collectionName)
}
} else {
let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
PostCellView(post: post, collectionName: collectionName)
PostCellView(post: post)
}
} else {
PostCellView(post: post)
}
}
.deleteDisabled(post.status != PostStatus.local.rawValue)
})
.deleteDisabled(post.status != PostStatus.local.rawValue)
}
.onDelete(perform: { indexSet in
for index in indexSet {

View File

@ -5,8 +5,6 @@ struct PostListView: View {
@EnvironmentObject var model: WriteFreelyModel
@Environment(\.managedObjectContext) var managedObjectContext
@State var selectedCollection: WFACollection?
@State var showAllPosts: Bool = false
@State private var postCount: Int = 0
#if os(iOS)
@ -21,9 +19,13 @@ struct PostListView: View {
var body: some View {
#if os(iOS)
ZStack(alignment: .bottom) {
PostListFilteredView(collection: selectedCollection, showAllPosts: showAllPosts, postCount: $postCount)
PostListFilteredView(
collection: model.selectedCollection,
showAllPosts: model.showAllPosts,
postCount: $postCount
)
.navigationTitle(
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.showAllPosts ? "All Posts" : model.selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
)
)
@ -54,8 +56,8 @@ struct PostListView: View {
managedPost.rtl = Locale.characterDirection(forLanguage: languageCode) == .rightToLeft
}
withAnimation {
self.selectedCollection = nil
self.showAllPosts = false
self.model.showAllPosts = false
self.model.selectedCollection = nil
self.model.selectedPost = managedPost
}
}, label: {
@ -122,8 +124,8 @@ struct PostListView: View {
.ignoresSafeArea()
#else //if os(macOS)
PostListFilteredView(
collection: selectedCollection,
showAllPosts: showAllPosts,
collection: model.selectedCollection,
showAllPosts: model.showAllPosts,
postCount: $postCount
)
.toolbar {
@ -145,15 +147,8 @@ struct PostListView: View {
}
}
}
.onDisappear {
DispatchQueue.main.async {
self.model.selectedCollection = nil
self.model.showAllPosts = true
self.model.selectedPost = nil
}
}
.navigationTitle(
showAllPosts ? "All Posts" : selectedCollection?.title ?? (
model.showAllPosts ? "All Posts" : model.selectedCollection?.title ?? (
model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
)
)