Add collection name to post list cell if it exists

This commit is contained in:
Angelo Stavrow 2020-10-01 17:00:27 -04:00
parent 0a68aad66b
commit c55636df89
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE

View File

@ -2,33 +2,41 @@ 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
return dateFormatter.string(from: date)
}
}
struct PostCell_Previews: PreviewProvider {
struct PostCell_AllPostsPreviews: PreviewProvider {
static var previews: some View {
let context = LocalStorageManager.persistentContainer.viewContext
let testPost = WFAPost(context: context)
@ -36,6 +44,20 @@ struct PostCell_Previews: PreviewProvider {
testPost.body = "Here's some cool sample body text."
testPost.createdDate = Date()
return PostCellView(post: testPost, collectionName: "My Cool Blog")
.environment(\.managedObjectContext, context)
}
}
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)
}