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
//
// Created by Dmitrii Ivanov on 21/07/2020.
@ -8,17 +8,60 @@
import UIKit
class DetailsViewController: UIViewController {
private let review: Review
private var titleLabel = UILabel()
private var authorLabel = UILabel()
private var contentLabel = UILabel()
private var ratingVersionLabel = UILabel()
final class DetailsViewController: UIViewController {
init(review: Review) {
self.review = review
// MARK: Constants
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)
}
@ -26,54 +69,57 @@ class DetailsViewController: UIViewController {
fatalError("init(coder:) has not been implemented")
}
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
setupViews()
}
func setupViews() {
titleLabel.translatesAutoresizingMaskIntoConstraints = false
authorLabel.translatesAutoresizingMaskIntoConstraints = false
contentLabel.translatesAutoresizingMaskIntoConstraints = false
ratingVersionLabel.translatesAutoresizingMaskIntoConstraints = false
setupView()
}
}
// MARK: - Helpers
private extension DetailsViewController {
// MARK: Functions
func setupView() {
view.backgroundColor = .white
view.addSubview(ratingVersionLabel)
view.addSubview(authorLabel)
view.addSubview(titleLabel)
view.addSubview(contentLabel)
view.addSubview(commentLabel)
ratingVersionLabel.text = review.rating.appVersion
ratingVersionLabel.font = UIFont.italicSystemFont(ofSize: 18)
authorLabel.text = review.author
authorLabel.font = UIFont.systemFont(ofSize: 18)
titleLabel.text = review.title
titleLabel.numberOfLines = 0
titleLabel.font = UIFont.boldSystemFont(ofSize: 22)
contentLabel.text = review.comment
contentLabel.numberOfLines = 0
ratingVersionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8).isActive = true
ratingVersionLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8).isActive = true
ratingVersionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8).isActive = true
ratingVersionLabel.heightAnchor.constraint(equalToConstant: 24).isActive = true
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
NSLayoutConstraint.activate([
authorLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
authorLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
authorLabel.topAnchor.constraint(equalTo: ratingVersionLabel.bottomAnchor, constant: 8),
authorLabel.heightAnchor.constraint(equalToConstant: 24),
commentLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
commentLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
commentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
commentLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 24),
ratingVersionLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
ratingVersionLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
ratingVersionLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
ratingVersionLabel.heightAnchor.constraint(equalToConstant: 24),
titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8),
titleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
titleLabel.topAnchor.constraint(equalTo: authorLabel.bottomAnchor, constant: 8),
titleLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 72),
])
}
}
// 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,
didSelectRowAt indexPath: IndexPath
) {
let details = DetailsViewController(review: viewModel.items[indexPath.row])
let details = DetailsViewController(viewModel.items[indexPath.row])
tableView.deselectRow(
at: indexPath,