diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index 6121a89..d95911c 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -1,38 +1,38 @@ import SwiftUI struct ContentView: View { @EnvironmentObject var model: WriteFreelyModel var body: some View { NavigationView { SidebarView() - PostListView(selectedCollection: nil) + PostListView(selectedCollection: nil, showAllPosts: true) Text("Select a post, or create a new local draft.") .foregroundColor(.secondary) } .environmentObject(model) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { let userCollection1 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection2 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection3 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) userCollection1.title = "Collection 1" userCollection2.title = "Collection 2" userCollection3.title = "Collection 3" let model = WriteFreelyModel() model.collections = CollectionListModel() for post in testPostData { model.store.add(post) } return ContentView() .environmentObject(model) .environment(\.managedObjectContext, PersistenceManager.persistentContainer.viewContext) } } diff --git a/Shared/PostCollection/CollectionListView.swift b/Shared/PostCollection/CollectionListView.swift index 0485d8a..9606fcc 100644 --- a/Shared/PostCollection/CollectionListView.swift +++ b/Shared/PostCollection/CollectionListView.swift @@ -1,53 +1,53 @@ import SwiftUI struct CollectionListView: View { @EnvironmentObject var model: WriteFreelyModel @Environment(\.managedObjectContext) var moc @FetchRequest( entity: WFACollection.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \WFACollection.title, ascending: true)] ) var collections: FetchedResults var body: some View { List { -// NavigationLink(destination: PostListView(selectedCollection: CollectionListModel.allPostsCollection)) { -// Text(CollectionListModel.allPostsCollection.title) -// } - NavigationLink(destination: PostListView(selectedCollection: nil)) { + NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: true)) { + Text("All Posts") + } + NavigationLink(destination: PostListView(selectedCollection: nil, showAllPosts: false)) { Text(model.account.server == "https://write.as" ? "Anonymous" : "Drafts") } Section(header: Text("Your Blogs")) { ForEach(collections, id: \.alias) { collection in NavigationLink( - destination: PostListView(selectedCollection: collection) + destination: PostListView(selectedCollection: collection, showAllPosts: false) ) { Text(collection.title) } } } } .navigationTitle("Collections") .listStyle(SidebarListStyle()) } } struct CollectionSidebar_Previews: PreviewProvider { @Environment(\.managedObjectContext) var moc static var previews: some View { let userCollection1 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection2 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection3 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) userCollection1.title = "Collection 1" userCollection2.title = "Collection 2" userCollection3.title = "Collection 3" let model = WriteFreelyModel() model.collections = CollectionListModel() return CollectionListView() .environmentObject(model) .environment(\.managedObjectContext, PersistenceManager.persistentContainer.viewContext) } } diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index d5e96eb..76c0c71 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -1,202 +1,198 @@ import SwiftUI struct PostListView: View { @EnvironmentObject var model: WriteFreelyModel @State var selectedCollection: WFACollection? + @State var showAllPosts: Bool = false #if os(iOS) @State private var isPresentingSettings = false #endif var body: some View { #if os(iOS) GeometryReader { geometry in List { ForEach(showPosts(for: selectedCollection)) { post in NavigationLink( destination: PostEditorView(post: post) ) { PostCellView( post: post ) } } } .environmentObject(model) .navigationTitle( - selectedCollection?.title ?? (model.account.server == "https://write.as" ? "Anonymous" : "Drafts") + showAllPosts ? "All Posts" : selectedCollection?.title ?? ( + model.account.server == "https://write.as" ? "Anonymous" : "Drafts" + ) ) .toolbar { ToolbarItem(placement: .primaryAction) { Button(action: { let post = Post() model.store.add(post) }, label: { Image(systemName: "square.and.pencil") }) } ToolbarItem(placement: .bottomBar) { HStack { Button(action: { isPresentingSettings = true }, label: { Image(systemName: "gear") }).sheet( isPresented: $isPresentingSettings, onDismiss: { isPresentingSettings = false }, content: { SettingsView(isPresented: $isPresentingSettings) } ) .padding(.leading) Spacer() Text(pluralizedPostCount(for: showPosts(for: selectedCollection))) .foregroundColor(.secondary) Spacer() Button(action: { reloadFromServer() }, label: { Image(systemName: "arrow.clockwise") }) .disabled(!model.account.isLoggedIn) } .padding() .frame(width: geometry.size.width) } } } #else //if os(macOS) List { ForEach(showPosts(for: selectedCollection)) { post in NavigationLink( destination: PostEditorView(post: post) ) { PostCellView( post: post ) } } } .navigationTitle( - selectedCollection?.title ?? (model.account.server == "https://write.as" ? "Anonymous" : "Drafts") + showAllPosts ? "All Posts" : selectedCollection?.title ?? ( + model.account.server == "https://write.as" ? "Anonymous" : "Drafts" + ) ) .navigationSubtitle(pluralizedPostCount(for: showPosts(for: selectedCollection))) .toolbar { Button(action: { let post = Post() model.store.add(post) }, label: { Image(systemName: "square.and.pencil") }) Button(action: { reloadFromServer() }, label: { Image(systemName: "arrow.clockwise") }) .disabled(!model.account.isLoggedIn) } #endif } private func pluralizedPostCount(for posts: [Post]) -> String { if posts.count == 1 { return "1 post" } else { return "\(posts.count) posts" } } private func showPosts(for collection: WFACollection?) -> [Post] { - var posts: [Post] - - if let selectedCollection = collection { - posts = model.store.posts.filter { $0.wfPost.collectionAlias == selectedCollection.alias } + if showAllPosts { + return model.store.posts } else { - posts = model.store.posts.filter { $0.wfPost.collectionAlias == nil } + var posts: [Post] + if let selectedCollection = collection { + posts = model.store.posts.filter { $0.wfPost.collectionAlias == selectedCollection.alias } + } else { + posts = model.store.posts.filter { $0.wfPost.collectionAlias == nil } + } + return posts } - -// for post in model.store.posts { -// print("Post '\(post.wfPost.title ?? "Untitled")' in \(post.collection?.title ?? "No collection")") -// } -// if collection == CollectionListModel.allPostsCollection { -// posts = model.store.posts -// } else if collection == CollectionListModel.draftsCollection { -// posts = model.store.posts.filter { $0.collection == nil } -// } else { -// posts = model.store.posts.filter { $0.collection == collection } -// } - - return posts } private func reloadFromServer() { DispatchQueue.main.async { model.collections.clearUserCollection() model.fetchUserCollections() model.fetchUserPosts() } } } struct PostList_Previews: PreviewProvider { static var previews: some View { let userCollection1 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection2 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) let userCollection3 = WFACollection(context: PersistenceManager.persistentContainer.viewContext) userCollection1.title = "Collection 1" userCollection2.title = "Collection 2" userCollection3.title = "Collection 3" let testPostData = [ Post( title: "My First Post", body: "Look at me, creating a first post! That's cool.", createdDate: Date(timeIntervalSince1970: 1595429452), status: .published, collection: userCollection1 ), Post( title: "Post 2: The Quickening", body: "See, here's the rule about Highlander jokes: _there can be only one_.", createdDate: Date(timeIntervalSince1970: 1595514125), status: .edited, collection: userCollection1 ), Post( title: "The Post Revolutions", body: "I can never keep the Matrix movie order straight. Why not just call them part 2 and part 3?", createdDate: Date(timeIntervalSince1970: 1595600006) ), Post( title: "Episode IV: A New Post", body: "How many movies does this person watch? How many movie-title jokes will they make?", createdDate: Date(timeIntervalSince1970: 1596219877), status: .published, collection: userCollection2 ), Post( title: "Fast (Post) Five", body: "Look, it was either a Fast and the Furious reference, or a Resident Evil reference." ), Post( title: "Post: The Final Chapter", body: "And there you have it, a Resident Evil movie reference.", createdDate: Date(timeIntervalSince1970: 1596043684), status: .edited, collection: userCollection3 ) ] let model = WriteFreelyModel() for post in testPostData { model.store.add(post) } return Group { PostListView(selectedCollection: userCollection1) .environmentObject(model) } } }