Skip to content

Commit 75f5d8f

Browse files
committed
Integarate more of the actions
1 parent 66ba472 commit 75f5d8f

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

WordPress/Classes/ViewRelated/Reader/ReaderPostCell.swift

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ final class ReaderPostCell: UITableViewCell {
77

88
static let avatarSize: CGFloat = 28
99

10+
private var viewModel: ReaderPostCellViewModel? // important: has to retain it
11+
1012
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
1113
super.init(style: style, reuseIdentifier: reuseIdentifier)
1214

@@ -23,9 +25,8 @@ final class ReaderPostCell: UITableViewCell {
2325
}
2426

2527
private func registerActions() {
26-
// TODO: (reader) implement actions
27-
2828
view.authorButton.addTarget(self, action: #selector(buttonAuthorTapped), for: .primaryActionTriggered)
29+
view.buttonMore.addTarget(self, action: #selector(buttonMoreTapped), for: .primaryActionTriggered)
2930
view.buttons.bookmark.addTarget(self, action: #selector(buttonBookmarkTapped), for: .primaryActionTriggered)
3031
view.buttons.reblog.addTarget(self, action: #selector(buttonReblogTapped), for: .primaryActionTriggered)
3132
view.buttons.comment.addTarget(self, action: #selector(buttonCommentTapped), for: .primaryActionTriggered)
@@ -35,6 +36,7 @@ final class ReaderPostCell: UITableViewCell {
3536
override func prepareForReuse() {
3637
super.prepareForReuse()
3738

39+
viewModel = nil
3840
view.prepareForReuse()
3941
}
4042

@@ -43,6 +45,8 @@ final class ReaderPostCell: UITableViewCell {
4345
isCompact: Bool,
4446
isSeparatorHidden: Bool
4547
) {
48+
self.viewModel = viewModel
49+
4650
view.isCompact = isCompact
4751
separatorInset = UIEdgeInsets(.leading, isSeparatorHidden ? 9999 : view.insets.left)
4852
view.configure(with: viewModel)
@@ -51,23 +55,27 @@ final class ReaderPostCell: UITableViewCell {
5155
// MARK: - Actions
5256

5357
@objc private func buttonAuthorTapped() {
54-
print("author")
58+
viewModel?.showSiteDetails()
59+
}
60+
61+
@objc private func buttonMoreTapped() {
62+
viewModel?.showMore(with: view.buttonMore)
5563
}
5664

5765
@objc private func buttonBookmarkTapped() {
58-
print("bookmark")
66+
viewModel?.toogleBookmark()
5967
}
6068

6169
@objc private func buttonReblogTapped() {
62-
print("reblog")
70+
viewModel?.reblog()
6371
}
6472

6573
@objc private func buttonCommentTapped() {
66-
print("comment")
74+
viewModel?.comment()
6775
}
6876

6977
@objc private func buttonLikeTapped() {
70-
print("like")
78+
viewModel?.toggleLike()
7179
}
7280
}
7381

@@ -102,7 +110,6 @@ private final class ReaderPostCellView: UIView {
102110

103111
private let coverAspectRatio: CGFloat = 239.0 / 358.0
104112
private var imageViewConstraints: [NSLayoutConstraint] = []
105-
private var viewModel: ReaderPostCellViewModel? // important: has to retain it
106113
private var cancellables: [AnyCancellable] = []
107114

108115
override init(frame: CGRect) {
@@ -221,15 +228,12 @@ private final class ReaderPostCellView: UIView {
221228

222229
func prepareForReuse() {
223230
cancellables = []
224-
viewModel = nil
225231
avatarView.prepareForReuse()
226232
imageView.prepareForReuse()
227233
imageView.isHidden = false
228234
}
229235

230236
func configure(with viewModel: ReaderPostCellViewModel) {
231-
self.viewModel = viewModel
232-
233237
setAvatar(with: viewModel)
234238
authorButton.configuration?.attributedTitle = AttributedString(viewModel.author, attributes: Self.authorAttributes)
235239
timeLabel.text = viewModel.time
@@ -241,10 +245,24 @@ private final class ReaderPostCellView: UIView {
241245
imageView.isHidden = true
242246
}
243247

248+
buttons.bookmark.configuration = {
249+
var configuration = buttons.bookmark.configuration ?? .plain()
250+
configuration.image = UIImage(systemName: viewModel.isBookmarked ? "bookmark.fill" : "bookmark")
251+
configuration.baseForegroundColor = viewModel.isBookmarked ? UIAppColor.brand : .secondaryLabel
252+
return configuration
253+
}()
254+
244255
buttons.comment.isHidden = !viewModel.isCommentsEnabled
245256
buttons.comment.configuration?.attributedTitle = viewModel.commentCount.map { AttributedString($0, attributes: Self.toolbarAttributes) }
257+
246258
buttons.like.isHidden = !viewModel.isLikesEnabled
247-
buttons.like.configuration?.attributedTitle = viewModel.likeCount.map { AttributedString($0, attributes: Self.toolbarAttributes) }
259+
buttons.like.configuration = {
260+
var configuration = buttons.like.configuration ?? .plain()
261+
configuration.attributedTitle = viewModel.likeCount.map { AttributedString($0, attributes: Self.toolbarAttributes) }
262+
configuration.image = UIImage(systemName: viewModel.isLiked ? "star.fill" : "star")
263+
configuration.baseForegroundColor = viewModel.isLiked ? .systemYellow : .secondaryLabel
264+
return configuration
265+
}()
248266
}
249267

250268
private func setAvatar(with viewModel: ReaderPostCellViewModel) {

WordPress/Classes/ViewRelated/Reader/ReaderPostCellViewModel.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,33 @@ final class ReaderPostCellViewModel {
1212
var imageURL: URL?
1313

1414
// Footer
15+
var isBookmarked: Bool
1516
var isCommentsEnabled: Bool
1617
var commentCount: String?
1718
var isLikesEnabled: Bool
1819
var likeCount: String?
20+
var isLiked: Bool
1921

22+
private var post: ReaderPost?
23+
private var followCommentsService: FollowCommentsService?
2024
private var faviconTask: Task<Void, Never>?
2125

26+
weak var viewController: ReaderStreamViewController?
27+
2228
deinit {
2329
faviconTask?.cancel()
2430
}
2531

2632
init(post: ReaderPost) {
33+
self.post = post
34+
2735
self.author = post.blogNameForDisplay()
2836
self.time = post.dateForDisplay()?.toShortString() ?? ""
2937
self.title = post.titleForDisplay()
3038
self.details = post.contentPreviewForDisplay()
3139
self.imageURL = post.featuredImageURLForDisplay()
3240

41+
self.isBookmarked = post.isSavedForLater
3342
// TODO: (reader) verify that is*Enabled is correct
3443
self.isCommentsEnabled = post.isCommentsEnabled
3544
if isCommentsEnabled, let count = post.commentCount?.intValue, count > 0 {
@@ -40,6 +49,7 @@ final class ReaderPostCellViewModel {
4049
if isLikesEnabled, let count = post.likeCount?.intValue, count > 0 {
4150
self.likeCount = kFormatted(count)
4251
}
52+
self.isLiked = post.isLiked()
4353

4454
if let avatarURL = post.siteIconForDisplay(ofSize: Int(ReaderPostCell.avatarSize)) {
4555
self.avatarURL = avatarURL
@@ -61,15 +71,66 @@ final class ReaderPostCellViewModel {
6171
self.title = "Discovering the Wonders of the Wild"
6272
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."
6373
self.imageURL = URL(string: "https://picsum.photos/1260/630.jpg")
74+
self.isBookmarked = false
6475
self.isLikesEnabled = true
6576
self.likeCount = "9K"
6677
self.isCommentsEnabled = true
6778
self.commentCount = "213"
79+
self.isLiked = true
6880
}
6981

7082
static func mock() -> ReaderPostCellViewModel {
7183
ReaderPostCellViewModel()
7284
}
85+
86+
// MARK: Actions
87+
88+
func showSiteDetails() {
89+
guard let post, let viewController else { return }
90+
ReaderHeaderAction().execute(post: post, origin: viewController)
91+
}
92+
93+
func toogleBookmark() {
94+
guard let post, let viewController else { return }
95+
// TODO: (reader) add accurate analytics
96+
ReaderSaveForLaterAction().execute(with: post, origin: .otherStream, viewController: viewController)
97+
}
98+
99+
func reblog() {
100+
guard let post, let viewController else { return }
101+
ReaderReblogAction().execute(readerPost: post, origin: viewController, reblogSource: .list)
102+
}
103+
104+
func comment() {
105+
guard let post, let viewController else { return }
106+
ReaderCommentAction().execute(post: post, origin: viewController, source: .postCard)
107+
}
108+
109+
func toggleLike() {
110+
guard let post else { return }
111+
ReaderLikeAction().execute(with: post)
112+
}
113+
114+
// TODO: (reader) implement the new menu
115+
func showMore(with anchor: UIView) {
116+
guard let post,
117+
let viewController,
118+
let followCommentsService = FollowCommentsService(post: post) else {
119+
return
120+
}
121+
self.followCommentsService = followCommentsService
122+
123+
ReaderMenuAction(logged: true).execute(
124+
post: post,
125+
context: viewController.viewContext,
126+
readerTopic: viewController.readerTopic,
127+
anchor: anchor,
128+
vc: viewController,
129+
source: ReaderPostMenuSource.card,
130+
followCommentsService: followCommentsService
131+
)
132+
WPAnalytics.trackReader(.postCardMoreTapped)
133+
}
73134
}
74135

75136
private func kFormatted(_ count: Int) -> String {

WordPress/Classes/ViewRelated/Reader/ReaderSaveForLaterAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class ReaderSaveForLaterAction {
1515
self.visibleConfirmation = visibleConfirmation
1616
}
1717

18-
func execute(with post: ReaderPost, context: NSManagedObjectContext, origin: ReaderSaveForLaterOrigin, viewController: UIViewController?, completion: (() -> Void)? = nil) {
18+
func execute(with post: ReaderPost, context: NSManagedObjectContext = ContextManager.shared.mainContext, origin: ReaderSaveForLaterOrigin, viewController: UIViewController?, completion: (() -> Void)? = nil) {
1919
/// Preload the post
2020
if let viewController = viewController, !post.isSavedForLater {
2121
let offlineReaderWebView = OfflineReaderWebView()

WordPress/Classes/ViewRelated/Reader/ReaderStreamViewController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,7 @@ extension ReaderStreamViewController: WPTableViewHandlerDelegate {
15281528

15291529
let cell = tableConfiguration.postCell(in: tableView, for: indexPath)
15301530
let viewModel = ReaderPostCellViewModel(post: post)
1531+
viewModel.viewController = self
15311532
cell.configure(with: viewModel, isCompact: traitCollection.horizontalSizeClass == .compact, isSeparatorHidden: !showsSeparator)
15321533
return cell
15331534
}

0 commit comments

Comments
 (0)