diff --git a/Shared/PostList/PostCellView.swift b/Shared/PostList/PostCellView.swift index 7ef7e91..14fa2fc 100644 --- a/Shared/PostList/PostCellView.swift +++ b/Shared/PostList/PostCellView.swift @@ -1,42 +1,64 @@ import SwiftUI struct PostCellView: View { @ObservedObject var post: WFAPost + var collectionName: String? + + static let createdDateFormat: DateFormatter = { + let formatter = DateFormatter() + formatter.locale = Locale.current + formatter.dateStyle = .long + formatter.timeStyle = .short + return formatter + }() var body: some View { HStack { VStack(alignment: .leading) { + if let collectionName = collectionName { + Text(collectionName) + .font(.caption) + .foregroundColor(.secondary) + .padding(EdgeInsets(top: 3, leading: 4, bottom: 3, trailing: 4)) + .overlay(RoundedRectangle(cornerRadius: 2).stroke(Color.secondary, lineWidth: 1)) + } Text(post.title) .font(.headline) - .lineLimit(1) - Text(buildDateString(from: post.createdDate ?? Date())) + Text(post.createdDate ?? Date(), formatter: Self.createdDateFormat) .font(.caption) - .lineLimit(1) + .foregroundColor(.secondary) + .padding(.top, -3) } Spacer() PostStatusBadgeView(post: post) } .padding(5) } +} - func buildDateString(from date: Date) -> String { - let dateFormatter = DateFormatter() - dateFormatter.dateStyle = .long - dateFormatter.timeStyle = .short +struct PostCell_AllPostsPreviews: PreviewProvider { + static var previews: some View { + let context = LocalStorageManager.persistentContainer.viewContext + let testPost = WFAPost(context: context) + testPost.title = "Test Post Title" + testPost.body = "Here's some cool sample body text." + testPost.createdDate = Date() - return dateFormatter.string(from: date) + return PostCellView(post: testPost, collectionName: "My Cool Blog") + .environment(\.managedObjectContext, context) } } -struct PostCell_Previews: PreviewProvider { +struct PostCell_NormalPreviews: PreviewProvider { static var previews: some View { let context = LocalStorageManager.persistentContainer.viewContext let testPost = WFAPost(context: context) testPost.title = "Test Post Title" testPost.body = "Here's some cool sample body text." + testPost.collectionAlias = "My Cool Blog" testPost.createdDate = Date() return PostCellView(post: testPost) .environment(\.managedObjectContext, context) } } diff --git a/Shared/PostList/PostListFilteredView.swift b/Shared/PostList/PostListFilteredView.swift index 9f72c94..bb42ba3 100644 --- a/Shared/PostList/PostListFilteredView.swift +++ b/Shared/PostList/PostListFilteredView.swift @@ -1,89 +1,101 @@ import SwiftUI struct PostListFilteredView: View { @EnvironmentObject var model: WriteFreelyModel + @FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults var fetchRequest: FetchRequest + var showAllPosts: Bool init(filter: String?, showAllPosts: Bool) { + self.showAllPosts = showAllPosts if showAllPosts { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)] ) } else { if let filter = filter { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], predicate: NSPredicate(format: "collectionAlias == %@", filter) ) } else { fetchRequest = FetchRequest( entity: WFAPost.entity(), sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)], predicate: NSPredicate(format: "collectionAlias == nil") ) } } } var body: some View { #if os(iOS) List { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), tag: post, selection: $model.selectedPost ) { - PostCellView(post: post) + if showAllPosts { + if let collection = collections.filter { $0.alias == post.collectionAlias }.first { + PostCellView(post: post, collectionName: collection.title) + } else { + let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts" + PostCellView(post: post, collectionName: collectionName) + } + } else { + PostCellView(post: post) + } } .deleteDisabled(post.status != PostStatus.local.rawValue) } .onDelete(perform: { indexSet in for index in indexSet { let post = fetchRequest.wrappedValue[index] delete(post) } }) } #else List { ForEach(fetchRequest.wrappedValue, id: \.self) { post in NavigationLink( destination: PostEditorView(post: post), tag: post, selection: $model.selectedPost ) { PostCellView(post: post) } .deleteDisabled(post.status != PostStatus.local.rawValue) } .onDelete(perform: { indexSet in for index in indexSet { let post = fetchRequest.wrappedValue[index] delete(post) } }) } .onDeleteCommand(perform: { guard let selectedPost = model.selectedPost else { return } if selectedPost.status == PostStatus.local.rawValue { model.postToDelete = selectedPost model.isPresentingDeleteAlert = true } }) #endif } func delete(_ post: WFAPost) { model.posts.remove(post) } } struct PostListFilteredView_Previews: PreviewProvider { static var previews: some View { return PostListFilteredView(filter: nil, showAllPosts: false) } }