137 lines
4.4 KiB
Swift
137 lines
4.4 KiB
Swift
//
|
|
// FeedItemViewController.swift
|
|
// ReviewsFeed
|
|
//
|
|
// Created by Dmitrii Ivanov on 21/07/2020.
|
|
// Copyright © 2020 ING. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
final class FeedItemViewController: UIViewController {
|
|
|
|
// 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)
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
// MARK: UIViewController
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
setView()
|
|
setNavigationBar()
|
|
setLayout()
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
private extension FeedItemViewController {
|
|
|
|
// MARK: Functions
|
|
func setLayout() {
|
|
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),
|
|
])
|
|
}
|
|
|
|
func setNavigationBar() {
|
|
navigationController?.navigationBar.prefersLargeTitles = true
|
|
navigationController?.navigationBar.isTranslucent = true
|
|
|
|
navigationItem.title = "#\(String(item.id))"
|
|
}
|
|
|
|
func setView() {
|
|
view.backgroundColor = .white
|
|
|
|
view.addSubview(ratingVersionLabel)
|
|
view.addSubview(authorLabel)
|
|
view.addSubview(titleLabel)
|
|
view.addSubview(commentLabel)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Previews
|
|
@available(iOS 17.0, *)
|
|
#Preview("Feed Item with a review") {
|
|
UINavigationController(rootViewController: FeedItemViewController(.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..."
|
|
)))
|
|
}
|