2019-03-15 13:19:19 +01:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
// MARK: - Variables
|
|
|
|
|
|
|
|
private var titleLabel: UILabel!
|
|
|
|
|
|
|
|
private var subtitleLabel: UILabel!
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
private var images = [UIImage?]()
|
|
|
|
|
|
|
|
var cap: Cap!
|
|
|
|
|
|
|
|
@IBOutlet weak var collection: UICollectionView!
|
2020-05-16 11:21:55 +02:00
|
|
|
|
|
|
|
private var titleText: String {
|
|
|
|
"Cap \(cap.id) (\(cap.count) images)"
|
|
|
|
}
|
|
|
|
|
|
|
|
private var subtitleText: String {
|
|
|
|
cap.name
|
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
// MARK: - Life cycle
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
collection.dataSource = self
|
|
|
|
collection.delegate = self
|
2020-05-16 11:21:55 +02:00
|
|
|
|
2019-03-15 13:19:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
downloadImages()
|
|
|
|
}
|
2020-05-16 11:21:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
// MARK: Image download
|
|
|
|
|
|
|
|
private func downloadImages() {
|
|
|
|
images = [UIImage?](repeating: nil, count: cap.count)
|
2020-05-16 11:21:55 +02:00
|
|
|
log("\(cap.count) images for cap \(cap.id)")
|
2020-06-18 22:55:51 +02:00
|
|
|
if let image = app.storage.image(for: cap.id) {
|
2019-07-17 11:10:07 +02:00
|
|
|
self.images[0] = image
|
|
|
|
self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)])
|
|
|
|
} else {
|
2020-06-18 22:55:51 +02:00
|
|
|
app.database.downloadMainImage(for: cap.id) { success in
|
|
|
|
guard success, let image = app.storage.image(for: self.cap.id) else {
|
|
|
|
return
|
|
|
|
}
|
2020-05-16 11:21:55 +02:00
|
|
|
self.images[0] = image
|
2020-06-18 22:55:51 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.collection.reloadItems(at: [IndexPath(row: 0, section: 0)])
|
|
|
|
}
|
2019-07-17 11:10:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
guard cap.count > 0 else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for number in 1..<cap.count {
|
2020-06-18 22:55:51 +02:00
|
|
|
app.database.downloadImage(for: cap.id, version: number) { success in
|
|
|
|
guard success, let image = app.storage.image(for: self.cap.id, version: number) else {
|
2020-05-16 11:21:55 +02:00
|
|
|
return
|
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
self.images[number] = image
|
2020-06-18 22:55:51 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.collection.reloadItems(at: [IndexPath(row: number, section: 0)])
|
|
|
|
}
|
2019-03-15 13:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Select
|
|
|
|
|
|
|
|
private func selectedImage(nr: Int) {
|
2020-05-16 11:21:55 +02:00
|
|
|
app.database.setMainImage(of: cap.id, to: nr)
|
2019-03-15 13:19:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
cell.capView.image = images[indexPath.row] ?? UIImage(named: "launch")
|
2019-03-15 13:19:19 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
extension ImageSelector: Logger { }
|
2019-03-15 13:19:19 +01:00
|
|
|
|
|
|
|
|