-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionViewController.swift
113 lines (86 loc) · 4.01 KB
/
SelectionViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// SelectionViewController.swift
// Day97 - Project30
//
// Created by Jean-Yves Garcin on 29/06/2023.
//
import UIKit
class SelectionViewController: UITableViewController {
var items = [String]() // this is the array that will store the filenames to load
// var viewControllers = [UIViewController]() // create a cache of the detail view controllers for faster loading
var dirty = false
override func viewDidLoad() {
super.viewDidLoad()
title = "Reactionist"
tableView.rowHeight = 90
tableView.separatorStyle = .none
// Always get back a dequeued cell
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// load all the JPEGs into our array
let fm = FileManager.default
if let tempItems = try? fm.contentsOfDirectory(atPath: Bundle.main.resourcePath!) {
for item in tempItems {
if item.range(of: "Large") != nil {
items.append(item)
}
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if dirty {
// we've been marked as needing a counter reload, so reload the whole table
tableView.reloadData()
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return items.count * 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// This does mean you can't use different types of cell when you're using the newer register approach
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
// find the image for this cell, and load its thumbnail
let currentImage = items[indexPath.row % items.count]
let imageRootName = currentImage.replacingOccurrences(of: "Large", with: "Thumb")
let path = Bundle.main.path(forResource: imageRootName, ofType: nil)!
let original = UIImage(contentsOfFile: path)!
// Rendering will now use a much smaller size than the original image size
let renderRect = CGRect(origin: .zero, size: CGSize(width: 90, height: 90))
let renderer = UIGraphicsImageRenderer(size: renderRect.size)
let rounded = renderer.image { ctx in
// ctx.cgContext.setShadow(offset: .zero, blur: 200, color: UIColor.black.cgColor)
// ctx.cgContext.fillEllipse(in: CGRect(origin: .zero, size: original.size))
// ctx.cgContext.setShadow(offset: .zero, blur: 0, color: nil)
ctx.cgContext.addEllipse(in: renderRect)
ctx.cgContext.clip()
original.draw(in: renderRect)
}
cell.imageView?.image = rounded
// give the images a nice shadow to make them look a bit more dramatic
cell.imageView?.layer.shadowColor = UIColor.black.cgColor
cell.imageView?.layer.shadowOpacity = 1
cell.imageView?.layer.shadowRadius = 10
cell.imageView?.layer.shadowOffset = CGSize.zero
cell.imageView?.layer.shadowPath = UIBezierPath(ovalIn: renderRect).cgPath
// each image stores how often it's been tapped
let defaults = UserDefaults.standard
cell.textLabel?.text = "\(defaults.integer(forKey: currentImage))"
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = ImageViewController()
vc.image = items[indexPath.row % items.count]
vc.owner = self
// mark us as not needing a counter reload when we return
dirty = false
// add to our view controller cache and show
// viewControllers.append(vc)
navigationController!.pushViewController(vc, animated: true)
}
}