This repository was archived by the owner on Jun 7, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathFileTableViewCell.swift
More file actions
148 lines (124 loc) · 4.1 KB
/
Copy pathFileTableViewCell.swift
File metadata and controls
148 lines (124 loc) · 4.1 KB
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
//
// FileTableViewCell.swift
// Rocket.Chat
//
// Created by Filipe Alvarenga on 11/04/18.
// Copyright © 2018 Rocket.Chat. All rights reserved.
//
import UIKit
import AVFoundation
import FLAnimatedImage
import Nuke
class FileTableViewCell: UITableViewCell {
static let identifier = String(describing: FileTableViewCell.self)
@IBOutlet weak var filePreview: FLAnimatedImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var username: UILabel!
@IBOutlet weak var uploadedAt: UILabel!
@IBOutlet weak var playOverlay: UIImageView!
var file: File! {
didSet {
updateFileData()
}
}
override func awakeFromNib() {
super.awakeFromNib()
filePreview.layer.cornerRadius = 4
filePreview.layer.borderWidth = 0.5
filePreview.layer.borderColor = UIColor.lightGray.cgColor
}
func updateFileData() {
name.text = file.name
username.text = "\(file.username)"
uploadedAt.text = file.uploadedAt?.formatted()
guard let fileURL = file.fullFileURL() else {
filePreview.animatedImage = nil
filePreview.image = nil
return
}
if file.isImage {
updateImage(withURL: fileURL)
return
}
if file.isVideo {
updateVideo(withURL: fileURL)
return
}
if file.isAudio {
updateAudio(withURL: fileURL)
return
}
if file.isDocument {
updateDocument(withURL: fileURL)
return
}
}
func updateImage(withURL url: URL) {
let options = ImageLoadingOptions(
failureImage: #imageLiteral(resourceName: "Resource Unavailable"),
contentModes: .init(
success: .scaleAspectFill,
failure: .scaleAspectFit,
placeholder: .scaleAspectFit
)
)
ImageManager.loadImage(with: url, options: options, into: filePreview)
}
func updateVideo(withURL url: URL) {
playOverlay.isHidden = false
guard let thumbURL = file.videoThumbPath else { return }
if let imageData = try? Data(contentsOf: thumbURL) {
if let thumbnail = UIImage(data: imageData) {
filePreview.image = thumbnail
return
}
}
DispatchQueue.global(qos: .background).async {
let asset = AVAsset(url: url)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 1, timescale: 1)
do {
let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: imageRef)
try thumbnail.pngData()?.write(to: thumbURL, options: .atomic)
DispatchQueue.main.async {
self.filePreview.image = thumbnail
}
} catch {
DispatchQueue.main.async {
self.filePreview.image = nil
}
}
}
}
func updateAudio(withURL url: URL) {
accessoryType = .disclosureIndicator
filePreview.contentMode = .scaleAspectFit
filePreview.image = #imageLiteral(resourceName: "audio")
}
func updateDocument(withURL url: URL) {
filePreview.contentMode = .scaleAspectFit
filePreview.image = #imageLiteral(resourceName: "icon_file")
}
override func prepareForReuse() {
super.prepareForReuse()
name.text = ""
username.text = ""
uploadedAt.text = ""
playOverlay.isHidden = true
accessoryType = .none
filePreview.contentMode = .scaleAspectFill
filePreview.animatedImage = nil
filePreview.image = nil
Nuke.cancelRequest(for: filePreview)
}
}
// MARK: Themeable
extension FileTableViewCell {
override func applyTheme() {
super.applyTheme()
guard let theme = theme else { return }
filePreview.tintColor = theme.titleText
}
}