JCFileDownloadViewController.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // JCFileDownloadViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/7/24.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. class JCFileDownloadViewController: UIViewController {
  10. var message: JMSGMessage!
  11. var fileSize: String?
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. _init()
  15. }
  16. private lazy var _imageView: UIImageView = {
  17. var _imageView = UIImageView()
  18. _imageView.image = UIImage.loadImage("com_icon_file_file_75")
  19. return _imageView
  20. }()
  21. private lazy var _tipsLabel: UILabel = {
  22. var _tipsLabel = UILabel()
  23. _tipsLabel.font = UIFont.systemFont(ofSize: 12)
  24. _tipsLabel.textAlignment = .center
  25. _tipsLabel.textColor = UIColor(netHex: 0x999999)
  26. _tipsLabel.text = "该文件不支持预览,请下载原文件查看"
  27. return _tipsLabel
  28. }()
  29. private lazy var _downloadButton: UIButton = {
  30. var _downloadButton = UIButton()
  31. _downloadButton.setBackgroundImage(UIImage.createImage(color: UIColor(netHex: 0x2ECFCF), size: CGSize(width: 225, height: 40)), for: .normal)
  32. _downloadButton.addTarget(self, action: #selector(_downloadFile), for: .touchUpInside)
  33. _downloadButton.setTitle("下载(\(self.fileSize ?? "未知大小"))", for: .normal)
  34. return _downloadButton
  35. }()
  36. private lazy var _openButton: UIButton = {
  37. var _openButton = UIButton()
  38. _openButton.setBackgroundImage(UIImage.createImage(color: UIColor(netHex: 0x2ECFCF), size: CGSize(width: 225, height: 40)), for: .normal)
  39. _openButton.addTarget(self, action: #selector(_openFile), for: .touchUpInside)
  40. _openButton.setTitle("打开文件", for: .normal)
  41. _openButton.isHidden = true
  42. return _openButton
  43. }()
  44. private lazy var _fileNameLabel: UILabel = {
  45. var _fileNameLabel = UILabel()
  46. _fileNameLabel.font = UIFont.systemFont(ofSize: 16)
  47. _fileNameLabel.numberOfLines = 0
  48. _fileNameLabel.textAlignment = .center
  49. _fileNameLabel.text = self.title
  50. return _fileNameLabel
  51. }()
  52. private lazy var _progressView: UIProgressView = {
  53. var _progressView = UIProgressView(frame: .zero)
  54. _progressView.backgroundColor = UIColor(netHex: 0x72D635)
  55. _progressView.isHidden = true
  56. return _progressView
  57. }()
  58. private var _fileData: Data!
  59. private lazy var documentInteractionController = UIDocumentInteractionController()
  60. private func _init() {
  61. view.backgroundColor = UIColor(netHex: 0xE8EDF3)
  62. documentInteractionController.delegate = self
  63. view.addSubview(_imageView)
  64. view.addSubview(_fileNameLabel)
  65. view.addSubview(_tipsLabel)
  66. view.addSubview(_downloadButton)
  67. view.addSubview(_openButton)
  68. view.addConstraint(_JCLayoutConstraintMake(_imageView, .top, .equal, view, .top, 98 + 64))
  69. view.addConstraint(_JCLayoutConstraintMake(_imageView, .centerX, .equal, view, .centerX))
  70. view.addConstraint(_JCLayoutConstraintMake(_imageView, .width, .equal, nil, .notAnAttribute, 75))
  71. view.addConstraint(_JCLayoutConstraintMake(_imageView, .height, .equal, nil, .notAnAttribute, 75))
  72. view.addConstraint(_JCLayoutConstraintMake(_fileNameLabel, .top, .equal, _imageView, .bottom, 14))
  73. view.addConstraint(_JCLayoutConstraintMake(_fileNameLabel, .centerX, .equal, view, .centerX))
  74. view.addConstraint(_JCLayoutConstraintMake(_fileNameLabel, .width, .equal, nil, .notAnAttribute, 225))
  75. view.addConstraint(_JCLayoutConstraintMake(_fileNameLabel, .height, .equal, nil, .notAnAttribute, 45))
  76. view.addConstraint(_JCLayoutConstraintMake(_tipsLabel, .top, .equal, _fileNameLabel, .bottom, 14))
  77. view.addConstraint(_JCLayoutConstraintMake(_tipsLabel, .centerX, .equal, view, .centerX))
  78. view.addConstraint(_JCLayoutConstraintMake(_tipsLabel, .width, .equal, nil, .notAnAttribute, 225))
  79. view.addConstraint(_JCLayoutConstraintMake(_tipsLabel, .height, .equal, nil, .notAnAttribute, 7))
  80. view.addConstraint(_JCLayoutConstraintMake(_downloadButton, .top, .equal, _tipsLabel, .bottom, 30))
  81. view.addConstraint(_JCLayoutConstraintMake(_downloadButton, .centerX, .equal, view, .centerX))
  82. view.addConstraint(_JCLayoutConstraintMake(_downloadButton, .width, .equal, nil, .notAnAttribute, 225))
  83. view.addConstraint(_JCLayoutConstraintMake(_downloadButton, .height, .equal, nil, .notAnAttribute, 40))
  84. view.addConstraint(_JCLayoutConstraintMake(_openButton, .top, .equal, _tipsLabel, .bottom, 30))
  85. view.addConstraint(_JCLayoutConstraintMake(_openButton, .centerX, .equal, view, .centerX))
  86. view.addConstraint(_JCLayoutConstraintMake(_openButton, .width, .equal, nil, .notAnAttribute, 225))
  87. view.addConstraint(_JCLayoutConstraintMake(_openButton, .height, .equal, nil, .notAnAttribute, 40))
  88. }
  89. @objc func _openFile() {
  90. if let fileType = message.ex.fileType {
  91. let content = message.content as! JMSGFileContent
  92. switch fileType.fileFormat() {
  93. case .document:
  94. let vc = JCDocumentViewController()
  95. vc.title = self.title
  96. vc.fileData = _fileData
  97. vc.filePath = content.originMediaLocalPath
  98. vc.fileType = fileType
  99. navigationController?.pushViewController(vc, animated: true)
  100. case .video, .voice:
  101. let url = URL(fileURLWithPath: content.originMediaLocalPath ?? "")
  102. try! JCVideoManager.playVideo(data: Data(contentsOf: url), fileType, currentViewController: self)
  103. case .photo:
  104. let browserImageVC = JCImageBrowserViewController()
  105. let image = UIImage(contentsOfFile: content.originMediaLocalPath ?? "")
  106. browserImageVC.imageArr = [image!]
  107. browserImageVC.imgCurrentIndex = 0
  108. present(browserImageVC, animated: true) {}
  109. default:
  110. let url = URL(fileURLWithPath: content.originMediaLocalPath ?? "")
  111. documentInteractionController.url = url
  112. documentInteractionController.presentOptionsMenu(from: .zero, in: view, animated: true)
  113. }
  114. }
  115. }
  116. @objc func _downloadFile() {
  117. let content = message.content as! JMSGFileContent
  118. MBProgressHUD_JChat.showMessage(message: "下载中", toView: view)
  119. content.fileData { (data, id, error) in
  120. MBProgressHUD_JChat.hide(forView: self.view, animated: true)
  121. if error == nil {
  122. self._openButton.isHidden = false
  123. self._downloadButton.isHidden = true
  124. self._fileData = data
  125. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kUpdateFileMessage), object: nil, userInfo: [kUpdateFileMessage : self.message.msgId])
  126. } else {
  127. MBProgressHUD_JChat.show(text: "下载失败", view: self.view)
  128. }
  129. }
  130. }
  131. }
  132. extension JCFileDownloadViewController: UIDocumentInteractionControllerDelegate {
  133. func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
  134. return self
  135. }
  136. func documentInteractionControllerViewForPreview(_ controller: UIDocumentInteractionController) -> UIView? {
  137. return view
  138. }
  139. func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
  140. return view.frame
  141. }
  142. }