2020-08-04 14:45:46 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2020-08-10 14:25:35 +00:00
|
|
|
struct CollectionListView: View {
|
2020-08-25 15:28:52 +00:00
|
|
|
@EnvironmentObject var model: WriteFreelyModel
|
2022-05-26 12:08:12 +00:00
|
|
|
@EnvironmentObject var errorHandling: ErrorHandling
|
2022-05-28 11:17:33 +00:00
|
|
|
@FetchRequest(sortDescriptors: []) var collections: FetchedResults<WFACollection>
|
2021-09-24 18:45:28 +00:00
|
|
|
@State var selectedCollection: WFACollection?
|
2020-08-07 14:29:22 +00:00
|
|
|
|
2020-08-04 14:45:46 +00:00
|
|
|
var body: some View {
|
2021-09-24 18:45:28 +00:00
|
|
|
List(selection: $selectedCollection) {
|
2020-09-16 18:45:11 +00:00
|
|
|
if model.account.isLoggedIn {
|
2021-09-24 18:45:28 +00:00
|
|
|
NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true))
|
2022-09-07 12:28:53 +00:00
|
|
|
NavigationLink(
|
|
|
|
model.account.server == "https://write.as" ? "Anonymous" : "Drafts",
|
|
|
|
destination: PostListView(selectedCollection: nil, showAllPosts: false)
|
|
|
|
)
|
2020-09-16 18:45:11 +00:00
|
|
|
Section(header: Text("Your Blogs")) {
|
2022-05-28 11:17:33 +00:00
|
|
|
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-09-03 19:22:13 +00:00
|
|
|
}
|
2020-08-04 14:45:46 +00:00
|
|
|
}
|
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")
|
|
|
|
}
|
2020-08-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 18:42:26 +00:00
|
|
|
.navigationTitle(
|
|
|
|
model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
|
|
|
|
)
|
2020-08-07 14:29:22 +00:00
|
|
|
.listStyle(SidebarListStyle())
|
2021-01-14 22:04:52 +00:00
|
|
|
.onChange(of: model.selectedCollection) { collection in
|
2022-09-10 12:30:55 +00:00
|
|
|
model.selectedPost = nil
|
2021-01-25 18:46:30 +00:00
|
|
|
if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
|
|
|
|
self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
|
2021-01-19 15:33:25 +00:00
|
|
|
}
|
2021-01-14 22:04:52 +00:00
|
|
|
}
|
2021-01-19 14:48:00 +00:00
|
|
|
.onChange(of: model.showAllPosts) { value in
|
2022-09-10 12:30:55 +00:00
|
|
|
model.selectedPost = nil
|
2021-01-25 18:46:30 +00:00
|
|
|
if value != model.editor.showAllPostsFlag {
|
|
|
|
self.model.editor.showAllPostsFlag = model.showAllPosts
|
2021-01-19 15:33:25 +00:00
|
|
|
}
|
2021-01-19 14:48:00 +00:00
|
|
|
}
|
2022-05-26 12:08:12 +00:00
|
|
|
.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
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 14:45:46 +00:00
|
|
|
}
|
2020-08-07 14:29:22 +00:00
|
|
|
}
|
2020-08-04 14:45:46 +00:00
|
|
|
|
2020-09-16 18:42:26 +00:00
|
|
|
struct CollectionListView_LoggedOutPreviews: PreviewProvider {
|
2020-08-07 13:53:16 +00:00
|
|
|
static var previews: some View {
|
2021-10-08 21:15:38 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-08-25 15:40:30 +00:00
|
|
|
let model = WriteFreelyModel()
|
2020-09-03 19:22:13 +00:00
|
|
|
|
2020-08-25 15:40:30 +00:00
|
|
|
return CollectionListView()
|
2020-09-09 16:12:19 +00:00
|
|
|
.environment(\.managedObjectContext, context)
|
2020-08-25 15:40:30 +00:00
|
|
|
.environmentObject(model)
|
2020-08-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
}
|