2020-07-25 11:02:11 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2020-08-17 16:03:27 +00:00
|
|
|
struct PostCellView: View {
|
2020-11-06 19:41:47 +00:00
|
|
|
@EnvironmentObject var model: WriteFreelyModel
|
2020-09-08 20:17:58 +00:00
|
|
|
@ObservedObject var post: WFAPost
|
2020-10-01 21:00:27 +00:00
|
|
|
var collectionName: String?
|
|
|
|
|
|
|
|
static let createdDateFormat: DateFormatter = {
|
|
|
|
let formatter = DateFormatter()
|
|
|
|
formatter.locale = Locale.current
|
|
|
|
formatter.dateStyle = .long
|
|
|
|
formatter.timeStyle = .short
|
|
|
|
return formatter
|
|
|
|
}()
|
2020-07-28 18:12:24 +00:00
|
|
|
|
2020-11-06 19:41:47 +00:00
|
|
|
var titleText: String {
|
|
|
|
if post.title.isEmpty {
|
|
|
|
return model.posts.getBodyPreview(of: post)
|
|
|
|
}
|
|
|
|
return post.title
|
|
|
|
}
|
|
|
|
|
2020-07-25 11:02:11 +00:00
|
|
|
var body: some View {
|
2020-08-07 14:13:36 +00:00
|
|
|
HStack {
|
|
|
|
VStack(alignment: .leading) {
|
2020-10-01 21:00:27 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-11-06 19:41:47 +00:00
|
|
|
Text(titleText)
|
2020-08-07 14:13:36 +00:00
|
|
|
.font(.headline)
|
2020-10-01 21:00:27 +00:00
|
|
|
Text(post.createdDate ?? Date(), formatter: Self.createdDateFormat)
|
2020-08-07 14:13:36 +00:00
|
|
|
.font(.caption)
|
2020-10-01 21:00:27 +00:00
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.padding(.top, -3)
|
2020-07-25 11:02:11 +00:00
|
|
|
}
|
2020-08-07 14:13:36 +00:00
|
|
|
Spacer()
|
2020-08-17 16:05:26 +00:00
|
|
|
PostStatusBadgeView(post: post)
|
2020-08-07 14:13:36 +00:00
|
|
|
}
|
2020-08-07 13:34:16 +00:00
|
|
|
.padding(5)
|
2022-11-19 14:10:24 +00:00
|
|
|
.contextMenu {
|
|
|
|
Button(
|
|
|
|
action: didTapDeleteContextMenuItem,
|
|
|
|
label: { Label("Delete", systemImage: "trash") }
|
|
|
|
)
|
|
|
|
.disabled(post.status != PostStatus.local.rawValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func didTapDeleteContextMenuItem() {
|
|
|
|
guard post.status == PostStatus.local.rawValue else { return }
|
|
|
|
if post === model.selectedPost {
|
|
|
|
model.selectedPost = nil
|
|
|
|
model.editor.clearLastDraft()
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
|
|
|
model.posts.remove(post)
|
|
|
|
}
|
2020-07-25 11:02:11 +00:00
|
|
|
}
|
2020-10-01 21:00:27 +00:00
|
|
|
}
|
2020-07-25 11:02:11 +00:00
|
|
|
|
2020-10-01 21:00:27 +00:00
|
|
|
struct PostCell_AllPostsPreviews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-10-08 21:15:38 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-10-01 21:00:27 +00:00
|
|
|
let testPost = WFAPost(context: context)
|
|
|
|
testPost.title = "Test Post Title"
|
|
|
|
testPost.body = "Here's some cool sample body text."
|
|
|
|
testPost.createdDate = Date()
|
2020-07-25 11:02:11 +00:00
|
|
|
|
2020-10-01 21:00:27 +00:00
|
|
|
return PostCellView(post: testPost, collectionName: "My Cool Blog")
|
|
|
|
.environment(\.managedObjectContext, context)
|
2020-07-25 11:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 21:00:27 +00:00
|
|
|
struct PostCell_NormalPreviews: PreviewProvider {
|
2020-09-09 16:12:19 +00:00
|
|
|
static var previews: some View {
|
2021-10-08 21:15:38 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-09-09 16:12:19 +00:00
|
|
|
let testPost = WFAPost(context: context)
|
|
|
|
testPost.title = "Test Post Title"
|
|
|
|
testPost.body = "Here's some cool sample body text."
|
2020-10-01 21:00:27 +00:00
|
|
|
testPost.collectionAlias = "My Cool Blog"
|
2020-09-09 16:12:19 +00:00
|
|
|
testPost.createdDate = Date()
|
|
|
|
|
|
|
|
return PostCellView(post: testPost)
|
|
|
|
.environment(\.managedObjectContext, context)
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 19:41:47 +00:00
|
|
|
|
|
|
|
struct PostCell_NoTitlePreviews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-10-08 21:15:38 +00:00
|
|
|
let context = LocalStorageManager.standard.container.viewContext
|
2020-11-06 19:41:47 +00:00
|
|
|
let testPost = WFAPost(context: context)
|
|
|
|
testPost.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)
|
|
|
|
}
|
|
|
|
}
|