Caps-iOS/CapCollector/Presentation/ImageSelector.swift
2021-01-10 16:11:31 +01:00

185 lines
5.3 KiB
Swift

//
// 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)")
for version in 0..<cap.count {
if let image = app.storage.image(for: cap.id, version: version) {
log("Image \(version) already downloaded")
set(image, for: version)
} else {
log("Downloading image \(version)")
app.database.downloadImage(for: cap.id, version: version) { image in
self.set(image, for: version)
if image != nil {
self.log("Downloaded version \(version)")
} else {
self.log("Failed to download image \(version)")
}
}
}
}
}
private func set(_ image: UIImage?, for version: Int) {
self.images[version] = image
DispatchQueue.main.async {
self.collection.reloadItems(at: [IndexPath(row: version, section: 0)])
}
}
// MARK: Select
private func selectedImage(nr: Int) {
app.database.setMainImage(of: cap.id, to: nr)
}
}
// MARK: - UICollectionViewDataSource
extension ImageSelector: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> 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 { }