/
ImageCell.swift
357 lines (305 loc) · 11.7 KB
/
ImageCell.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//
// ImageCell.swift
// react-native-simple-grid
//
// Created by Vittorio Cellucci on 2022-08-08.
//
import Foundation
class ImageCell: UIView, ConstraintCellProtocol, SelectionsListener {
var id: Int = 0
var dynamicWidth = NSLayoutConstraint()
let contextMenu = ContextMenu()
weak var imageView: UIImageView?
weak var delegate: ExpandedCellProtocol?
var workItem: URLSessionDataTask?
var imagedata: Data?
var representation: Representation?
var menuTranslations: MenuTranslations?
var cell: DataCell?
var selectionsEngine: SelectionsEngine?
var selectionBand: SelectionBand?
var selected = false
let selectedBackgroundColor = ColorParser.fromCSS(cssString: "#009845")
var prevBackgroundColor = UIColor.clear
init( selectionBand: SelectionBand?) {
self.selectionBand = selectionBand
super.init(frame: CGRect.zero)
if let selectionBand = self.selectionBand {
selectionBand.notificationCenter.addObserver(self, selector: #selector(onTappedSelectionBand), name: Notification.Name.onTappedSelectionBand, object: nil)
selectionBand.notificationCenter.addObserver(self, selector: #selector(onSelectionDragged), name: Notification.Name.onSelectionDragged, object: nil)
}
showMenus()
}
@objc func onTappedSelectionBand(notificaiton: Notification) {
guard let point = notificaiton.object as? CGPoint else {return}
guard let selectionBand = self.selectionBand else { return }
let hitTestPoint = selectionBand.convert(point, to: self)
if self.frame.contains(hitTestPoint) {
toggleSelection()
if !selected {
selectionBand.notificationCenter.post(name: Notification.Name.onClearSelectionBand, object: nil)
}
}
}
@objc func onSelectionDragged(notificaiton: Notification) {
if !selected {
guard let envelope = notificaiton.object as? SelectionBandEnvelope else {return}
guard let selectionBand = self.selectionBand else { return }
if envelope.sender === selectionBand {
let convertedFrame = convertLocalFrameToSelectionBandFrame(selectionBand)
if envelope.frame.contains(convertedFrame) {
addToSelections()
}
}
}
}
fileprivate func addToSelections() {
guard let selectionsEngine = self.selectionsEngine else { return }
let sig = SelectionsEngine.buildSelectionSignator(from: cell!)
selectionsEngine.addSelection(sig)
selected = true
updateBackground()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var canBecomeFirstResponder: Bool {
return true
}
func getDynamicWidth() -> NSLayoutConstraint {
return dynamicWidth
}
func setDynamicWidth(_ newVal: NSLayoutConstraint, value: Double) {
dynamicWidth = newVal
}
func getLineCount(columnWidth: Double) -> Int {
return 1
}
func showMenus() {
isUserInteractionEnabled = true
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(showMenu))
self.addGestureRecognizer(longPress)
}
@objc func showMenu(_ sender: UILongPressGestureRecognizer) {
self.becomeFirstResponder()
contextMenu.menuTranslations = self.menuTranslations
contextMenu.cell = self.cell
contextMenu.showMenu(sender, view: self)
}
@objc func handleCopy(_ controller: UIMenuController) {
let format = UIGraphicsImageRendererFormat()
format.scale = UIScreen.main.scale
if let img = imageView?.image {
let board = UIPasteboard.general
board.image = img
}
controller.setMenuVisible(false, animated: true)
self.resignFirstResponder()
}
@objc func handleExpand(_ controller: UIMenuController) {
guard let cell = self.cell else { return }
guard let delegate = self.delegate else { return }
delegate.onExpandedCell(cell: cell)
self.resignFirstResponder()
}
func setData(data: DataCell, representedAs rep: Representation, index: Int?) {
self.representation = rep
self.cell = data
guard let urlString = getUrlString(data: data, representedAs: rep, index: index) else { return }
guard let url = URL(string: urlString) else {return}
self.imagedata = nil;
if let pendingWorkItem = self.workItem {
pendingWorkItem.cancel();
}
let task = URLSession.shared.dataTask(with: url, completionHandler: { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async {
self.imagedata = data
self.setNeedsLayout()
}
})
self.workItem = task;
task.resume()
}
func getUrlString(data: DataCell, representedAs rep: Representation, index: Int?) -> String? {
var urlString = ""
if(rep.imageSetting == "label") {
urlString = data.qText ?? ""
return urlString
}
guard let qAttrExps = data.qAttrExps else {return nil}
guard let qValues = qAttrExps.qValues else {return nil}
guard let attrIndex = index else { return nil }
if qValues.count > 0 {
guard let urlString = qValues[attrIndex].qText else {return nil}
return urlString
}
return nil
}
override func layoutSubviews() {
super.layoutSubviews()
guard let data = imagedata else { return }
if self.imageView == nil {
let im = UIImageView(frame: CGRect.zero)
let image = UIImage(data: data)
im.image = image
addSubview(im)
setupConstraints(imageView: im)
self.imageView = im
displayImage()
} else {
updateContentScaleFactorIfNeeded()
}
}
func setupConstraints(imageView: UIImageView) {
imageView.translatesAutoresizingMaskIntoConstraints = false
guard let rep = self.representation else { return }
if(rep.imageSize == "fitHeight" ) {
if let image = imageView.image {
let aspectRatio = image.size.width/image.size.height
let height = self.frame.height
let width = height * aspectRatio
if rep.imagePosition == "centerCenter" {
imageView.fitToView(self)
} else {
let leadingAnchor = rep.imagePosition == "topCenter" ?
imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
let constraints = [
leadingAnchor,
imageView.heightAnchor.constraint(equalToConstant: height),
imageView.widthAnchor.constraint(equalToConstant: width)
]
NSLayoutConstraint.activate(constraints)
addConstraints(constraints)
}
}
} else if rep.imageSize == "alwaysFit" {
if rep.imagePosition == "centerCenter" {
imageView.fitToView(self)
} else if let image = imageView.image {
let aspectRatio = (image.size.height/image.size.width)
let maxWidth = self.frame.height / aspectRatio
let width = imageView.widthAnchor.constraint(lessThanOrEqualToConstant: maxWidth)
let leading = rep.imagePosition == "topCenter" ? imageView.leadingAnchor.constraint(equalTo: self.leadingAnchor) :
imageView.leadingAnchor.constraint(greaterThanOrEqualTo: self.trailingAnchor, constant: -maxWidth)
var constraints = [
leading,
imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
imageView.topAnchor.constraint(equalTo: self.topAnchor),
imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
width,
]
// need to bind to the leading of imageView and keep trailing lower priority
if rep.imagePosition == "bottomCenter" {
let hardLeading = imageView.leadingAnchor.constraint(greaterThanOrEqualTo: self.leadingAnchor)
leading.priority = UILayoutPriority(999)
constraints.append(hardLeading)
}
NSLayoutConstraint.activate(constraints)
addConstraints(constraints)
}
}
else {
imageView.fitToView(self)
}
}
func displayImage() {
guard let rep = representation else { return }
guard let imageView = self.imageView else {return}
if(rep.imageSize == "fitHeight" || rep.imageSize == "alwaysFit") {
imageView.contentMode = .scaleAspectFit
} else if rep.imageSize == "fitWidth" {
imageView.contentMode = .scaleAspectFill
if rep.imagePosition == "centerRight" {
imageView.contentMode = .bottom
} else if rep.imagePosition == "centerLeft" {
imageView.contentMode = .top
}
imageView.clipsToBounds = true
}
self.setNeedsLayout()
imageView.setNeedsLayout()
DispatchQueue.main.async {
self.updateContentScaleFactorIfNeeded()
}
}
func updateContentScaleFactorIfNeeded() {
guard let imageView = self.imageView else { return }
guard let image = imageView.image else { return }
guard let rep = representation else { return }
if rep.imageSize == "fitWidth" && imageView.contentMode != .scaleAspectFill {
let widthScale = image.size.width/imageView.frame.width
let heightScale = image.size.height/imageView.frame.height
imageView.contentScaleFactor = min(widthScale, heightScale)
}
}
func makeSelectable(selectionsEngine: SelectionsEngine) {
isUserInteractionEnabled = true
self.selectionsEngine = selectionsEngine
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageCliked(_:)))
addGestureRecognizer(tapGesture)
selectionsEngine.addListener(listener: self)
}
@objc func imageCliked(_ sender: UITapGestureRecognizer) {
let menu = UIMenuController.shared
if menu.isMenuVisible {
menu.setMenuVisible(false, animated: true)
}
guard let selectionsEngine = self.selectionsEngine else {return}
guard let selectionBand = self.selectionBand else { return }
if selectionsEngine.canSelect(self.cell!) {
if !selected {
let convertedFrame = convertLocalFrameToSelectionBandFrame(selectionBand)
let envelope = SelectionBandEnvelope(convertedFrame, sender: self, colIdx: self.cell?.colIdx ?? -1.0)
selectionBand.handleActivation(envelope)
selectionBand.notificationCenter.post(name: Notification.Name.onTappedSelection, object: envelope)
}
toggleSelection()
}
}
fileprivate func toggleSelection() {
let sig = SelectionsEngine.buildSelectionSignator(from: cell!)
if let selectionsEngine = selectionsEngine {
selectionsEngine.toggleSelected(sig)
}
}
fileprivate func convertLocalFrameToSelectionBandFrame(_ selectionBand: UIView) -> CGRect {
let convertedFrame = convert(self.frame, from: self.superview)
// account for the 1 width colunm grabber line
return convert(convertedFrame, to: selectionBand).insetBy(dx: 0.5, dy: 0).offsetBy(dx: -0.5, dy: 0)
}
func toggleSelected(data: String) {
let sig = SelectionsEngine.signatureKey(from: data)
let comp = SelectionsEngine.signatureKey(from: cell!)
if sig == comp {
selected = !selected
updateBackground()
}
}
func clearSelected() {
selected = false
updateBackground()
}
func addedToSelection(data: String) {
let sig = SelectionsEngine.signatureKey(from: data)
let comp = SelectionsEngine.signatureKey(from: cell!)
if sig == comp {
selected = true
updateBackground()
}
}
fileprivate func updateBackground() {
animateBackgroundColor(to: selected ? selectedBackgroundColor : prevBackgroundColor)
}
fileprivate func animateBackgroundColor(to: UIColor) {
UIView.animate(withDuration: 0.3, animations: {
self.layer.backgroundColor = to.cgColor
})
}
deinit {
if let selectionBand = self.selectionBand {
selectionBand.notificationCenter.removeObserver(self.onTappedSelectionBand)
}
}
}