Improved the overall implementation of the DetailsViewController view controller in the Feed framework.

This commit is contained in:
Javier Cicchelli 2024-03-19 11:53:02 +01:00
parent 07ca1605e2
commit 05b5b8dc50
2 changed files with 99 additions and 53 deletions

View File

@ -1,5 +1,5 @@
// //
// AppDelegate.swift // DetailsViewController.swift
// ReviewsFeed // ReviewsFeed
// //
// Created by Dmitrii Ivanov on 21/07/2020. // Created by Dmitrii Ivanov on 21/07/2020.
@ -8,17 +8,60 @@
import UIKit import UIKit
class DetailsViewController: UIViewController { final class DetailsViewController: UIViewController {
private let review: Review
private var titleLabel = UILabel()
private var authorLabel = UILabel()
private var contentLabel = UILabel()
private var ratingVersionLabel = UILabel()
init(review: Review) { // MARK: Constants
self.review = review private let item: Review
// MARK: Outlets
private lazy var titleLabel = {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .title3)
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = item.title
return label
}()
private lazy var authorLabel = {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .headline)
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = item.author
return label
}()
private lazy var commentLabel = {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .body)
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = item.comment
return label
}()
private lazy var ratingVersionLabel = {
let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .subheadline)
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.text = item.rating.appVersion
return label
}()
// MARK: Initialisers
init(_ item: Review) {
self.item = item
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
} }
@ -26,54 +69,57 @@ class DetailsViewController: UIViewController {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
// MARK: UIViewController
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
view.backgroundColor = UIColor.white
setupViews()
}
func setupViews() { setupView()
titleLabel.translatesAutoresizingMaskIntoConstraints = false }
authorLabel.translatesAutoresizingMaskIntoConstraints = false
contentLabel.translatesAutoresizingMaskIntoConstraints = false }
ratingVersionLabel.translatesAutoresizingMaskIntoConstraints = false
// MARK: - Helpers
private extension DetailsViewController {
// MARK: Functions
func setupView() {
view.backgroundColor = .white
view.addSubview(ratingVersionLabel) view.addSubview(ratingVersionLabel)
view.addSubview(authorLabel) view.addSubview(authorLabel)
view.addSubview(titleLabel) view.addSubview(titleLabel)
view.addSubview(contentLabel) view.addSubview(commentLabel)
ratingVersionLabel.text = review.rating.appVersion NSLayoutConstraint.activate([
ratingVersionLabel.font = UIFont.italicSystemFont(ofSize: 18) authorLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
authorLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
authorLabel.text = review.author authorLabel.topAnchor.constraint(equalTo: ratingVersionLabel.bottomAnchor, constant: 8),
authorLabel.font = UIFont.systemFont(ofSize: 18) authorLabel.heightAnchor.constraint(equalToConstant: 24),
commentLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
titleLabel.text = review.title commentLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
titleLabel.numberOfLines = 0 commentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
titleLabel.font = UIFont.boldSystemFont(ofSize: 22) commentLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 24),
ratingVersionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
contentLabel.text = review.comment ratingVersionLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
contentLabel.numberOfLines = 0 ratingVersionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
ratingVersionLabel.heightAnchor.constraint(equalToConstant: 24),
ratingVersionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
ratingVersionLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
ratingVersionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8).isActive = true titleLabel.topAnchor.constraint(equalTo: authorLabel.bottomAnchor, constant: 8),
ratingVersionLabel.heightAnchor.constraint(equalToConstant: 24).isActive = true titleLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 72),
])
authorLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
authorLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
authorLabel.topAnchor.constraint(equalTo: ratingVersionLabel.bottomAnchor, constant: 8).isActive = true
authorLabel.heightAnchor.constraint(equalToConstant: 24).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
titleLabel.topAnchor.constraint(equalTo: authorLabel.bottomAnchor, constant: 8).isActive = true
titleLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 72).isActive = true
contentLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
contentLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
contentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8).isActive = true
contentLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 24).isActive = true
} }
}
// MARK: - Previews
@available(iOS 17.0, *)
#Preview("Details View Controller with a review") {
UINavigationController(rootViewController: DetailsViewController(.init(
author: "Some author name here...",
comment: "Some long, explanatory review comment goes here...",
id: 1,
rating: .init(stars: 3, appVersion: "v1.0.0"),
title: "Some review title goes here..."
)))
} }

View File

@ -80,7 +80,7 @@ public class FeedViewController: UITableViewController {
_ tableView: UITableView, _ tableView: UITableView,
didSelectRowAt indexPath: IndexPath didSelectRowAt indexPath: IndexPath
) { ) {
let details = DetailsViewController(review: viewModel.items[indexPath.row]) let details = DetailsViewController(viewModel.items[indexPath.row])
tableView.deselectRow( tableView.deselectRow(
at: indexPath, at: indexPath,