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
|
//
// NCListCell.swift
// Nextcloud
//
// Created by Marino Faggiana on 24/10/2018.
// Copyright © 2018 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
class NCListCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProtocol {
@IBOutlet weak var imageItem: UIImageView!
@IBOutlet weak var imageSelect: UIImageView!
@IBOutlet weak var imageStatus: UIImageView!
@IBOutlet weak var imageFavorite: UIImageView!
@IBOutlet weak var imageLocal: UIImageView!
@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var labelInfo: UILabel!
@IBOutlet weak var imageShared: UIImageView!
@IBOutlet weak var buttonShared: UIButton!
@IBOutlet weak var imageMore: UIImageView!
@IBOutlet weak var buttonMore: UIButton!
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var separator: UIView!
@IBOutlet weak var imageItemLeftConstraint: NSLayoutConstraint!
@IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var titleTrailingConstraint: NSLayoutConstraint!
@IBOutlet weak var infoTrailingConstraint: NSLayoutConstraint!
private var objectId = ""
private var user = ""
weak var delegate: NCListCellDelegate?
var namedButtonMore = ""
var fileAvatarImageView: UIImageView? {
get { return imageShared }
}
var fileObjectId: String? {
get { return objectId }
set { objectId = newValue ?? "" }
}
var filePreviewImageView: UIImageView? {
get { return imageItem }
set { imageItem = newValue }
}
var fileUser: String? {
get { return user }
set { user = newValue ?? "" }
}
var fileTitleLabel: UILabel? {
get { return labelTitle }
set { labelTitle = newValue }
}
var fileInfoLabel: UILabel? {
get { return labelInfo }
set { labelInfo = newValue }
}
var fileProgressView: UIProgressView? {
get { return progressView }
set { progressView = newValue }
}
var fileSelectImage: UIImageView? {
get { return imageSelect }
set { imageSelect = newValue }
}
var fileStatusImage: UIImageView? {
get { return imageStatus }
set { imageStatus = newValue }
}
var fileLocalImage: UIImageView? {
get { return imageLocal }
set { imageLocal = newValue }
}
var fileFavoriteImage: UIImageView? {
get { return imageFavorite }
set { imageFavorite = newValue }
}
var fileSharedImage: UIImageView? {
get { return imageShared }
set { imageShared = newValue }
}
var fileMoreImage: UIImageView? {
get { return imageMore }
set { imageMore = newValue }
}
var cellSeparatorView: UIView? {
get { return separator }
set { separator = newValue }
}
override func awakeFromNib() {
super.awakeFromNib()
imageItem.layer.cornerRadius = 6
imageItem.layer.masksToBounds = true
// use entire cell as accessibility element
accessibilityHint = nil
accessibilityLabel = nil
accessibilityValue = nil
isAccessibilityElement = true
progressView.tintColor = NCBrandColor.shared.brandElement
progressView.transform = CGAffineTransform(scaleX: 1.0, y: 0.5)
progressView.trackTintColor = .clear
let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
longPressedGesture.minimumPressDuration = 0.5
longPressedGesture.delegate = self
longPressedGesture.delaysTouchesBegan = true
self.addGestureRecognizer(longPressedGesture)
let longPressedGestureMore = UILongPressGestureRecognizer(target: self, action: #selector(longPressInsideMore(gestureRecognizer:)))
longPressedGestureMore.minimumPressDuration = 0.5
longPressedGestureMore.delegate = self
longPressedGestureMore.delaysTouchesBegan = true
buttonMore.addGestureRecognizer(longPressedGestureMore)
separator.backgroundColor = NCBrandColor.shared.separator
separatorHeightConstraint.constant = 0.5
labelTitle.text = ""
labelInfo.text = ""
labelTitle.textColor = NCBrandColor.shared.label
labelInfo.textColor = NCBrandColor.shared.systemGray
}
override func prepareForReuse() {
super.prepareForReuse()
imageItem.backgroundColor = nil
accessibilityHint = nil
accessibilityLabel = nil
accessibilityValue = nil
}
override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
return nil
}
@IBAction func touchUpInsideShare(_ sender: Any) {
delegate?.tapShareListItem(with: objectId, sender: sender)
}
@IBAction func touchUpInsideMore(_ sender: Any) {
delegate?.tapMoreListItem(with: objectId, namedButtonMore: namedButtonMore, image: imageItem.image, sender: sender)
}
@objc func longPressInsideMore(gestureRecognizer: UILongPressGestureRecognizer) {
delegate?.longPressMoreListItem(with: objectId, namedButtonMore: namedButtonMore, gestureRecognizer: gestureRecognizer)
}
@objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
delegate?.longPressListItem(with: objectId, gestureRecognizer: gestureRecognizer)
}
fileprivate func setA11yActions() {
let moreName = namedButtonMore == NCGlobal.shared.buttonMoreStop ? "_cancel_" : "_more_"
self.accessibilityCustomActions = [
UIAccessibilityCustomAction(
name: NSLocalizedString("_share_", comment: ""),
target: self,
selector: #selector(touchUpInsideShare)),
UIAccessibilityCustomAction(
name: NSLocalizedString(moreName, comment: ""),
target: self,
selector: #selector(touchUpInsideMore))
]
}
func titleInfoTrailingFull() {
titleTrailingConstraint.constant = 10
infoTrailingConstraint.constant = 10
}
func titleInfoTrailingDefault() {
titleTrailingConstraint.constant = 90
infoTrailingConstraint.constant = 90
}
func setButtonMore(named: String, image: UIImage) {
namedButtonMore = named
imageMore.image = image
setA11yActions()
}
func hideButtonMore(_ status: Bool) {
imageMore.isHidden = status
buttonMore.isHidden = status
}
func hideButtonShare(_ status: Bool) {
imageShared.isHidden = status
buttonShared.isHidden = status
}
func hideSeparator(_ status: Bool) {
separator.isHidden = status
}
func selectMode(_ status: Bool) {
if status {
imageItemLeftConstraint.constant = 45
imageSelect.isHidden = false
accessibilityCustomActions = nil
} else {
imageItemLeftConstraint.constant = 10
imageSelect.isHidden = true
backgroundView = nil
setA11yActions()
}
}
func selected(_ status: Bool) {
if status {
var blurEffect: UIVisualEffect?
var blurEffectView: UIView?
imageSelect.image = NCBrandColor.cacheImages.checkedYes
if traitCollection.userInterfaceStyle == .dark {
blurEffect = UIBlurEffect(style: .dark)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView?.backgroundColor = .black
} else {
blurEffect = UIBlurEffect(style: .extraLight)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView?.backgroundColor = .lightGray
}
blurEffectView?.frame = self.bounds
blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
backgroundView = blurEffectView
separator.isHidden = true
} else {
imageSelect.image = NCBrandColor.cacheImages.checkedNo
backgroundView = nil
separator.isHidden = false
}
}
func writeInfoDateSize(date: NSDate, size: Int64) {
labelInfo.text = CCUtility.dateDiff(date as Date) + " · " + CCUtility.transformedSize(size)
}
func setAccessibility(label: String, value: String) {
accessibilityLabel = label
accessibilityValue = value
}
}
protocol NCListCellDelegate: AnyObject {
func tapShareListItem(with objectId: String, sender: Any)
func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any)
func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer)
func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer)
}
// optional func
extension NCListCellDelegate {
func tapShareListItem(with objectId: String, sender: Any) {}
func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, sender: Any) {}
func longPressMoreListItem(with objectId: String, namedButtonMore: String, gestureRecognizer: UILongPressGestureRecognizer) {}
func longPressListItem(with objectId: String, gestureRecognizer: UILongPressGestureRecognizer) {}
}
// MARK: - List Layout
class NCListLayout: UICollectionViewFlowLayout {
var itemHeight: CGFloat = 60
// MARK: - View Life Cycle
override init() {
super.init()
sectionHeadersPinToVisibleBounds = false
minimumInteritemSpacing = 0
minimumLineSpacing = 1
self.scrollDirection = .vertical
self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var itemSize: CGSize {
get {
if let collectionView = collectionView {
let itemWidth: CGFloat = collectionView.frame.width
return CGSize(width: itemWidth, height: self.itemHeight)
}
// Default fallback
return CGSize(width: 100, height: 100)
}
set {
super.itemSize = newValue
}
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
return proposedContentOffset
}
}
|