126 lines
4.1 KiB
Swift
Raw Normal View History

2020-07-21 12:29:54 +02:00
//
// DetailsViewController.swift
// ReviewsFeed
2020-07-21 12:29:54 +02:00
//
// Created by Dmitrii Ivanov on 21/07/2020.
// Copyright © 2020 ING. All rights reserved.
//
import UIKit
final class DetailsViewController: UIViewController {
// MARK: Constants
private let item: Review
// MARK: Outlets
private lazy var titleLabel = {
let label = UILabel()
2020-07-21 12:29:54 +02:00
label.font = UIFont.preferredFont(forTextStyle: .title3)
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = item.title
2020-07-21 12:29:54 +02:00
return label
}()
2020-07-21 12:29:54 +02:00
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
2020-07-21 12:29:54 +02:00
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: UIViewController
2020-07-21 12:29:54 +02:00
override func viewDidLoad() {
super.viewDidLoad()
setupView()
2020-07-21 12:29:54 +02:00
}
}
2020-07-21 12:29:54 +02:00
// MARK: - Helpers
private extension DetailsViewController {
// MARK: Functions
func setupView() {
view.backgroundColor = .white
2020-07-21 12:29:54 +02:00
view.addSubview(ratingVersionLabel)
view.addSubview(authorLabel)
view.addSubview(titleLabel)
view.addSubview(commentLabel)
2020-07-21 12:29:54 +02:00
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),
])
2020-07-21 12:29:54 +02:00
}
}
// 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..."
)))
2020-07-21 12:29:54 +02:00
}