Skip to content

Commit ac007fa

Browse files
authored
Resolve content link/profile presentation from the local window (#25707)
* Format ActivityFormattableContentView before migration * Resolve Activity content link presentation from the local window * Format CommentContentTableViewCell before migration * Resolve comment profile presentation from the local window * Format ReaderReadMoreView before migration * Resolve Reader read more presentation from the local window * Format ReaderDetailCommentsTableViewDelegate before migration * Resolve comment share presentation from the local window
1 parent a798138 commit ac007fa

4 files changed

Lines changed: 178 additions & 81 deletions

File tree

WordPress/Classes/ViewRelated/Activity/Details/ActivityFormattableContentView.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,35 @@ struct ActivityFormattableContentView: UIViewRepresentable {
5757
super.init()
5858
}
5959

60-
func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
60+
func textView(
61+
_ textView: UITextView,
62+
primaryActionFor textItem: UITextItem,
63+
defaultAction: UIAction
64+
) -> UIAction? {
6165
guard case let .link(URL) = textItem.content else {
6266
return nil
6367
}
6468

65-
return UIAction { [weak self] _ in
66-
self?.routeTo(URL)
69+
return UIAction { [weak self, weak textView] _ in
70+
self?.routeTo(URL, from: textView)
6771
}
6872
}
6973

70-
func textView(_ textView: UITextView, menuConfigurationFor textItem: UITextItem, defaultMenu: UIMenu) -> UITextItem.MenuConfiguration? {
74+
func textView(
75+
_ textView: UITextView,
76+
menuConfigurationFor textItem: UITextItem,
77+
defaultMenu: UIMenu
78+
) -> UITextItem.MenuConfiguration? {
7179
if case .link = textItem.content {
7280
return nil
7381
}
7482

7583
return .init(menu: defaultMenu)
7684
}
7785

78-
private func routeTo(_ URL: URL) {
86+
private func routeTo(_ URL: URL, from view: UIView?) {
7987
// Get the top view controller to create content coordinator
80-
guard let viewController = UIViewController.topViewController else {
88+
guard let viewController = view?.window?.topmostPresentedViewController else {
8189
return
8290
}
8391

WordPress/Classes/ViewRelated/Comments/Views/Detail/CommentContentTableViewCell.swift

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
4848
private var effectiveDepth: Int = 0 {
4949
didSet {
5050
guard oldValue != effectiveDepth else { return }
51-
highlightBarLeadingSpacingConstraint.constant = effectiveDepth == 0 ? 0 : (Self.depthInset * CGFloat(effectiveDepth))
51+
highlightBarLeadingSpacingConstraint.constant =
52+
effectiveDepth == 0 ? 0 : (Self.depthInset * CGFloat(effectiveDepth))
5253
containerStackLeadingConstraint?.constant = (Self.depthInset * CGFloat(effectiveDepth)) + 16
5354
configureDepthSeparators(depth: effectiveDepth)
5455
}
@@ -177,17 +178,23 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
177178
self.isContentExpanded = helper.isCommentExpanded(comment.objectID)
178179
self.onContentLoaded = onContentLoaded
179180

180-
viewModel.$state.sink { [weak self] in
181-
self?.configure(with: $0)
182-
}.store(in: &cancellables)
181+
viewModel.$state
182+
.sink { [weak self] in
183+
self?.configure(with: $0)
184+
}
185+
.store(in: &cancellables)
183186

184-
viewModel.$avatar.sink { [weak self] in
185-
self?.configureAvatar(with: $0)
186-
}.store(in: &cancellables)
187+
viewModel.$avatar
188+
.sink { [weak self] in
189+
self?.configureAvatar(with: $0)
190+
}
191+
.store(in: &cancellables)
187192

188-
viewModel.$content.sink { [weak self] in
189-
self?.configureContent($0 ?? "", helper: helper)
190-
}.store(in: &cancellables)
193+
viewModel.$content
194+
.sink { [weak self] in
195+
self?.configureContent($0 ?? "", helper: helper)
196+
}
197+
.store(in: &cancellables)
191198

192199
// Configure feature availability.
193200
isAccessoryButtonEnabled = comment.isApproved()
@@ -198,7 +205,11 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
198205
/// - Parameters:
199206
/// - comment: The `Comment` object to display.
200207
/// - onContentLoaded: Callback to be called once the content has been loaded. Provides the new content height as parameter.
201-
func configureForPostDetails(with comment: Comment, helper: ReaderCommentsHelper, onContentLoaded: ((CGFloat) -> Void)?) {
208+
func configureForPostDetails(
209+
with comment: Comment,
210+
helper: ReaderCommentsHelper,
211+
onContentLoaded: ((CGFloat) -> Void)?
212+
) {
202213
configure(viewModel: CommentCellViewModel(comment: comment), helper: helper, onContentLoaded: onContentLoaded)
203214

204215
containerStackLeadingConstraint.constant = 0
@@ -231,7 +242,7 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
231242

232243
func configureForCommentDetails() {
233244
containerStackView.isLayoutMarginsRelativeArrangement = true
234-
containerStackView.directionalLayoutMargins = NSDirectionalEdgeInsets(top: 4, leading: 0, bottom: 4, trailing: 0)
245+
containerStackView.layoutMargins = UIEdgeInsets(.vertical, 4)
235246
}
236247

237248
private func configure(with state: CommentCellViewModel.State) {
@@ -250,15 +261,18 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
250261
depthSeparator?.isHidden = true
251262
}
252263
for level in 0..<depth {
253-
let separatorView = depthSeparators[level] ?? {
254-
let separatorView = SeparatorView.vertical(width: 1.0)
255-
depthSeparators[level] = separatorView
256-
contentView.addSubview(separatorView)
257-
separatorView.pinEdges([.top, .bottom])
258-
let inset = -Self.depthInset * CGFloat(level + 1)
259-
separatorView.trailingAnchor.constraint(equalTo: containerStackView.leadingAnchor, constant: inset).isActive = true
260-
return separatorView
261-
}()
264+
let separatorView =
265+
depthSeparators[level]
266+
?? {
267+
let separatorView = SeparatorView.vertical(width: 1.0)
268+
depthSeparators[level] = separatorView
269+
contentView.addSubview(separatorView)
270+
separatorView.pinEdges([.top, .bottom])
271+
let inset = -Self.depthInset * CGFloat(level + 1)
272+
separatorView.trailingAnchor.constraint(equalTo: containerStackView.leadingAnchor, constant: inset)
273+
.isActive = true
274+
return separatorView
275+
}()
262276
separatorView.isHidden = false
263277
}
264278
}
@@ -274,7 +288,8 @@ final class CommentContentTableViewCell: UITableViewCell, NibReusable {
274288
backgroundView.backgroundColor = Style.highlightedBackgroundColor
275289
contentView.insertSubview(backgroundView, belowSubview: containerStackView)
276290
backgroundView.pinEdges([.vertical, .trailing], to: contentView)
277-
backgroundView.leadingAnchor.constraint(equalTo: containerStackView.leadingAnchor, constant: -16).isActive = true
291+
backgroundView.leadingAnchor.constraint(equalTo: containerStackView.leadingAnchor, constant: -16)
292+
.isActive = true
278293
highlightedBackgroundView = backgroundView
279294
}
280295
}
@@ -352,9 +367,16 @@ private extension CommentContentTableViewCell {
352367
var accessoryButtonImage: UIImage? {
353368
switch accessoryButtonType {
354369
case .ellipsis:
355-
return .init(systemName: "ellipsis", withConfiguration: UIImage.SymbolConfiguration(font: .preferredFont(forTextStyle: .footnote)))?.withTintColor(.secondaryLabel)
370+
return .init(
371+
systemName: "ellipsis",
372+
withConfiguration: UIImage.SymbolConfiguration(font: .preferredFont(forTextStyle: .footnote))
373+
)?
374+
.withTintColor(.secondaryLabel)
356375
case .info:
357-
return .init(systemName: "info.circle", withConfiguration: UIImage.SymbolConfiguration(font: .preferredFont(forTextStyle: .footnote)))
376+
return .init(
377+
systemName: "info.circle",
378+
withConfiguration: UIImage.SymbolConfiguration(font: .preferredFont(forTextStyle: .footnote))
379+
)
358380
}
359381
}
360382

@@ -465,7 +487,8 @@ private extension CommentContentTableViewCell {
465487
func updateLikeButton(isLiked: Bool, likeCount: Int) {
466488
likeButton.tintColor = isLiked ? UIAppColor.primary : displaySetting.color.secondaryForeground
467489
if var configuration = likeButton.configuration {
468-
configuration.image = isLiked ? WPStyleGuide.ReaderDetail.likeSelectedToolbarIcon : WPStyleGuide.ReaderDetail.likeToolbarIcon
490+
configuration.image =
491+
isLiked ? WPStyleGuide.ReaderDetail.likeSelectedToolbarIcon : WPStyleGuide.ReaderDetail.likeToolbarIcon
469492
configuration.title = likeCount > 0 ? "\(likeCount)" : String.noLikes
470493
likeButton.accessibilityLabel = {
471494
switch likeCount {
@@ -478,18 +501,21 @@ private extension CommentContentTableViewCell {
478501
} else {
479502
wpAssertionFailure("missing configuration")
480503
}
481-
likeButton.accessibilityLabel = isLiked ? String(likeCount) + .commentIsLiked : String(likeCount) + .commentIsNotLiked
504+
likeButton.accessibilityLabel =
505+
isLiked ? String(likeCount) + .commentIsLiked : String(likeCount) + .commentIsNotLiked
482506
}
483507

484508
// MARK: Content Rendering
485509

486510
func configureContent(_ content: String, helper: ReaderCommentsHelper) {
487-
let renderer = self.renderer ?? {
488-
let renderer = helper.makeWebRenderer()
489-
renderer.delegate = self
490-
self.renderer = renderer
491-
return renderer
492-
}()
511+
let renderer =
512+
self.renderer
513+
?? {
514+
let renderer = helper.makeWebRenderer()
515+
renderer.delegate = self
516+
self.renderer = renderer
517+
return renderer
518+
}()
493519

494520
renderer.displaySettings = displaySetting
495521

@@ -525,7 +551,8 @@ private extension CommentContentTableViewCell {
525551
// point hiding a few pixels
526552
isContentExpanded = true
527553
}
528-
let displayHeight = isContentExpanded ? effectiveContentHeight : min(effectiveContentHeight, Self.maxCollapsedHeight)
554+
let displayHeight =
555+
isContentExpanded ? effectiveContentHeight : min(effectiveContentHeight, Self.maxCollapsedHeight)
529556

530557
contentContainerHeightConstraint?.constant = displayHeight
531558

@@ -550,11 +577,14 @@ private extension CommentContentTableViewCell {
550577
let viewModel = ReaderUserProfileViewModel(comment: comment)
551578
let profileVC = UIHostingController(rootView: ReaderUserProfileView(viewModel: viewModel))
552579
let navigationVC = UINavigationController(rootViewController: profileVC)
553-
profileVC.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: .init { [weak profileVC] _ in
554-
profileVC?.presentingViewController?.dismiss(animated: true)
555-
})
580+
profileVC.navigationItem.leftBarButtonItem = UIBarButtonItem(
581+
systemItem: .close,
582+
primaryAction: .init { [weak profileVC] _ in
583+
profileVC?.presentingViewController?.dismiss(animated: true)
584+
}
585+
)
556586
navigationVC.sheetPresentationController?.detents = [.medium()]
557-
UIViewController.topViewController?.present(navigationVC, animated: true)
587+
window?.topmostPresentedViewController?.present(navigationVC, animated: true)
558588
}
559589

560590
@objc func accessoryButtonTapped() {
@@ -603,6 +633,12 @@ private extension String {
603633
static let likeButtonAccessibilityId = "like-comment-button"
604634
static let reply = NSLocalizedString("Reply", comment: "Reply to a comment.")
605635
static let noLikes = NSLocalizedString("Like", comment: "Button title to Like a comment.")
606-
static let singularLikeFormat = NSLocalizedString("%1$d Like", comment: "Singular button title to Like a comment. %1$d is a placeholder for the number of Likes.")
607-
static let pluralLikesFormat = NSLocalizedString("%1$d Likes", comment: "Plural button title to Like a comment. %1$d is a placeholder for the number of Likes.")
636+
static let singularLikeFormat = NSLocalizedString(
637+
"%1$d Like",
638+
comment: "Singular button title to Like a comment. %1$d is a placeholder for the number of Likes."
639+
)
640+
static let pluralLikesFormat = NSLocalizedString(
641+
"%1$d Likes",
642+
comment: "Plural button title to Like a comment. %1$d is a placeholder for the number of Likes."
643+
)
608644
}

0 commit comments

Comments
 (0)