167 lines
4.7 KiB
Swift
167 lines
4.7 KiB
Swift
|
//
|
||
|
// ListViewController.swift
|
||
|
// CapFinder
|
||
|
//
|
||
|
// Created by User on 22.02.18.
|
||
|
// Copyright © 2018 User. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import UIKit
|
||
|
import SwiftyDropbox
|
||
|
|
||
|
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: - CollectionView
|
||
|
|
||
|
private var images = [UIImage?]()
|
||
|
|
||
|
var cap: Cap!
|
||
|
|
||
|
@IBOutlet weak var collection: UICollectionView!
|
||
|
|
||
|
// MARK: - Life cycle
|
||
|
|
||
|
override func viewDidLoad() {
|
||
|
super.viewDidLoad()
|
||
|
|
||
|
collection.dataSource = self
|
||
|
collection.delegate = self
|
||
|
}
|
||
|
|
||
|
override func viewWillAppear(_ animated: Bool) {
|
||
|
super.viewWillAppear(animated)
|
||
|
|
||
|
downloadImages()
|
||
|
}
|
||
|
|
||
|
// MARK: Image download
|
||
|
|
||
|
private func downloadImages() {
|
||
|
images = [UIImage?](repeating: nil, count: cap.count)
|
||
|
event("\(cap.count) images for cap \(cap.id)")
|
||
|
for number in 0..<cap.count {
|
||
|
cap.downloadImage(number) { image in
|
||
|
self.images[number] = image
|
||
|
self.collection.reloadItems(at: [IndexPath(row: number, section: 0)])
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// MARK: Select
|
||
|
|
||
|
private func selectedImage(nr: Int) {
|
||
|
guard images[nr] != nil else {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
let tempId = cap.count
|
||
|
let tempFile = "/Images/\(cap.id)/\(cap.id)-\(tempId).jpg"
|
||
|
let oldFile = "/Images/\(cap.id)/\(cap.id)-0.jpg"
|
||
|
let newFile = "/Images/\(cap.id)/\(cap.id)-\(nr).jpg"
|
||
|
guard oldFile != newFile else {
|
||
|
return
|
||
|
}
|
||
|
DropboxController.shared.move(file: oldFile, to: tempFile) { success in
|
||
|
guard success else {
|
||
|
self.error("Could not move \(oldFile) to \(tempFile)")
|
||
|
return
|
||
|
}
|
||
|
DropboxController.shared.move(file: newFile, to: oldFile) { success in
|
||
|
guard success else {
|
||
|
self.error("Could not move \(newFile) to \(oldFile)")
|
||
|
return
|
||
|
}
|
||
|
DropboxController.shared.move(file: tempFile, to: newFile) { success in
|
||
|
if !success {
|
||
|
self.error("Could not move \(tempFile) to \(newFile)")
|
||
|
}
|
||
|
self.finish(with: nr)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func finish(with nr: Int) {
|
||
|
let image = images[nr]!
|
||
|
guard cap.save(mainImage: image) else {
|
||
|
return
|
||
|
}
|
||
|
event("Successfully switched image")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 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]
|
||
|
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 {
|
||
|
|
||
|
static let logToken = "[ImageSelector]"
|
||
|
}
|
||
|
|
||
|
|