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.
 
 
 

87 rader
2.9 KiB

  1. import SwiftUI
  2. struct PostCellView: View {
  3. @EnvironmentObject var model: WriteFreelyModel
  4. @ObservedObject var post: WFAPost
  5. var collectionName: String?
  6. static let createdDateFormat: DateFormatter = {
  7. let formatter = DateFormatter()
  8. formatter.locale = Locale.current
  9. formatter.dateStyle = .long
  10. formatter.timeStyle = .short
  11. return formatter
  12. }()
  13. var titleText: String {
  14. if post.title.isEmpty {
  15. return model.posts.getBodyPreview(of: post)
  16. }
  17. return post.title
  18. }
  19. var body: some View {
  20. HStack {
  21. VStack(alignment: .leading) {
  22. if let collectionName = collectionName {
  23. Text(collectionName)
  24. .font(.caption)
  25. .foregroundColor(.secondary)
  26. .padding(EdgeInsets(top: 3, leading: 4, bottom: 3, trailing: 4))
  27. .overlay(RoundedRectangle(cornerRadius: 2).stroke(Color.secondary, lineWidth: 1))
  28. }
  29. Text(titleText)
  30. .font(.headline)
  31. Text(post.createdDate ?? Date(), formatter: Self.createdDateFormat)
  32. .font(.caption)
  33. .foregroundColor(.secondary)
  34. .padding(.top, -3)
  35. }
  36. Spacer()
  37. PostStatusBadgeView(post: post)
  38. }
  39. .padding(5)
  40. }
  41. }
  42. struct PostCell_AllPostsPreviews: PreviewProvider {
  43. static var previews: some View {
  44. let context = LocalStorageManager.standard.container.viewContext
  45. let testPost = WFAPost(context: context)
  46. testPost.title = "Test Post Title"
  47. testPost.body = "Here's some cool sample body text."
  48. testPost.createdDate = Date()
  49. return PostCellView(post: testPost, collectionName: "My Cool Blog")
  50. .environment(\.managedObjectContext, context)
  51. }
  52. }
  53. struct PostCell_NormalPreviews: PreviewProvider {
  54. static var previews: some View {
  55. let context = LocalStorageManager.standard.container.viewContext
  56. let testPost = WFAPost(context: context)
  57. testPost.title = "Test Post Title"
  58. testPost.body = "Here's some cool sample body text."
  59. testPost.collectionAlias = "My Cool Blog"
  60. testPost.createdDate = Date()
  61. return PostCellView(post: testPost)
  62. .environment(\.managedObjectContext, context)
  63. }
  64. }
  65. struct PostCell_NoTitlePreviews: PreviewProvider {
  66. static var previews: some View {
  67. let context = LocalStorageManager.standard.container.viewContext
  68. let testPost = WFAPost(context: context)
  69. testPost.title = ""
  70. testPost.body = "Here's some cool sample body text."
  71. testPost.collectionAlias = "My Cool Blog"
  72. testPost.createdDate = Date()
  73. return PostCellView(post: testPost)
  74. .environment(\.managedObjectContext, context)
  75. }
  76. }