-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageTableViewCell.swift
More file actions
182 lines (129 loc) · 5.57 KB
/
MessageTableViewCell.swift
File metadata and controls
182 lines (129 loc) · 5.57 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
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
//
// FlowMessagesTableView.swift
// FlowBook
//
// Created by Benjamin Afonso on 08/02/2017.
// Copyright © 2017 Benjamin Afonso. All rights reserved.
//
import UIKit
class MessageTableViewCell: UITableViewCell {
@IBOutlet weak var authorImage: RoundedImageView!
@IBOutlet weak var authorUsername: UILabel!
@IBOutlet weak var messageText: UILabel!
@IBOutlet weak var filesCollectionView: UICollectionView!
@IBOutlet weak var timeStampLabel: UILabel!
var delegate: messageTableDelegate?
var message: Message?
var files: NSSet?
var images: NSSet?
var author: User?
@IBOutlet weak var editedView: UIStackView!
@IBOutlet weak var editTime: UILabel!
@IBOutlet weak var editDate: UILabel!
override func awakeFromNib() {
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(MessageTableViewCell.swiped(sender:)))
addGestureRecognizer(swipeGesture)
self.filesCollectionView.delegate = self
self.filesCollectionView.dataSource = self
}
func setAuthor(author: User?) {
if let author = author {
self.authorImage.image = author.getImage()
self.authorUsername.text = author.getUsername()
self.author = author
}
}
func setFiles(files: NSSet) {
self.files = files
self.filesCollectionView.reloadData()
}
func setImages(images: NSSet) {
self.images = images
self.filesCollectionView.reloadData()
}
func setTimeStamp(time: NSDate?) {
if let time = time {
let date = self.getDate(date: time)
self.timeStampLabel.text = "\(date[2]):\(date[3])"
}
}
func setEdited(time: NSDate?) {
self.editedView.isHidden = false
if let time = time {
let date = self.getDate(date: time)
self.editDate.text = "\(date[0])/\(date[1])"
self.editTime.text = "\(date[2]):\(date[3])"
}
}
func setContent(message: String) {
self.messageText.text = message
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func visitProfile(_ sender: Any) {
if let rootController = self.parentViewController?.parent as? RootViewController {
rootController.visitProfile(ofUser: self.author!)
}
}
func swiped(sender: UISwipeGestureRecognizer) {
delegate?.swippedCell(cell: self)
}
private func getDate(date: NSDate) -> [String] {
let calendar = Calendar.current
let day = calendar.component(.day, from: date as Date)
let dayString = String(day)
let month = calendar.component(.month, from: date as Date)
let monthString = String(month)
let hour = calendar.component(.hour, from: date as Date)
let hourString = String(hour)
let minutes = calendar.component(.minute, from: date as Date)
var minuteString = String(minutes)
// Converts 1 digit minute to 2 digits (e.g: 9:9 -> 9:09)
if String(minutes).characters.count < 2 {
minuteString = "0"+minuteString
}
return [dayString, monthString, hourString, minuteString]
}
}
extension MessageTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let fileCell = self.filesCollectionView.dequeueReusableCell(withReuseIdentifier: "fileCell", for: indexPath) as! FileCollectionViewCell
if (indexPath.item >= (self.files?.count)!) {
let image = (self.images?.allObjects as! [Image])[indexPath.item-(self.files?.count)!]
//fileCell.fileImage.image = UIImage(data: image.image as! Data)
fileCell.setImage(image: image)
fileCell.targetButton.tag = indexPath.item
fileCell.targetButton.addTarget(self, action: #selector(previewImage(sender:)), for: .touchUpInside)
} else {
let file = (self.files?.allObjects as! [File])[indexPath.item]
fileCell.setFile(file: file)
fileCell.targetButton.tag = indexPath.item
fileCell.targetButton.addTarget(self, action: #selector(openLink(sender:)), for: .touchUpInside)
//fileCell.fileImage.image = UIImage(named: "file-icon")
}
return fileCell
}
@IBAction func previewImage(sender: UIButton) {
let image = self.images?.allObjects[sender.tag-(self.files?.count)!] as? Image
let uiImage = UIImage(data: image?.image as! Data)
if let uiImage = uiImage {
delegate?.previewImage(image: uiImage)
}
}
@IBAction func openLink(sender: UIButton) {
let file = self.files?.allObjects[sender.tag] as? File
let url = NSURL(string: (file?.link)!)
UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil)
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (self.files?.count)!+(self.images?.count)!
}
}
protocol messageTableDelegate {
func swippedCell(cell: MessageTableViewCell)
func previewImage(image: UIImage)
}