Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

54 rader
2.3 KiB

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