123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- //
- // ListGroupImages.swift
- // DigiXLite
- //
- // Created by Akhmad Al Qindi Irsyam on 28/07/23.
- //
- import UIKit
- class ListGroupImages: UIViewController, UITableViewDataSource, UITableViewDelegate {
- var listGroupingImages: [ImageGrouping]!
- var imageTapped: Int!
- var titleName: String!
- let tableViewImages = UITableView()
- var isInitiator = false
- override func viewDidLoad() {
- super.viewDidLoad()
- view.backgroundColor = .white
- let centeredTitleView = CenteredTitleSubtitleView(frame: CGRect(x: 0, y: 0, width: 200, height: 44))
- centeredTitleView.titleLabel.text = titleName
- centeredTitleView.subtitleLabel.text = String(listGroupingImages.count) + " " + "images".localized()
- navigationItem.titleView = centeredTitleView
-
- tableViewImages.register(UITableViewCell.self, forCellReuseIdentifier: "cellGrupingImages")
- tableViewImages.dataSource = self
- tableViewImages.delegate = self
- tableViewImages.separatorStyle = .none
- self.view.addSubview(tableViewImages)
- tableViewImages.anchor(top: self.view.safeAreaLayoutGuide.topAnchor, left: self.view.safeAreaLayoutGuide.leftAnchor, bottom: self.view.safeAreaLayoutGuide.bottomAnchor, right: self.view.safeAreaLayoutGuide.rightAnchor)
-
- tableViewImages.scrollToRow(at: IndexPath(row: imageTapped, section: 0), at: .top, animated: false)
-
- let center: NotificationCenter = NotificationCenter.default
- center.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: DigiX.listenerStatusChat), object: nil)
- }
-
- @objc func onStatusChat(notification: NSNotification) {
- DispatchQueue.main.async { [self] in
- let data:[AnyHashable : Any] = notification.userInfo!
- if let dataMessage = data["message"] as? TMessage {
- var messageId = dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)
- messageId = messageId.contains("-2") ? String(messageId.split(separator: ",")[1]) : messageId
- if let idx = listGroupingImages.firstIndex(where: { $0.messageId == messageId }) {
- listGroupingImages[idx].status = dataMessage.getBody(key: CoreMessage_TMessageKey.STATUS)
- tableViewImages.reloadRows(at: [IndexPath(row: idx, section: 0)], with: .none)
- }
- }
- }
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return listGroupingImages.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cellGrupingImages", for: indexPath as IndexPath)
- cell.contentView.subviews.forEach({ $0.removeFromSuperview() })
- cell.backgroundColor = .clear
- cell.selectionStyle = .none
-
- let containerImages = UIImageView()
- cell.contentView.addSubview(containerImages)
- 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)
-
- let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
- let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
- let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
- if let dirPath = paths.first {
- let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listGroupingImages[indexPath.row].imageId)
- let image = UIImage(contentsOfFile: imageURL.path)
- containerImages.image = image
- if !FileManager.default.fileExists(atPath: imageURL.path) {
- let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
- let blurEffectView = UIVisualEffectView(effect: blurEffect)
- blurEffectView.frame = CGRect(x: 0, y: 0, width: containerImages.frame.size.width, height: containerImages.frame.size.height)
- blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- containerImages.addSubview(blurEffectView)
- }
- }
- let containerTimeStatus = UIView()
- containerImages.addSubview(containerTimeStatus)
- containerTimeStatus.anchor(bottom: containerImages.bottomAnchor, right: containerImages.rightAnchor, height: 15)
- let widthcontainerTimeStatus = containerTimeStatus.widthAnchor.constraint(equalToConstant: 50)
- widthcontainerTimeStatus.isActive = true
- containerTimeStatus.layer.cornerRadius = 5.0
- containerTimeStatus.layer.masksToBounds = true
- containerTimeStatus.backgroundColor = .black.withAlphaComponent(0.25)
-
- let timeInImage = UILabel()
- containerTimeStatus.addSubview(timeInImage)
- let date = Date(milliseconds: Int64(listGroupingImages[indexPath.row].time) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- timeInImage.text = formatter.string(from: date as Date)
- timeInImage.textColor = .white
- timeInImage.font = UIFont.systemFont(ofSize: 10, weight: .medium)
-
- if isInitiator {
- let statusInImage = UIImageView()
- containerTimeStatus.addSubview(statusInImage)
- statusInImage.anchor(right: containerTimeStatus.rightAnchor, centerY: containerTimeStatus.centerYAnchor, width: 15, height: 15)
- if listGroupingImages[indexPath.row].status == "1" || listGroupingImages[indexPath.row].status == "2" {
- statusInImage.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.white)
- } else if listGroupingImages[indexPath.row].status == "3" {
- statusInImage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.white)
- } else {
- statusInImage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- }
- timeInImage.anchor(right: statusInImage.leftAnchor, centerY: containerTimeStatus.centerYAnchor, height: 15)
- } else {
- timeInImage.anchor(right: containerTimeStatus.rightAnchor, paddingRight: 5, centerY: containerTimeStatus.centerYAnchor, height: 15)
- widthcontainerTimeStatus.constant = 40
- }
- return cell
- }
- }
- class CenteredTitleSubtitleView: UIView {
- let titleLabel: UILabel = {
- let label = UILabel()
- label.textAlignment = .center
- label.font = UIFont.boldSystemFont(ofSize: 18)
- label.textColor = .white
- return label
- }()
-
- let subtitleLabel: UILabel = {
- let label = UILabel()
- label.textAlignment = .center
- label.font = UIFont.systemFont(ofSize: 14)
- label.textColor = .lightGray
- return label
- }()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupSubviews()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- setupSubviews()
- }
-
- private func setupSubviews() {
- addSubview(titleLabel)
- addSubview(subtitleLabel)
-
- // Add any constraints or frames you prefer
- // Here's an example using autolayout anchors
- titleLabel.translatesAutoresizingMaskIntoConstraints = false
- titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
-
- subtitleLabel.translatesAutoresizingMaskIntoConstraints = false
- subtitleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
- subtitleLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor).isActive = true
- }
- }
|