DocumentGalleryController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import UIKit
  2. import DcCore
  3. class DocumentGalleryController: UIViewController {
  4. private let fileMessageIds: [Int]
  5. private lazy var tableViews: UITableView = {
  6. let table = UITableView(frame: .zero, style: .grouped)
  7. table.register(DocumentGalleryFileCell.self, forCellReuseIdentifier: DocumentGalleryFileCell.reuseIdentifier)
  8. table.dataSource = self
  9. table.delegate = self
  10. table.rowHeight = 60
  11. return table
  12. }()
  13. private lazy var emptyStateView: EmptyStateLabel = {
  14. let label = EmptyStateLabel()
  15. label.text = String.localized("tab_docs_empty_hint")
  16. label.isHidden = true
  17. return label
  18. }()
  19. init(fileMessageIds: [Int]) {
  20. self.fileMessageIds = fileMessageIds
  21. super.init(nibName: nil, bundle: nil)
  22. self.title = String.localized("files")
  23. }
  24. required init?(coder: NSCoder) {
  25. fatalError("init(coder:) has not been implemented")
  26. }
  27. // MARK: - lifecycle
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. setupSubviews()
  31. if fileMessageIds.isEmpty {
  32. emptyStateView.isHidden = false
  33. }
  34. }
  35. // MARK: - layout
  36. private func setupSubviews() {
  37. view.addSubview(tableViews)
  38. tableViews.translatesAutoresizingMaskIntoConstraints = false
  39. tableViews.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
  40. tableViews.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
  41. tableViews.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
  42. tableViews.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  43. view.addSubview(emptyStateView)
  44. emptyStateView.translatesAutoresizingMaskIntoConstraints = false
  45. emptyStateView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor).isActive = true
  46. emptyStateView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor).isActive = true
  47. emptyStateView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
  48. emptyStateView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
  49. }
  50. }
  51. // MARK: - UITableViewDelegate, UITableViewDataSource
  52. extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource {
  53. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  54. return fileMessageIds.count
  55. }
  56. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  57. guard let cell = tableView.dequeueReusableCell(withIdentifier: DocumentGalleryFileCell.reuseIdentifier, for: indexPath) as? DocumentGalleryFileCell else {
  58. return UITableViewCell()
  59. }
  60. let msg = DcMsg(id: fileMessageIds[indexPath.row])
  61. cell.update(msg: msg)
  62. return cell
  63. }
  64. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  65. let msgId = fileMessageIds[indexPath.row]
  66. showPreview(msgId: msgId)
  67. tableView.deselectRow(at: indexPath, animated: false)
  68. }
  69. }
  70. // MARK: - coordinator
  71. extension DocumentGalleryController {
  72. func showPreview(msgId: Int) {
  73. guard let index = fileMessageIds.index(of: msgId) else {
  74. return
  75. }
  76. let previewController = PreviewController(type: .multi(fileMessageIds, index))
  77. present(previewController, animated: true, completion: nil)
  78. }
  79. }