Skip to content

Commit 66ba472

Browse files
committed
Display number of comments and likes
1 parent 969d86c commit 66ba472

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

WordPress/Classes/ViewRelated/Reader/ReaderPostCell.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private final class ReaderPostCellView: UIView {
178178
postPreview.bottomAnchor.constraint(equalTo: toolbarView.topAnchor),
179179

180180
// Align with a preview, but keep the extended frame to make it easier to tap
181-
toolbarView.leadingAnchor.constraint(equalTo: postPreview.leadingAnchor, constant: -18),
181+
toolbarView.leadingAnchor.constraint(equalTo: postPreview.leadingAnchor, constant: -14),
182182
toolbarView.bottomAnchor.constraint(equalTo: bottomAnchor)
183183
])
184184
}
@@ -241,8 +241,10 @@ private final class ReaderPostCellView: UIView {
241241
imageView.isHidden = true
242242
}
243243

244-
buttons.comment.configuration?.attributedTitle = AttributedString("213", attributes: Self.toolbarAttributes)
245-
buttons.like.configuration?.attributedTitle = AttributedString("1K", attributes: Self.toolbarAttributes)
244+
buttons.comment.isHidden = !viewModel.isCommentsEnabled
245+
buttons.comment.configuration?.attributedTitle = viewModel.commentCount.map { AttributedString($0, attributes: Self.toolbarAttributes) }
246+
buttons.like.isHidden = !viewModel.isLikesEnabled
247+
buttons.like.configuration?.attributedTitle = viewModel.likeCount.map { AttributedString($0, attributes: Self.toolbarAttributes) }
246248
}
247249

248250
private func setAvatar(with viewModel: ReaderPostCellViewModel) {
@@ -291,7 +293,7 @@ private func makeButton(systemImage: String, font: UIFont = UIFont.preferredFont
291293
configuration.imagePadding = 8
292294
configuration.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(font: font)
293295
configuration.baseForegroundColor = .secondaryLabel
294-
configuration.contentInsets = .init(top: 16, leading: 16, bottom: 14, trailing: 16)
296+
configuration.contentInsets = .init(top: 16, leading: 12, bottom: 14, trailing: 12)
295297

296298
let button = UIButton(configuration: configuration)
297299
if #available(iOS 17.0, *) {

WordPress/Classes/ViewRelated/Reader/ReaderPostCellViewModel.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import Foundation
22

33
final class ReaderPostCellViewModel {
4+
// Header
45
@Published var avatarURL: URL?
56
var author: String
67
var time: String
8+
9+
// Content
710
var title: String
811
var details: String
912
var imageURL: URL?
1013

14+
// Footer
15+
var isCommentsEnabled: Bool
16+
var commentCount: String?
17+
var isLikesEnabled: Bool
18+
var likeCount: String?
19+
1120
private var faviconTask: Task<Void, Never>?
1221

1322
deinit {
@@ -21,6 +30,17 @@ final class ReaderPostCellViewModel {
2130
self.details = post.contentPreviewForDisplay()
2231
self.imageURL = post.featuredImageURLForDisplay()
2332

33+
// TODO: (reader) verify that is*Enabled is correct
34+
self.isCommentsEnabled = post.isCommentsEnabled
35+
if isCommentsEnabled, let count = post.commentCount?.intValue, count > 0 {
36+
self.commentCount = kFormatted(count)
37+
}
38+
39+
self.isLikesEnabled = post.isLikesEnabled()
40+
if isLikesEnabled, let count = post.likeCount?.intValue, count > 0 {
41+
self.likeCount = kFormatted(count)
42+
}
43+
2444
if let avatarURL = post.siteIconForDisplay(ofSize: Int(ReaderPostCell.avatarSize)) {
2545
self.avatarURL = avatarURL
2646
} else if let blogURL = post.blogURL.flatMap(URL.init) {
@@ -41,9 +61,17 @@ final class ReaderPostCellViewModel {
4161
self.title = "Discovering the Wonders of the Wild"
4262
self.details = "Lorem ipsum dolor sit amet. Non omnis quia et natus voluptatum et eligendi voluptate vel iusto fuga sit repellendus molestiae aut voluptatem blanditiis ad neque sapiente. Id galisum distinctio quo enim aperiam non veritatis vitae et ducimus rerum."
4363
self.imageURL = URL(string: "https://picsum.photos/1260/630.jpg")
64+
self.isLikesEnabled = true
65+
self.likeCount = "9K"
66+
self.isCommentsEnabled = true
67+
self.commentCount = "213"
4468
}
4569

4670
static func mock() -> ReaderPostCellViewModel {
4771
ReaderPostCellViewModel()
4872
}
4973
}
74+
75+
private func kFormatted(_ count: Int) -> String {
76+
count >= 1000 ? String(format: "%.0fK", Double(count) / 1000) : String(count)
77+
}

0 commit comments

Comments
 (0)