Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment Detail: Add regular text cells #17105

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ class CommentDetailViewController: UITableViewController {
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch rows[indexPath.row] {
let row = rows[indexPath.row]
switch row {
case .header:
configureHeaderCell()
return headerCell

case .text:
let cell = tableView.dequeueReusableCell(withIdentifier: .textCellIdentifier) ?? .init(style: .subtitle, reuseIdentifier: .textCellIdentifier)
configureTextCell(cell, with: row)
return cell

default:
return .init()
}
Expand All @@ -60,6 +66,9 @@ class CommentDetailViewController: UITableViewController {
case .header:
navigateToPost()

case .text(_, _, _, let action):
action?()

default:
break
}
Expand All @@ -75,19 +84,35 @@ private extension CommentDetailViewController {
case header
case content
case replyIndicator
case textWithDescriptor(descriptor: String, content: String, imageName: String?, action: (() -> Void)?)
case text(title: String, detail: String, image: UIImage? = nil, action: (() -> Void)? = nil)
}

struct Constants {
static let tableLeadingInset: CGFloat = 20.0
}

func configureNavigationBar() {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(editButtonTapped))
}

func configureTable() {
tableView.tableFooterView = UIView(frame: .zero)
// get rid of the separator line for the last cell.
tableView.tableFooterView = UIView(frame: .init(x: 0, y: 0, width: tableView.frame.size.width, height: 1))

// assign 20pt leading inset to the table view, as per the design.
// note that by default, the system assigns 16pt inset for .phone, and 20pt for .pad idioms.
if UIDevice.current.userInterfaceIdiom == .phone {
tableView.directionalLayoutMargins.leading = Constants.tableLeadingInset
}
}

func configureRows() {
rows = [.header]
rows = [
.header,
.text(title: .webAddressLabelText, detail: comment.authorUrlForDisplay(), image: .gridicon(.external), action: visitAuthorURL),
.text(title: .emailAddressLabelText, detail: comment.author_email),
.text(title: .ipAddressLabelText, detail: comment.author_ip)
]
}

// MARK: Cell configuration
Expand All @@ -99,24 +124,48 @@ private extension CommentDetailViewController {
headerCell.detailTextLabel?.text = comment.titleForDisplay()
}

func configureTextCell(_ cell: UITableViewCell, with row: RowType) {
guard case let .text(title, detail, image, _) = row else {
return
}

cell.tintColor = .primary

cell.textLabel?.font = WPStyleGuide.fontForTextStyle(.subheadline)
cell.textLabel?.textColor = .textSubtle
cell.textLabel?.text = title

cell.detailTextLabel?.font = WPStyleGuide.fontForTextStyle(.body)
cell.detailTextLabel?.textColor = .text
cell.detailTextLabel?.numberOfLines = 0
cell.detailTextLabel?.text = detail.isEmpty ? " " : detail // prevent the cell from collapsing due to empty label text.

cell.accessoryView = {
guard let image = image else {
return nil
}
return UIImageView(image: image)
}()
}

// MARK: Actions and navigations

func navigateToPost() {
guard let blog = comment.blog,
let siteID = blog.dotComID,
blog.supports(.wpComRESTAPI) else {
viewPostInWebView()
let postPermalinkURL = URL(string: comment.post?.permaLink ?? "")
openWebView(for: postPermalinkURL)
return
}

let readerViewController = ReaderDetailViewController.controllerWithPostID(NSNumber(value: comment.postID), siteID: siteID, isFeed: false)
navigationController?.pushFullscreenViewController(readerViewController, animated: true)
}

func viewPostInWebView() {
guard let post = comment.post,
let permalink = post.permaLink,
let url = URL(string: permalink) else {
func openWebView(for url: URL?) {
guard let url = url else {
DDLogError("\(Self.classNameWithoutNamespaces()): Attempted to open an invalid URL [\(url?.absoluteString ?? "")]")
return
}

Expand All @@ -135,12 +184,27 @@ private extension CommentDetailViewController {
}
}

func visitAuthorURL() {
guard let authorURL = comment.authorURL() else {
return
}

openWebView(for: authorURL)
}

}

// MARK: - Localization
// MARK: - Strings

private extension String {
// MARK: Constants
static let textCellIdentifier = "textCell"

// MARK: Localization
static let postCommentTitleText = NSLocalizedString("Comment on", comment: "Provides hint that the current screen displays a comment on a post. "
+ "The title of the post will displayed below this string. "
+ "Example: Comment on \n My First Post")
static let webAddressLabelText = NSLocalizedString("Web address", comment: "Describes the web address section in the comment detail screen.")
static let emailAddressLabelText = NSLocalizedString("Email address", comment: "Describes the email address section in the comment detail screen.")
static let ipAddressLabelText = NSLocalizedString("IP address", comment: "Describes the IP address section in the comment detail screen.")
}