FileViewController.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // FileViewController.swift
  3. // JChat
  4. //
  5. // Created by 邓永豪 on 2017/8/28.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. enum FileType {
  10. case doc
  11. case music
  12. case video
  13. case other
  14. }
  15. class FileViewController: UIViewController {
  16. var isEditModel: Bool {
  17. get {
  18. return isEdit
  19. } set {
  20. isEdit = newValue
  21. selectMessages = []
  22. tableView.reloadData()
  23. }
  24. }
  25. var fileType = FileType.doc // default
  26. var messages: [JMSGMessage] = []
  27. var selectMessages: [JMSGMessage] = []
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. _init()
  31. }
  32. func reloadDate() {
  33. sortMessage()
  34. tableView.reloadData()
  35. }
  36. fileprivate var isEdit = false
  37. fileprivate var tableView: UITableView = UITableView(frame: .zero, style: .grouped)
  38. fileprivate lazy var data: Dictionary<String, [JMSGMessage]> = Dictionary()
  39. fileprivate lazy var keys: [String] = []
  40. fileprivate lazy var documentInteractionController = UIDocumentInteractionController()
  41. private func _init() {
  42. view.backgroundColor = UIColor(netHex: 0xe8edf3)
  43. tableView.backgroundColor = UIColor(netHex: 0xe8edf3)
  44. tableView.delegate = self
  45. tableView.dataSource = self
  46. tableView.separatorStyle = .none
  47. tableView.register(FileCell.self, forCellReuseIdentifier: "FileCell")
  48. view.addSubview(tableView)
  49. view.addConstraint(_JCLayoutConstraintMake(tableView, .left, .equal, view, .left))
  50. view.addConstraint(_JCLayoutConstraintMake(tableView, .top, .equal, view, .top))
  51. view.addConstraint(_JCLayoutConstraintMake(tableView, .right, .equal, view, .right))
  52. view.addConstraint(_JCLayoutConstraintMake(tableView, .bottom, .equal, view, .bottom))
  53. }
  54. func sortMessage() {
  55. keys.removeAll()
  56. data.removeAll()
  57. for message in messages {
  58. let formatter = DateFormatter()
  59. formatter.dateFormat = "yyyy-MM";
  60. let date = Date(timeIntervalSince1970: TimeInterval(message.timestamp.intValue / 1000))
  61. let key = formatter.string(from: date)
  62. var array = data[key]
  63. if array == nil {
  64. array = [message]
  65. } else {
  66. array?.append(message)
  67. }
  68. if !keys.contains(key) {
  69. keys.append(key)
  70. }
  71. data[key] = array
  72. }
  73. keys = keys.sorted(by: { (str1, str2) -> Bool in
  74. str1 > str2
  75. })
  76. }
  77. }
  78. //Mark: -
  79. extension FileViewController: UITableViewDelegate, UITableViewDataSource {
  80. func numberOfSections(in tableView: UITableView) -> Int {
  81. return keys.count
  82. }
  83. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  84. return data[keys[section]]!.count
  85. }
  86. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  87. return keys[section]
  88. }
  89. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  90. return 84
  91. }
  92. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  93. return 26
  94. }
  95. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  96. return 0.001
  97. }
  98. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  99. return tableView.dequeueReusableCell(withIdentifier: "FileCell", for: indexPath)
  100. }
  101. func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
  102. guard let cell = cell as? FileCell else {
  103. return
  104. }
  105. let message = data[keys[indexPath.section]]![indexPath.row]
  106. let content = message.content as! JMSGFileContent
  107. var image = UIImage()
  108. switch fileType {
  109. case .doc:
  110. image = UIImage.loadImage("com_icon_file_file")!
  111. case .music:
  112. image = UIImage.loadImage("com_icon_file_music")!
  113. case .video:
  114. image = UIImage.loadImage("com_icon_file_video")!
  115. default:
  116. image = UIImage.loadImage("com_icon_file_other")!
  117. }
  118. let file = File(image, content.fileName, message.ex.fileSize ?? "", "\(message.fromName) \(keys[indexPath.section])")
  119. cell.bindData(file)
  120. cell.isEditMode = isEdit
  121. if !isEditModel {
  122. cell.isSelectImage = false
  123. }
  124. }
  125. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  126. tableView.deselectRow(at: indexPath, animated: true)
  127. guard let cell = tableView.cellForRow(at: indexPath) as? FileCell else {
  128. return
  129. }
  130. let message = data[keys[indexPath.section]]![indexPath.row]
  131. if cell.isEditMode {
  132. if cell.isSelectImage {
  133. selectMessages = selectMessages.filter({ (m) -> Bool in
  134. message.msgId != m.msgId
  135. })
  136. } else {
  137. selectMessages.append(message)
  138. }
  139. cell.isSelectImage = !cell.isSelectImage
  140. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "kDidSelectFileMessage"), object: nil)
  141. } else {
  142. let content = message.content as! JMSGFileContent
  143. let type = message.ex.fileType
  144. let fileName = content.fileName
  145. switch fileType {
  146. case .doc:
  147. content.fileData({ (data, msgId, error) in
  148. let vc = JCDocumentViewController()
  149. vc.title = fileName
  150. vc.fileData = data
  151. vc.filePath = content.originMediaLocalPath
  152. vc.fileType = type
  153. DispatchQueue.main.async {
  154. self.navigationController?.pushViewController(vc, animated: true)
  155. }
  156. })
  157. case .video, .music:
  158. let url = URL(fileURLWithPath: content.originMediaLocalPath ?? "")
  159. try! JCVideoManager.playVideo(data: Data(contentsOf: url), type ?? "", currentViewController: self)
  160. default:
  161. let url = URL(fileURLWithPath: content.originMediaLocalPath ?? "")
  162. documentInteractionController.url = url
  163. documentInteractionController.presentOptionsMenu(from: .zero, in: self.view, animated: true)
  164. }
  165. }
  166. }
  167. }
  168. extension FileViewController: UIDocumentInteractionControllerDelegate {
  169. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  170. return self
  171. }
  172. func documentInteractionControllerViewForPreview(_ controller: UIDocumentInteractionController) -> UIView? {
  173. return view
  174. }
  175. func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
  176. return view.frame
  177. }
  178. }