Caps-iOS/CapCollector/Presentation/GridViewController.swift

361 lines
10 KiB
Swift
Raw Normal View History

2019-03-15 13:19:19 +01:00
//
// GridViewController.swift
// CapCollector
//
// Created by Christoph on 07.01.19.
// Copyright © 2019 CH. All rights reserved.
//
import UIKit
class GridViewController: UIViewController {
/// The number of horizontal pixels for each cap.
2019-03-15 13:19:19 +01:00
static let len: CGFloat = 60
private lazy var rowHeight = GridViewController.len * 0.866
private lazy var margin = GridViewController.len - rowHeight
private var myView: UIView!
private var canvasSize: CGSize = .zero
@IBOutlet weak var scrollView: UIScrollView!
2020-05-16 11:21:55 +02:00
/// A dictionary of the caps for the tiles
private var tiles = [Int]()
2020-05-16 11:21:55 +02:00
/// The name of the tile image
private var name: String = "default"
2020-05-16 11:21:55 +02:00
/// The number of caps horizontally.
private var columns = 40
/// A dictionary for the colors of the caps
private var colors = [Int : UIColor]()
/// The currently displaxed image views indexed by their tile ids
private var installedTiles = [Int : RoundedImageView]()
2020-05-16 11:21:55 +02:00
2019-03-15 13:19:19 +01:00
private var selectedTile: Int? = nil
private weak var selectionView: RoundedButton!
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
override var shouldAutorotate: Bool {
return true
}
private var isShowingColors = false
@IBAction func toggleAverageColor(_ sender: Any) {
isShowingColors = !isShowingColors
for (tile, view) in installedTiles {
if isShowingColors {
view.image = nil
view.backgroundColor = tileColor(tile: tile)
} else {
let id = tiles[tile]
if let image = app.storage.thumbnail(for: id) {
2020-05-16 11:21:55 +02:00
view.image = image
continue
}
self.downloadImage(cap: id, tile: tile)
}
}
}
func load(tileImage: Database.TileImage) {
let totalCount = app.database.capCount
let firstNewId = tileImage.caps.count + 1
if totalCount >= firstNewId {
self.tiles = tileImage.caps + (firstNewId...totalCount)
} else {
self.tiles = tileImage.caps
}
self.columns = tileImage.width
self.name = tileImage.name
}
private func saveTileImage() {
let tileImage = Database.TileImage(name: name, width: columns, caps: tiles)
guard app.database.save(tileImage: tileImage) else {
log("Failed to save tile image")
return
}
log("Tile image saved")
}
2019-03-15 13:19:19 +01:00
override func viewDidLoad() {
super.viewDidLoad()
colors = app.database.colors
2020-05-16 11:21:55 +02:00
2019-03-15 13:19:19 +01:00
let width = CGFloat(columns) * GridViewController.len + GridViewController.len / 2
let height = (CGFloat(tiles.count) / CGFloat(columns)).rounded(.up) * rowHeight + margin
2019-03-15 13:19:19 +01:00
canvasSize = CGSize(width: width, height: height)
myView = UIView(frame: CGRect(origin: .zero, size: canvasSize))
scrollView.addSubview(myView)
scrollView.contentSize = canvasSize
scrollView.delegate = self
scrollView.zoomScale = 0.5
scrollView.maximumZoomScale = 1
setZoomRange()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateTiles()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
myView.addGestureRecognizer(tapRecognizer)
}
override func viewDidLayoutSubviews() {
setZoomRange()
updateTiles()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
2020-05-16 11:21:55 +02:00
saveTileImage()
2020-05-16 11:21:55 +02:00
}
2019-03-15 13:19:19 +01:00
2020-05-16 11:21:55 +02:00
// MARK: Tiles
private func tileColor(tile: Int) -> UIColor? {
let id = tiles[tile]
return colors[id]
2019-03-15 13:19:19 +01:00
}
2020-05-16 11:21:55 +02:00
/**
Switch two tiles.
*/
private func switchTiles(_ lhs: Int, _ rhs: Int) -> Bool {
let temp = tiles[rhs]
tiles[rhs] = tiles[lhs]
tiles[lhs] = temp
return true
}
2019-03-15 13:19:19 +01:00
private func setZoomRange() {
let size = scrollView.frame.size
let a = size.width / canvasSize.width
let b = size.height / canvasSize.height
let scale = min(a,b)
scrollView.minimumZoomScale = min(a,b)
if scrollView.zoomScale < scale {
scrollView.setZoomScale(scale, animated: true)
}
}
@objc func handleTap(_ sender: UITapGestureRecognizer) {
let loc = sender.location(in: myView)
let y = loc.y
let s = y.truncatingRemainder(dividingBy: rowHeight)
let row = Int(y / rowHeight)
guard s > margin else {
return
}
2020-05-16 11:21:55 +02:00
let column: Int
2019-03-15 13:19:19 +01:00
if row.isEven {
2020-05-16 11:21:55 +02:00
column = Int(loc.x / GridViewController.len)
// Abort, if user tapped outside of the grid
if column >= columns {
clearTileSelection()
return
}
2019-03-15 13:19:19 +01:00
} else {
2020-05-16 11:21:55 +02:00
column = Int((loc.x - GridViewController.len / 2) / GridViewController.len)
2019-03-15 13:19:19 +01:00
}
handleTileTapped(tile: row * columns + Int(column))
}
private func handleTileTapped(tile: Int) {
if let selected = selectedTile {
switchTiles(oldTile: selected, newTile: tile)
} else {
showSelection(tile: tile)
}
}
private func showSelection(tile: Int) {
clearTileSelection()
if let view = installedTiles[tile] {
view.borderWidth = 3
view.borderColor = AppDelegate.tintColor
selectedTile = tile
} else {
selectedTile = nil
}
}
private func tileIsVisible(tile: Int, in rect: CGRect) -> Bool {
return rect.intersects(frame(for: tile))
}
private func makeTile(_ tile: Int) {
let view = RoundedImageView(frame: frame(for: tile))
myView.addSubview(view)
2020-05-16 11:21:55 +02:00
defer {
installedTiles[tile] = view
}
// Only set image if images are shown
2020-05-16 11:21:55 +02:00
guard !isShowingColors else {
view.backgroundColor = tileColor(tile: tile)
2020-05-16 11:21:55 +02:00
return
}
if let image = app.storage.thumbnail(for: tiles[tile]) {
2020-05-16 11:21:55 +02:00
view.image = image
return
}
downloadImage(tile: tile)
}
private func downloadImage(tile: Int) {
let id = tiles[tile]
downloadImage(cap: id, tile: tile)
}
private func downloadImage(cap id: Int, tile: Int) {
app.database.downloadMainImage(for: id) { success in
guard success else {
return
}
guard let view = self.installedTiles[tile] else {
self.log("No installed tile for downloaded image \(id)")
return
}
guard let image = app.storage.thumbnail(for: id) else {
self.log("Failed to load image for cap \(id) after successful download")
return
}
DispatchQueue.main.async {
guard self.isShowingColors else {
view.image = image
return
}
guard let color = image.averageColor else {
self.log("Failed to get average color from image for cap \(id)")
return
}
view.backgroundColor = color
self.colors[id] = color
}
2020-05-16 11:21:55 +02:00
}
2019-03-15 13:19:19 +01:00
}
2019-03-15 13:19:19 +01:00
private func frame(for tile: Int) -> CGRect {
let row = tile / columns
let column = tile - row * columns
let x = CGFloat(column) * GridViewController.len + (row.isEven ? 0 : GridViewController.len / 2)
let y = CGFloat(row) * rowHeight
return CGRect(x: x, y: y, width: GridViewController.len, height: GridViewController.len)
}
private func switchTiles(oldTile: Int, newTile: Int) {
guard oldTile != newTile else {
clearTileSelection()
return
}
2020-05-16 11:21:55 +02:00
guard switchTiles(oldTile, newTile) else {
clearTileSelection()
return
}
// Switch cap colors
2020-05-16 11:21:55 +02:00
let temp = installedTiles[oldTile]?.backgroundColor
installedTiles[oldTile]?.backgroundColor = installedTiles[newTile]?.backgroundColor
installedTiles[newTile]?.backgroundColor = temp
if !isShowingColors {
2020-05-16 11:21:55 +02:00
let temp = installedTiles[oldTile]?.image
installedTiles[oldTile]?.image = installedTiles[newTile]?.image
installedTiles[newTile]?.image = temp
2019-03-15 13:19:19 +01:00
}
clearTileSelection()
2019-03-15 13:19:19 +01:00
}
private func clearTileSelection() {
guard let tile = selectedTile else {
return
}
installedTiles[tile]?.borderWidth = 0
selectedTile = nil
}
private func showTiles(in rect: CGRect) {
for tile in 0..<tiles.count {
2020-05-16 11:21:55 +02:00
refresh(tile: tile, inVisibleRect: rect)
2019-03-15 13:19:19 +01:00
}
}
2020-05-16 11:21:55 +02:00
private func refresh(tile: Int, inVisibleRect rect: CGRect) {
if tileIsVisible(tile: tile, in: rect) {
show(tile: tile)
} else if let installed = installedTiles[tile] {
installed.removeFromSuperview()
installedTiles[tile] = nil
}
}
private func show(tile: Int) {
guard installedTiles[tile] == nil else {
2019-03-15 13:19:19 +01:00
return
}
2020-05-16 11:21:55 +02:00
makeTile(tile)
}
private func remove(tile: Int) {
installedTiles[tile]?.removeFromSuperview()
installedTiles[tile] = nil
}
private var visibleRect: CGRect {
2019-03-15 13:19:19 +01:00
let scale = scrollView.zoomScale
let offset = scrollView.contentOffset
let size = scrollView.visibleSize
let scaledOrigin = CGPoint(x: offset.x / scale, y: offset.y / scale)
let scaledSize = CGSize(width: size.width / scale, height: size.height / scale)
2020-05-16 11:21:55 +02:00
return CGRect(origin: scaledOrigin, size: scaledSize)
}
private func updateTiles() {
DispatchQueue.main.async {
self.showTiles(in: self.visibleRect)
}
2019-03-15 13:19:19 +01:00
}
}
extension GridViewController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myView
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateTiles()
}
}
private extension Int {
var isEven: Bool {
return self % 2 == 0
}
}
2020-05-16 11:21:55 +02:00
extension GridViewController: Logger { }