Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

70 líneas
2.9 KiB

  1. import SwiftUI
  2. struct CollectionListView: View {
  3. @EnvironmentObject var model: WriteFreelyModel
  4. @EnvironmentObject var errorHandling: ErrorHandling
  5. @FetchRequest(sortDescriptors: []) var collections: FetchedResults<WFACollection>
  6. @State var selectedCollection: WFACollection?
  7. var body: some View {
  8. List(selection: $selectedCollection) {
  9. if model.account.isLoggedIn {
  10. NavigationLink("All Posts", destination: PostListView(selectedCollection: nil, showAllPosts: true))
  11. NavigationLink(
  12. model.account.server == "https://write.as" ? "Anonymous" : "Drafts",
  13. destination: PostListView(selectedCollection: nil, showAllPosts: false)
  14. )
  15. Section(header: Text("Your Blogs")) {
  16. ForEach(collections, id: \.self) { collection in
  17. NavigationLink(destination: PostListView(selectedCollection: collection, showAllPosts: false),
  18. tag: collection,
  19. selection: $selectedCollection,
  20. label: { Text("\(collection.title)") })
  21. }
  22. }
  23. } else {
  24. NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) {
  25. Text("Drafts")
  26. }
  27. }
  28. }
  29. .navigationTitle(
  30. model.account.isLoggedIn ? "\(URL(string: model.account.server)?.host ?? "WriteFreely")" : "WriteFreely"
  31. )
  32. .listStyle(SidebarListStyle())
  33. .onChange(of: model.selectedCollection) { collection in
  34. model.selectedPost = nil
  35. if collection != model.editor.fetchSelectedCollectionFromAppStorage() {
  36. self.model.editor.selectedCollectionURL = collection?.objectID.uriRepresentation()
  37. }
  38. }
  39. .onChange(of: model.showAllPosts) { value in
  40. model.selectedPost = nil
  41. if value != model.editor.showAllPostsFlag {
  42. self.model.editor.showAllPostsFlag = model.showAllPosts
  43. }
  44. }
  45. .onChange(of: model.hasError) { value in
  46. if value {
  47. if let error = model.currentError {
  48. self.errorHandling.handle(error: error)
  49. } else {
  50. self.errorHandling.handle(error: AppError.genericError())
  51. }
  52. model.hasError = false
  53. }
  54. }
  55. }
  56. }
  57. struct CollectionListView_LoggedOutPreviews: PreviewProvider {
  58. static var previews: some View {
  59. let context = LocalStorageManager.standard.container.viewContext
  60. let model = WriteFreelyModel()
  61. return CollectionListView()
  62. .environment(\.managedObjectContext, context)
  63. .environmentObject(model)
  64. }
  65. }