swiftui-multiplatform/Shared/PostCollection/CollectionListView.swift

70 lines
2.9 KiB
Swift
Raw Permalink Normal View History

import SwiftUI
struct CollectionListView: View {
@EnvironmentObject var model: WriteFreelyModel
@EnvironmentObject var errorHandling: ErrorHandling
@FetchRequest(sortDescriptors: []) var collections: FetchedResults<WFACollection>
2021-09-24 18:45:28 +00:00
@State var selectedCollection: WFACollection?
var body: some View {
2021-09-24 18:45:28 +00:00
List(selection: $selectedCollection) {
if model.account.isLoggedIn {
2021-09-24 18:45:28 +00:00
NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true))
NavigationLink(
model.account.server == "https://write.as" ? "Anonymous" : "Drafts",
destination: PostListView(selectedCollection: nil, showAllPosts: false)
)
Section(header: Text("Your Blogs")) {
ForEach(collections, id: \.self) { collection in
2021-09-24 18:45:28 +00:00
NavigationLink(destination: PostListView(selectedCollection: collection, showAllPosts: false),
tag: collection,
selection: $selectedCollection,
label: { Text("\(collection.title)") })
}
}
2020-10-29 14:37:16 +00:00
} else {
2021-09-24 18:45:28 +00:00
NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) {
2020-10-29 14:37:16 +00:00
Text("Drafts")
}
}
}
.navigationTitle(
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
)
.listStyle(SidebarListStyle())
.onChange(of: model.selectedCollection) { collection in
model.selectedPost = nil
if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
}
}
.onChange(of: model.showAllPosts) { value in
model.selectedPost = nil
if value != model.editor.showAllPostsFlag {
self.model.editor.showAllPostsFlag = model.showAllPosts
}
}
.onChange(of: model.hasError) { value in
if value {
if let error = model.currentError {
self.errorHandling.handle(error: error)
} else {
self.errorHandling.handle(error: AppError.genericError())
}
model.hasError = false
}
}
}
}
struct CollectionListView_LoggedOutPreviews: PreviewProvider {
static var previews: some View {
let context = LocalStorageManager.standard.container.viewContext
let model = WriteFreelyModel()
return CollectionListView()
2020-09-09 16:12:19 +00:00
.environment(\.managedObjectContext, context)
.environmentObject(model)
}
}