Skip to content

Commit e52be1f

Browse files
authored
Reader: Cleanup ReaderStreamVC (#23771)
* Remove table view header manipulations * Remove Gridicon usage * Remove postInMainContext * Remove ReaderActionHelpers * Cleanup
1 parent c38f7b1 commit e52be1f

File tree

5 files changed

+17
-103
lines changed

5 files changed

+17
-103
lines changed

WordPress/Classes/ViewRelated/Reader/Controllers/ReaderPostActions/ReaderActionHelpers.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

WordPress/Classes/ViewRelated/Reader/Controllers/ReaderPostActions/ReaderCommentAction.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ final class ReaderCommentAction {
55
promptToAddComment: Bool = false,
66
navigateToCommentID: Int? = nil,
77
source: ReaderCommentsSource) {
8-
guard let postInMainContext = ReaderActionHelpers.postInMainContext(post),
9-
let controller = ReaderCommentsViewController(post: postInMainContext, source: source) else {
10-
return
11-
}
12-
8+
guard let controller = ReaderCommentsViewController(post: post, source: source) else {
9+
return
10+
}
1311
controller.navigateToCommentID = navigateToCommentID as NSNumber?
1412
controller.promptToAddComment = promptToAddComment
1513
controller.trackCommentsOpened()

WordPress/Classes/ViewRelated/Reader/Controllers/ReaderPostActions/ReaderPostMenu.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct ReaderPostMenu {
4646
private var share: UIAction {
4747
UIAction(Strings.share, systemImage: "square.and.arrow.up") {
4848
guard let viewController else { return }
49-
ReaderShareAction().execute(with: post, context: context, anchor: anchor ?? viewController.view, vc: viewController)
49+
ReaderShareAction().execute(with: post, anchor: anchor ?? viewController.view, vc: viewController)
5050
track(.share)
5151
}
5252
}

WordPress/Classes/ViewRelated/Reader/Controllers/ReaderPostActions/ReaderShareAction.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import UIKit
22

33
/// Encapsulates a command share a post
44
final class ReaderShareAction {
5-
func execute(with post: ReaderPost, context: NSManagedObjectContext, anchor: UIPopoverPresentationControllerSourceItem, vc: UIViewController) {
6-
let postID = post.objectID
7-
if let post: ReaderPost = ReaderActionHelpers.existingObject(for: postID, in: context) {
8-
let sharingController = PostSharingController()
5+
func execute(with post: ReaderPost, anchor: UIPopoverPresentationControllerSourceItem, vc: UIViewController) {
6+
let sharingController = PostSharingController()
97

10-
sharingController.shareReaderPost(post, fromAnchor: anchor, inViewController: vc)
11-
WPAnalytics.trackReader(.itemSharedReader, properties: ["blog_id": post.siteID as Any])
12-
}
8+
sharingController.shareReaderPost(post, fromAnchor: anchor, inViewController: vc)
9+
WPAnalytics.trackReader(.itemSharedReader, properties: ["blog_id": post.siteID as Any])
1310
}
1411
}

WordPress/Classes/ViewRelated/Reader/Controllers/ReaderStreamViewController.swift

Lines changed: 9 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ import AutomatticTracks
346346
// the view being fully setup in viewDidLoad.
347347
// See: https://github.com/wordpress-mobile/WordPress-iOS/issues/4419
348348
if didSetupView {
349-
refreshTableViewHeaderLayout()
349+
tableView.sizeToFitHeaderView()
350350
}
351351
}
352352

@@ -529,7 +529,6 @@ import AutomatticTracks
529529
configureStreamHeader()
530530
tableView.setContentOffset(CGPoint.zero, animated: false)
531531
content.refresh()
532-
refreshTableViewHeaderLayout()
533532

534533
if synchronize {
535534
syncIfAppropriate()
@@ -572,10 +571,7 @@ import AutomatticTracks
572571

573572
private func configureCloseButtonIfNeeded() {
574573
if isModal() {
575-
navigationItem.leftBarButtonItem = UIBarButtonItem(image: .gridicon(.cross),
576-
style: .plain,
577-
target: self,
578-
action: #selector(closeButtonTapped))
574+
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "xmark"), style: .plain, target: self, action: #selector(closeButtonTapped))
579575
}
580576
}
581577

@@ -585,56 +581,6 @@ import AutomatticTracks
585581

586582
// MARK: - Instance Methods
587583

588-
/// Retrieve an instance of the specified post from the main NSManagedObjectContext.
589-
///
590-
/// - Parameters:
591-
/// - post: The post to retrieve.
592-
///
593-
/// - Returns: The post fetched from the main context or nil if the post does not exist in the context.
594-
///
595-
private func postInMainContext(_ post: ReaderPost) -> ReaderPost? {
596-
guard let post = (try? ContextManager.sharedInstance().mainContext.existingObject(with: post.objectID)) as? ReaderPost else {
597-
DDLogError("Error retrieving an exsting post from the main context by its object ID.")
598-
return nil
599-
}
600-
return post
601-
}
602-
603-
/// Refreshes the layout of the header. Required for sizing the tableHeaderView according
604-
/// to its intrinsic content layout, and after major layout changes on the viewcontroller itself.
605-
///
606-
private func refreshTableViewHeaderLayout() {
607-
guard let headerView = tableView.tableHeaderView else {
608-
return
609-
}
610-
611-
// The tableView may need to layout, run this layout now, if needed.
612-
// This ensures the proper margins, such as readable margins, are
613-
// inherited and calculated by the headerView.
614-
tableView.layoutIfNeeded()
615-
616-
// Start with the provided UILayoutFittingCompressedSize to let iOS handle its own magic
617-
// number for a "compressed" height, meaning we want our fitting size to be the minimal height.
618-
var fittingSize = UIView.layoutFittingCompressedSize
619-
620-
// Set the width to the tableView's width since this is a known width for the headerView.
621-
// Otherwise, the layout will try and adopt 'any' width and may break based on the how
622-
// the constraints are set up in the nib.
623-
fittingSize.width = tableView.frame.size.width
624-
625-
// Require horizontal fitting since our width is known.
626-
// Use the lower fitting size priority as we want to minimize our height consumption
627-
// according to the layout's contraints and intrinsic size.
628-
let size = headerView.systemLayoutSizeFitting(fittingSize,
629-
withHorizontalFittingPriority: .required,
630-
verticalFittingPriority: .fittingSizeLevel)
631-
// Update the tableHeaderView itself. Classic.
632-
var headerFrame = headerView.frame
633-
headerFrame.size.height = size.height
634-
headerView.frame = headerFrame
635-
tableView.tableHeaderView = headerView
636-
}
637-
638584
/// Scrolls to the top of the list of posts.
639585
@objc func scrollViewToTop() {
640586
guard tableView.numberOfRows(inSection: .zero) > 0 else {
@@ -1481,35 +1427,31 @@ extension ReaderStreamViewController: WPTableViewHandlerDelegate {
14811427
}
14821428
return
14831429
}
1484-
1485-
guard let apost = posts[safe: indexPath.row] else {
1430+
guard let post = posts[safe: indexPath.row] else {
14861431
wpAssertionFailure("invalid_index_path")
14871432
return
14881433
}
1489-
1490-
didSelectPost(apost, at: indexPath)
1434+
didSelectPost(post, at: indexPath)
14911435
}
14921436

1493-
func didSelectPost(_ apost: ReaderPost, at indexPath: IndexPath) {
1494-
guard let post = postInMainContext(apost) else {
1495-
return
1496-
}
1437+
func didSelectPost(_ post: ReaderPost, at indexPath: IndexPath) {
1438+
wpAssert(post.managedObjectContext == viewContext)
14971439

14981440
if post.isKind(of: ReaderGapMarker.self) {
14991441
syncFillingGap(indexPath)
15001442
return
15011443
}
15021444

1503-
if recentlyBlockedSitePostObjectIDs.contains(apost.objectID) {
1504-
unblockSiteForPost(apost)
1445+
if recentlyBlockedSitePostObjectIDs.contains(post.objectID) {
1446+
unblockSiteForPost(post)
15051447
return
15061448
}
15071449

15081450
if let topic = post.topic, ReaderHelpers.isTopicSearchTopic(topic) {
15091451
WPAppAnalytics.track(.readerSearchResultTapped)
15101452

15111453
// We can use `if let` when `ReaderPost` adopts nullability.
1512-
let railcar = apost.railcarDictionary()
1454+
let railcar = post.railcarDictionary()
15131455
if railcar != nil {
15141456
WPAppAnalytics.trackTrainTracksInteraction(.readerSearchResultTapped, withProperties: railcar)
15151457
}

0 commit comments

Comments
 (0)