-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathIceCreamsViewController.swift
115 lines (82 loc) · 4.06 KB
/
IceCreamsViewController.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
114
115
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
A `UICollectionViewController` that displays the history of ice creams as well as a cell that can be tapped to start the process of creating a new ice cream.
*/
import UIKit
class IceCreamsViewController: UICollectionViewController {
// MARK: Types
/// An enumeration that represents an item in the collection view.
enum CollectionViewItem {
case iceCream(IceCream)
case create
}
// MARK: Properties
static let storyboardIdentifier = "IceCreamsViewController"
weak var delegate: IceCreamsViewControllerDelegate?
private let items: [CollectionViewItem]
private let stickerCache = IceCreamStickerCache.cache
// MARK: Initialization
required init?(coder aDecoder: NSCoder) {
// Map the previously completed ice creams to an array of `CollectionViewItem`s.
let reversedHistory = IceCreamHistory.load().reversed()
var items: [CollectionViewItem] = reversedHistory.map { .iceCream($0) }
// Add `CollectionViewItem` that the user can tap to start building a new ice cream.
items.insert(.create, at: 0)
self.items = items
super.init(coder: aDecoder)
}
// MARK: UICollectionViewDataSource
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let item = items[indexPath.row]
// The item's type determines which type of cell to return.
switch item {
case .iceCream(let iceCream):
return dequeueIceCreamCell(for: iceCream, at: indexPath)
case .create:
return dequeueIceCreamOutlineCell(at: indexPath)
}
}
// MARK: UICollectionViewDelegate
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = items[indexPath.row]
switch item {
case .create:
delegate?.iceCreamsViewControllerDidSelectAdd(self)
default:
break
}
}
// MARK: Convenience
private func dequeueIceCreamCell(for iceCream: IceCream, at indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: IceCreamCell.reuseIdentifier, for: indexPath) as? IceCreamCell else { fatalError("Unable to dequeue am IceCreamCell") }
cell.representedIceCream = iceCream
// Use a placeholder sticker while we fetch the real one from the cache.
let cache = IceCreamStickerCache.cache
cell.stickerView.sticker = cache.placeholderSticker
// Fetch the sticker for the ice cream from the cache.
cache.sticker(for: iceCream) { sticker in
OperationQueue.main.addOperation {
// If the cell is still showing the same ice cream, update its sticker view.
guard cell.representedIceCream == iceCream else { return }
cell.stickerView.sticker = sticker
}
}
return cell
}
private func dequeueIceCreamOutlineCell(at indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView?.dequeueReusableCell(withReuseIdentifier: IceCreamOutlineCell.reuseIdentifier, for: indexPath) as? IceCreamOutlineCell else { fatalError("Unable to dequeue a IceCreamOutlineCell") }
return cell
}
}
/**
A delegate protocol for the `IceCreamsViewController` class.
*/
protocol IceCreamsViewControllerDelegate: class {
/// Called when a user choses to add a new `IceCream` in the `IceCreamsViewController`.
func iceCreamsViewControllerDidSelectAdd(_ controller: IceCreamsViewController)
}