Skip to content

Commit 0ea92e9

Browse files
Merge pull request #89 from writeas/show-collection-in-all-posts
Show collection name in "All Posts" list
2 parents 3a9aeae + 6131f9e commit 0ea92e9

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

Shared/PostList/PostCellView.swift

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,60 @@ import SwiftUI
22

33
struct PostCellView: View {
44
@ObservedObject var post: WFAPost
5+
var collectionName: String?
6+
7+
static let createdDateFormat: DateFormatter = {
8+
let formatter = DateFormatter()
9+
formatter.locale = Locale.current
10+
formatter.dateStyle = .long
11+
formatter.timeStyle = .short
12+
return formatter
13+
}()
514

615
var body: some View {
716
HStack {
817
VStack(alignment: .leading) {
18+
if let collectionName = collectionName {
19+
Text(collectionName)
20+
.font(.caption)
21+
.foregroundColor(.secondary)
22+
.padding(EdgeInsets(top: 3, leading: 4, bottom: 3, trailing: 4))
23+
.overlay(RoundedRectangle(cornerRadius: 2).stroke(Color.secondary, lineWidth: 1))
24+
}
925
Text(post.title)
1026
.font(.headline)
11-
.lineLimit(1)
12-
Text(buildDateString(from: post.createdDate ?? Date()))
27+
Text(post.createdDate ?? Date(), formatter: Self.createdDateFormat)
1328
.font(.caption)
14-
.lineLimit(1)
29+
.foregroundColor(.secondary)
30+
.padding(.top, -3)
1531
}
1632
Spacer()
1733
PostStatusBadgeView(post: post)
1834
}
1935
.padding(5)
2036
}
37+
}
2138

22-
func buildDateString(from date: Date) -> String {
23-
let dateFormatter = DateFormatter()
24-
dateFormatter.dateStyle = .long
25-
dateFormatter.timeStyle = .short
39+
struct PostCell_AllPostsPreviews: PreviewProvider {
40+
static var previews: some View {
41+
let context = LocalStorageManager.persistentContainer.viewContext
42+
let testPost = WFAPost(context: context)
43+
testPost.title = "Test Post Title"
44+
testPost.body = "Here's some cool sample body text."
45+
testPost.createdDate = Date()
2646

27-
return dateFormatter.string(from: date)
47+
return PostCellView(post: testPost, collectionName: "My Cool Blog")
48+
.environment(\.managedObjectContext, context)
2849
}
2950
}
3051

31-
struct PostCell_Previews: PreviewProvider {
52+
struct PostCell_NormalPreviews: PreviewProvider {
3253
static var previews: some View {
3354
let context = LocalStorageManager.persistentContainer.viewContext
3455
let testPost = WFAPost(context: context)
3556
testPost.title = "Test Post Title"
3657
testPost.body = "Here's some cool sample body text."
58+
testPost.collectionAlias = "My Cool Blog"
3759
testPost.createdDate = Date()
3860

3961
return PostCellView(post: testPost)

Shared/PostList/PostListFilteredView.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import SwiftUI
33
struct PostListFilteredView: View {
44
@EnvironmentObject var model: WriteFreelyModel
55

6+
@FetchRequest(entity: WFACollection.entity(), sortDescriptors: []) var collections: FetchedResults<WFACollection>
67
var fetchRequest: FetchRequest<WFAPost>
8+
var showAllPosts: Bool
79

810
init(filter: String?, showAllPosts: Bool) {
11+
self.showAllPosts = showAllPosts
912
if showAllPosts {
1013
fetchRequest = FetchRequest<WFAPost>(
1114
entity: WFAPost.entity(),
@@ -37,7 +40,16 @@ struct PostListFilteredView: View {
3740
tag: post,
3841
selection: $model.selectedPost
3942
) {
40-
PostCellView(post: post)
43+
if showAllPosts {
44+
if let collection = collections.filter { $0.alias == post.collectionAlias }.first {
45+
PostCellView(post: post, collectionName: collection.title)
46+
} else {
47+
let collectionName = model.account.server == "https://write.as" ? "Anonymous" : "Drafts"
48+
PostCellView(post: post, collectionName: collectionName)
49+
}
50+
} else {
51+
PostCellView(post: post)
52+
}
4153
}
4254
.deleteDisabled(post.status != PostStatus.local.rawValue)
4355
}

0 commit comments

Comments
 (0)