ListGroupImages.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // ListGroupImages.swift
  3. // DigiXLite
  4. //
  5. // Created by Akhmad Al Qindi Irsyam on 28/07/23.
  6. //
  7. import UIKit
  8. class ListGroupImages: UIViewController, UITableViewDataSource, UITableViewDelegate {
  9. var listGroupingImages: [ImageGrouping]!
  10. var imageTapped: Int!
  11. var titleName: String!
  12. let tableViewImages = UITableView()
  13. var isInitiator = false
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. view.backgroundColor = .white
  17. let centeredTitleView = CenteredTitleSubtitleView(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
  18. centeredTitleView.titleLabel.text = titleName
  19. centeredTitleView.subtitleLabel.text = String(listGroupingImages.count) + " " + "images".localized()
  20. navigationItem.titleView = centeredTitleView
  21. tableViewImages.register(UITableViewCell.self, forCellReuseIdentifier: "cellGrupingImages")
  22. tableViewImages.dataSource = self
  23. tableViewImages.delegate = self
  24. tableViewImages.separatorStyle = .none
  25. self.view.addSubview(tableViewImages)
  26. tableViewImages.anchor(top: self.view.safeAreaLayoutGuide.topAnchor, left: self.view.safeAreaLayoutGuide.leftAnchor, bottom: self.view.safeAreaLayoutGuide.bottomAnchor, right: self.view.safeAreaLayoutGuide.rightAnchor)
  27. tableViewImages.scrollToRow(at: IndexPath(row: imageTapped, section: 0), at: .top, animated: false)
  28. let center: NotificationCenter = NotificationCenter.default
  29. center.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: DigiX.listenerStatusChat), object: nil)
  30. }
  31. @objc func onStatusChat(notification: NSNotification) {
  32. DispatchQueue.main.async { [self] in
  33. let data:[AnyHashable : Any] = notification.userInfo!
  34. if let dataMessage = data["message"] as? TMessage {
  35. var messageId = dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)
  36. messageId = messageId.contains("-2") ? String(messageId.split(separator: ",")[1]) : messageId
  37. if let idx = listGroupingImages.firstIndex(where: { $0.messageId == messageId }) {
  38. listGroupingImages[idx].status = dataMessage.getBody(key: CoreMessage_TMessageKey.STATUS)
  39. tableViewImages.reloadRows(at: [IndexPath(row: idx, section: 0)], with: .none)
  40. }
  41. }
  42. }
  43. }
  44. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  45. return listGroupingImages.count
  46. }
  47. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  48. let cell = tableView.dequeueReusableCell(withIdentifier: "cellGrupingImages", for: indexPath as IndexPath)
  49. cell.contentView.subviews.forEach({ $0.removeFromSuperview() })
  50. cell.backgroundColor = .clear
  51. cell.selectionStyle = .none
  52. let containerImages = UIImageView()
  53. cell.contentView.addSubview(containerImages)
  54. containerImages.anchor(top: cell.contentView.topAnchor, left: cell.contentView.leftAnchor, bottom: cell.contentView.bottomAnchor, right: cell.contentView.rightAnchor, paddingBottom: 15, height: UIScreen.main.bounds.height - 104)
  55. let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
  56. let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
  57. let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
  58. if let dirPath = paths.first {
  59. let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listGroupingImages[indexPath.row].imageId)
  60. let image = UIImage(contentsOfFile: imageURL.path)
  61. containerImages.image = image
  62. if !FileManager.default.fileExists(atPath: imageURL.path) {
  63. let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
  64. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  65. blurEffectView.frame = CGRect(x: 0, y: 0, width: containerImages.frame.size.width, height: containerImages.frame.size.height)
  66. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  67. containerImages.addSubview(blurEffectView)
  68. }
  69. }
  70. let containerTimeStatus = UIView()
  71. containerImages.addSubview(containerTimeStatus)
  72. containerTimeStatus.anchor(bottom: containerImages.bottomAnchor, right: containerImages.rightAnchor, height: 15)
  73. let widthcontainerTimeStatus = containerTimeStatus.widthAnchor.constraint(equalToConstant: 50)
  74. widthcontainerTimeStatus.isActive = true
  75. containerTimeStatus.layer.cornerRadius = 5.0
  76. containerTimeStatus.layer.masksToBounds = true
  77. containerTimeStatus.backgroundColor = .black.withAlphaComponent(0.25)
  78. let timeInImage = UILabel()
  79. containerTimeStatus.addSubview(timeInImage)
  80. let date = Date(milliseconds: Int64(listGroupingImages[indexPath.row].time) ?? 100)
  81. let formatter = DateFormatter()
  82. formatter.dateFormat = "HH:mm"
  83. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  84. timeInImage.text = formatter.string(from: date as Date)
  85. timeInImage.textColor = .white
  86. timeInImage.font = UIFont.systemFont(ofSize: 10, weight: .medium)
  87. if isInitiator {
  88. let statusInImage = UIImageView()
  89. containerTimeStatus.addSubview(statusInImage)
  90. statusInImage.anchor(right: containerTimeStatus.rightAnchor, centerY: containerTimeStatus.centerYAnchor, width: 15, height: 15)
  91. if listGroupingImages[indexPath.row].status == "1" || listGroupingImages[indexPath.row].status == "2" {
  92. statusInImage.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.white)
  93. } else if listGroupingImages[indexPath.row].status == "3" {
  94. statusInImage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.white)
  95. } else {
  96. statusInImage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
  97. }
  98. timeInImage.anchor(right: statusInImage.leftAnchor, centerY: containerTimeStatus.centerYAnchor, height: 15)
  99. } else {
  100. timeInImage.anchor(right: containerTimeStatus.rightAnchor, paddingRight: 5, centerY: containerTimeStatus.centerYAnchor, height: 15)
  101. widthcontainerTimeStatus.constant = 40
  102. }
  103. return cell
  104. }
  105. }
  106. class CenteredTitleSubtitleView: UIView {
  107. let titleLabel: UILabel = {
  108. let label = UILabel()
  109. label.textAlignment = .center
  110. label.font = UIFont.boldSystemFont(ofSize: 18)
  111. label.textColor = .white
  112. return label
  113. }()
  114. let subtitleLabel: UILabel = {
  115. let label = UILabel()
  116. label.textAlignment = .center
  117. label.font = UIFont.systemFont(ofSize: 14)
  118. label.textColor = .lightGray
  119. return label
  120. }()
  121. override init(frame: CGRect) {
  122. super.init(frame: frame)
  123. setupSubviews()
  124. }
  125. required init?(coder: NSCoder) {
  126. super.init(coder: coder)
  127. setupSubviews()
  128. }
  129. private func setupSubviews() {
  130. addSubview(titleLabel)
  131. addSubview(subtitleLabel)
  132. // Add any constraints or frames you prefer
  133. // Here's an example using autolayout anchors
  134. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  135. titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  136. titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
  137. subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
  138. subtitleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
  139. subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor).isActive = true
  140. }
  141. }