Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
2.7 KiB

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