// // ListViewController.swift // CapFinder // // Created by User on 22.02.18. // Copyright © 2018 User. All rights reserved. // import UIKit class ImageSelector: UIViewController { // MARK: - Constants /// The number of items per row private let itemsPerRow: CGFloat = 3 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait } override var shouldAutorotate: Bool { return false } // MARK: - Variables private var titleLabel: UILabel! private var subtitleLabel: UILabel! private var images = [UIImage?]() var cap: Cap! @IBOutlet weak var collection: UICollectionView! private var titleText: String { "Cap \(cap.id) (\(cap.count) images)" } private var subtitleText: String { cap.name } // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() collection.dataSource = self collection.delegate = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) downloadImages() } override func didMove(toParent parent: UIViewController?) { super.didMove(toParent: parent) guard parent != nil && self.navigationItem.titleView == nil else { return } initNavigationItemTitleView() } private func initNavigationItemTitleView() { self.titleLabel = UILabel() titleLabel.text = titleText titleLabel.font = .preferredFont(forTextStyle: .headline) titleLabel.textColor = .label self.subtitleLabel = UILabel() subtitleLabel.text = subtitleText subtitleLabel.font = .preferredFont(forTextStyle: .footnote) subtitleLabel.textColor = .secondaryLabel let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel]) stackView.distribution = .equalCentering stackView.alignment = .center stackView.axis = .vertical self.navigationItem.titleView = stackView } // MARK: Image download private func downloadImages() { images = [UIImage?](repeating: nil, count: cap.count) log("\(cap.count) images for cap \(cap.id)") if let image = cap.image { self.images[0] = image self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)]) } else { cap.downloadMainImage { image in self.images[0] = image self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)]) } } guard cap.count > 0 else { return } for number in 1.. Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return images.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell( withReuseIdentifier: "Image", for: indexPath) as! ImageCell cell.capView.image = images[indexPath.row] ?? UIImage(named: "launch") return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { selectedImage(nr: indexPath.row) navigationController?.popViewController(animated: true) } } // MARK: - UICollectionViewDelegateFlowLayout extension ImageSelector : UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let widthPerItem = collectionView.frame.width / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0 } } extension ImageSelector: Logger { }