1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114 |
- //
- // MessageInfo.swift
- // DigiXLite
- //
- // Created by Qindi on 11/08/22.
- //
- import UIKit
- class MessageInfo: UIViewController, UITableViewDelegate, UITableViewDataSource {
- var data: [String: Any?] = [:]
- var dataStatus: [[String: Any?]] = []
- private var tableStatus: UITableView!
- var dateMessage = ""
- var dataPerson: [String: String?] = [:]
- var dataGroup: [String: Any?] = [:]
- var isPersonal = true
- override func viewDidLoad() {
- super.viewDidLoad()
- title = "Message Info".localized()
- view.backgroundColor = .white
- navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
- navigationController?.navigationBar.topItem?.backButtonTitle = "Back".localized()
-
- tableStatus = UITableView(frame: .zero, style: .grouped)
- tableStatus.register(UITableViewCell.self, forCellReuseIdentifier: "cellStatus")
- tableStatus.dataSource = self
- tableStatus.delegate = self
- tableStatus.separatorStyle = .none
- tableStatus.bounces = false
-
- getData()
- dateMessage = chatDate(stringDate: data["server_date"] as! String)
-
- tableStatus.reloadData()
-
- view.addSubview(tableStatus)
- tableStatus.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
- NotificationCenter.default.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: DigiX.listenerStatusChat), object: nil)
- }
-
- @objc func onStatusChat(notification: NSNotification) {
- DispatchQueue.main.async{
- self.dataStatus.removeAll()
- self.getData()
- self.tableStatus.reloadData()
- }
- }
-
- private func getData() {
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: "SELECT f_pin, status, time_delivered, time_read, time_ack FROM MESSAGE_STATUS where message_id='\(data["message_id"]!!)'") {
- var listStatus: [Int] = []
- while cursorData.next() {
- var row: [String: Any?] = [:]
- row["f_pin"] = cursorData.string(forColumnIndex: 0) ?? ""
- row["status"] = cursorData.string(forColumnIndex: 1) ?? ""
- row["time_delivered"] = cursorData.string(forColumnIndex: 2) ?? ""
- row["time_read"] = cursorData.string(forColumnIndex: 3) ?? ""
- row["time_ack"] = cursorData.string(forColumnIndex: 4) ?? ""
- dataStatus.append(row)
- listStatus.append(Int(row["status"] as! String)!)
- }
- data["status"] = "\(listStatus.min() ?? 2)"
- cursorData.close()
- }
- })
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let containerViewStatus = UIView()
- containerViewStatus.backgroundColor = .darkGray
-
- let viewStatus = UIView()
- containerViewStatus.addSubview(viewStatus)
- viewStatus.anchor(top: containerViewStatus.topAnchor, left: containerViewStatus.leftAnchor, bottom: containerViewStatus.bottomAnchor, right: containerViewStatus.rightAnchor, height: 50)
-
- let imageStatus = UIImageView()
- viewStatus.addSubview(imageStatus)
- imageStatus.anchor(left: viewStatus.leftAnchor, bottom: viewStatus.bottomAnchor, paddingLeft: 15, paddingBottom: 5, width: 15, height: 15)
-
- let textStatus = UILabel()
- textStatus.font = .systemFont(ofSize: 12)
- textStatus.textColor = .white
- viewStatus.addSubview(textStatus)
- textStatus.anchor(left: imageStatus.rightAnchor, bottom: viewStatus.bottomAnchor, paddingLeft: 5.0, paddingBottom: 5.0)
-
- if section == 0 {
- let containerView = UIView()
- containerView.backgroundColor = .clear
-
- let dateView = UIView()
- containerView.addSubview(dateView)
- dateView.translatesAutoresizingMaskIntoConstraints = false
- var topAnchor = dateView.topAnchor.constraint(equalTo: containerView.topAnchor)
- if section == 0 {
- topAnchor = dateView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10.0)
- }
- NSLayoutConstraint.activate([
- topAnchor,
- dateView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
- dateView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
- dateView.heightAnchor.constraint(equalToConstant: 30),
- dateView.widthAnchor.constraint(greaterThanOrEqualToConstant: 60)
- ])
- dateView.backgroundColor = .orangeColor
- dateView.layer.cornerRadius = 15.0
- dateView.clipsToBounds = true
-
- let labelDate = UILabel()
- dateView.addSubview(labelDate)
- labelDate.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- labelDate.centerYAnchor.constraint(equalTo: dateView.centerYAnchor),
- labelDate.centerXAnchor.constraint(equalTo: dateView.centerXAnchor),
- labelDate.leadingAnchor.constraint(equalTo: dateView.leadingAnchor, constant: 10),
- labelDate.trailingAnchor.constraint(equalTo: dateView.trailingAnchor, constant: -10),
- ])
- labelDate.textAlignment = .center
- labelDate.textColor = .secondaryColor
- labelDate.font = UIFont.systemFont(ofSize: 12, weight: .medium)
- labelDate.text = dateMessage
- return containerView
- } else if section == 1 {
- if !data.isEmpty && data["read_receipts"] as? String == "8" {
- imageStatus.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
- textStatus.text = ("Confirmed".localized() + " " + "by".localized()).uppercased()
- } else {
- imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- textStatus.text = ("Read".localized() + " " + "by".localized()).uppercased()
- }
- } else if section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
- imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- textStatus.text = ("Read".localized() + " " + "by".localized()).uppercased()
- } else {
- imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
- textStatus.text = ("Delivered".localized() + " " + "to".localized()).uppercased()
- }
- return containerViewStatus
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
- if !isPersonal {
- var numberSection = 4
- if !data.isEmpty && data["read_receipts"] as? String != "8" {
- numberSection -= 1
- }
- if dataStatus.count == dataStatus.filter({ !($0["time_delivered"] as! String).isEmpty && !($0["time_read"] as! String).isEmpty }).count {
- numberSection -= 1
- }
- if !data.isEmpty && data["read_receipts"] as? String == "8" {
- if dataStatus.count == dataStatus.filter({ !($0["time_read"] as! String).isEmpty && !($0["time_ack"] as! String).isEmpty }).count{
- numberSection -= 1
- }
- }
- return numberSection
- }
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if isPersonal {
- if !data.isEmpty && data["read_receipts"] as? String == "8" {
- return 4
- }
- return 3
- }
- if section == 0 {
- return 1
- } else if section == 1 {
- if !data.isEmpty && data["read_receipts"] as? String == "8" {
- return dataStatus.filter({ ($0["status"] as! String) == "8" }).count + 1
- } else {
- return dataStatus.filter({ ($0["status"] as! String) == "4" }).count + 1
- }
- } else if section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
- return dataStatus.filter({ ($0["status"] as! String) == "4" }).count + 1
- }
- return dataStatus.filter({ ($0["status"] as! String) == "3" }).count + 1
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let idMe = UserDefaults.standard.string(forKey: "me") as String?
- let cell = tableView.dequeueReusableCell(withIdentifier: "cellStatus", for: indexPath as IndexPath)
- cell.selectionStyle = .none
- cell.backgroundColor = .clear
- cell.accessoryView = nil
- cell.contentConfiguration = nil
- cell.contentView.subviews.forEach({ $0.removeFromSuperview() })
-
- if !isPersonal {
- if indexPath.section != 0 {
- let imageNil = UIImageView()
- imageNil.image = UIImage(systemName: "ellipsis")
- imageNil.contentMode = .center
- imageNil.tintColor = .black
-
- let dataStatusAck = dataStatus.filter({ ($0["status"] as! String) == "8" })
- let dataStatusRead = dataStatus.filter({ ($0["status"] as! String) == "4" })
- let dataStatusDelivered = dataStatus.filter({ ($0["status"] as! String) == "3" })
-
- cell.backgroundColor = .white
-
- var content = cell.defaultContentConfiguration()
- content.textProperties.font = UIFont.systemFont(ofSize: 14)
- content.imageProperties.maximumSize = CGSize(width: 24, height: 24)
-
- if indexPath.section == 1 {
- if !data.isEmpty && data["read_receipts"] as? String == "8" {
- if dataStatusAck.count == 0 || indexPath.row == dataStatusAck.count {
- cell.contentView.addSubview(imageNil)
- imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
- } else {
- let dataProfile = getDataProfile(f_pin: dataStatusAck[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
- content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
- getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
- content.image = image
- })
-
- content.text = dataProfile["name"]!
-
- let date = Date(milliseconds: Int64(dataStatusAck[indexPath.row]["time_ack"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatusAck[indexPath.row]["time_ack"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- cell.contentConfiguration = content
- }
- } else {
- if dataStatusRead.count == 0 || indexPath.row == dataStatusRead.count {
- cell.contentView.addSubview(imageNil)
- imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
- } else {
- let dataProfile = getDataProfile(f_pin: dataStatusRead[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
- content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
- getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
- content.image = image
- })
-
- content.text = dataProfile["name"]!
-
- let date = Date(milliseconds: Int64(dataStatusRead[indexPath.row]["time_read"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatusRead[indexPath.row]["time_read"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- cell.contentConfiguration = content
- }
- }
- } else if indexPath.section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
- if dataStatusRead.count == 0 || indexPath.row == dataStatusRead.count {
- cell.contentView.addSubview(imageNil)
- imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
- } else {
- let dataProfile = getDataProfile(f_pin: dataStatusRead[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
- content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
- getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
- content.image = image
- })
-
- content.text = dataProfile["name"]!
-
- let date = Date(milliseconds: Int64(dataStatusRead[indexPath.row]["time_read"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatusRead[indexPath.row]["time_read"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- cell.contentConfiguration = content
- }
- } else {
- if dataStatusDelivered.count == 0 || indexPath.row == dataStatusDelivered.count {
- cell.contentView.addSubview(imageNil)
- imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
- } else {
- let dataProfile = getDataProfile(f_pin: dataStatusDelivered[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
- content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
- getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
- content.image = image
- })
-
- content.text = dataProfile["name"]!
-
- let date = Date(milliseconds: Int64(dataStatusDelivered[indexPath.row]["time_delivered"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatusDelivered[indexPath.row]["time_delivered"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- cell.contentConfiguration = content
- }
- }
- return cell
- }
- }
-
- if indexPath.row != 0 {
- cell.backgroundColor = .white
- var content = cell.defaultContentConfiguration()
- content.textProperties.font = UIFont.systemFont(ofSize: 14)
- content.imageProperties.maximumSize = CGSize(width: 24, height: 24)
-
- let noStatus = UIImageView(frame: CGRect(x: 0, y: cell.frame.height / 2, width: 50, height: 20))
- noStatus.image = UIImage(systemName: "ellipsis")
- noStatus.contentMode = .center
- noStatus.tintColor = .black
-
- if indexPath.row == 1 {
- if !data.isEmpty && data["read_receipts"] as? String == "8"{
- content.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
- content.text = "Confirmed".localized()
- if dataStatus.count != 0 {
- if (dataStatus[0]["time_ack"] as! String).isEmpty {
- cell.accessoryView = noStatus
- } else {
- let date = Date(milliseconds: Int64(dataStatus[0]["time_ack"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_ack"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- }
- }
- } else {
- content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- content.text = "Read".localized()
- if dataStatus.count != 0 {
- if (dataStatus[0]["time_read"] as! String).isEmpty {
- cell.accessoryView = noStatus
- } else {
- let date = Date(milliseconds: Int64(dataStatus[0]["time_read"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_read"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- }
- }
- }
- } else if indexPath.row == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
- content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- content.text = "Read".localized()
- if dataStatus.count != 0 {
- if (dataStatus[0]["time_read"] as! String).isEmpty {
- cell.accessoryView = noStatus
- } else {
- let date = Date(milliseconds: Int64(dataStatus[0]["time_read"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_read"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- }
- }
- } else {
- content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
- content.text = "Delivered".localized()
- if (dataStatus[0]["time_delivered"] as! String).isEmpty {
- cell.accessoryView = noStatus
- } else {
- let date = Date(milliseconds: Int64(dataStatus[0]["time_delivered"] as! String) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- let time = formatter.string(from: date as Date)
-
- let viewTimeStatus = UIView()
- viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
-
- let titleTime = UILabel()
- viewTimeStatus.addSubview(titleTime)
- titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
- titleTime.font = .systemFont(ofSize: 12)
- titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_delivered"] as! String)) \(time)"
-
- cell.accessoryView = viewTimeStatus
- }
- }
- cell.contentConfiguration = content
- } else {
- let thumbChat = (data["thumb_id"] as? String) ?? ""
- let imageChat = (data["image_id"] as? String) ?? ""
- let videoChat = (data["video_id"] as? String) ?? ""
- let fileChat = (data["file_id"] as? String) ?? ""
- let reffChat = (data["reff_id"] as? String) ?? ""
-
- cell.backgroundColor = .clear
- cell.selectionStyle = .none
-
- let containerMessage = UIView()
- cell.contentView.addSubview(containerMessage)
- containerMessage.translatesAutoresizingMaskIntoConstraints = false
-
- let timeMessage = UILabel()
- cell.contentView.addSubview(timeMessage)
- timeMessage.translatesAutoresizingMaskIntoConstraints = false
- if (data["read_receipts"] as? String) == "8" {
- timeMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -40).isActive = true
- } else {
- timeMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -5).isActive = true
- }
-
- let statusMessage = UIImageView()
- containerMessage.leadingAnchor.constraint(greaterThanOrEqualTo: cell.contentView.leadingAnchor, constant: 60).isActive = true
- if (data["read_receipts"] as? String) == "8" {
- containerMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -40).isActive = true
- } else {
- containerMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -5).isActive = true
- }
- containerMessage.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 5).isActive = true
- containerMessage.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor, constant: -15).isActive = true
- containerMessage.widthAnchor.constraint(greaterThanOrEqualToConstant: 46).isActive = true
- if (data["attachment_flag"] as? String == "11" && data["reff_id"]as? String == "") {
- containerMessage.backgroundColor = .clear
- } else {
- containerMessage.backgroundColor = .blueBubbleColor
- }
- containerMessage.layer.cornerRadius = 10.0
- containerMessage.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner, .layerMinXMinYCorner]
- containerMessage.clipsToBounds = true
-
- timeMessage.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
-
- cell.contentView.addSubview(statusMessage)
- statusMessage.translatesAutoresizingMaskIntoConstraints = false
- statusMessage.bottomAnchor.constraint(equalTo: timeMessage.topAnchor).isActive = true
- statusMessage.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
- statusMessage.widthAnchor.constraint(equalToConstant: 15).isActive = true
- statusMessage.heightAnchor.constraint(equalToConstant: 15).isActive = true
- if (data["status"]! as! String == "1" || data["status"]! as! String == "2" ) {
- statusMessage.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
- } else if (data["status"]! as! String == "3") {
- statusMessage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
- } else if (data["status"]! as! String == "8") {
- statusMessage.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
- } else {
- statusMessage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
- }
-
- if data["is_stared"] as? String == "1" {
- let imageStared = UIImageView()
- cell.contentView.addSubview(imageStared)
- imageStared.translatesAutoresizingMaskIntoConstraints = false
- imageStared.bottomAnchor.constraint(equalTo: statusMessage.topAnchor).isActive = true
- imageStared.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
- imageStared.widthAnchor.constraint(equalToConstant: 15).isActive = true
- imageStared.heightAnchor.constraint(equalToConstant: 15).isActive = true
- imageStared.image = UIImage(systemName: "star.fill")
- imageStared.backgroundColor = .clear
- imageStared.tintColor = .systemYellow
- }
-
- if data["read_receipts"] as? String == "8" {
- let imageAckView = UIImageView()
- var imageAck = UIImage(named: "ack_icon_gray", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
- if data["status"] as? String == "8" {
- imageAck = UIImage(named: "ack_icon", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
- }
- imageAckView.image = imageAck
- cell.contentView.addSubview(imageAckView)
- imageAckView.translatesAutoresizingMaskIntoConstraints = false
- imageAckView.widthAnchor.constraint(equalToConstant: 30).isActive = true
- imageAckView.heightAnchor.constraint(equalToConstant: 30).isActive = true
- imageAckView.topAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: 5).isActive = true
- imageAckView.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 30).isActive = true
- }
-
- let messageText = UILabel()
- messageText.numberOfLines = 0
- messageText.lineBreakMode = .byWordWrapping
- containerMessage.addSubview(messageText)
- messageText.translatesAutoresizingMaskIntoConstraints = false
- let topMarginText = messageText.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15)
- topMarginText.isActive = true
- messageText.textColor = .black
- if data["attachment_flag"] as? String == "27" || data["attachment_flag"] as? String == "26" || data["message_scope_id"] as? String == "18" {
- messageText.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 85).isActive = true
- let imageLS = UIImageView()
- containerMessage.addSubview(imageLS)
- imageLS.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- imageLS.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15.0),
- imageLS.trailingAnchor.constraint(equalTo: messageText.leadingAnchor, constant: -10.0),
- imageLS.centerYAnchor.constraint(equalTo: containerMessage.centerYAnchor),
- imageLS.heightAnchor.constraint(equalToConstant: 60.0)
- ])
- if data["attachment_flag"] as! String == "26" {
- imageLS.image = UIImage(named: "pb_seminar_wpr", in: Bundle.resourceBundle(for: DigiX.self), with: nil)
- } else if data["attachment_flag"] as! String == "27" {
- imageLS.image = UIImage(named: "pb_live_tv", in: Bundle.resourceBundle(for: DigiX.self), with: nil)
- } else if data["message_scope_id"] as? String == "18" {
- imageLS.image = UIImage(systemName: "doc.richtext.fill")
- imageLS.tintColor = .mainColor
- }
- } else {
- messageText.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
- }
- if data["f_pin"] as? String == "-999" && (data["blog_id"] as? String) != nil && !(data["blog_id"] as! String).isEmpty && (data["message_text"] as! String).contains("Berikut QR Code dan detil booking Anda") {
- messageText.bottomAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: -115).isActive = true
- let imageQR = UIImageView()
- containerMessage.addSubview(imageQR)
- imageQR.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- imageQR.centerXAnchor.constraint(equalTo: containerMessage.centerXAnchor),
- imageQR.topAnchor.constraint(equalTo: messageText.bottomAnchor),
- imageQR.widthAnchor.constraint(equalToConstant: 100.0),
- imageQR.heightAnchor.constraint(equalToConstant: 100.0)
- ])
- imageQR.image = generateQRCode(from: data["blog_id"] as! String)
- } else {
- messageText.bottomAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: -15).isActive = true
- }
- messageText.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
- var textChat = (data["message_text"] as? String) ?? ""
- if (data["lock"] != nil && (data["lock"])! as? String == "1") {
- if (data["f_pin"] as? String == idMe) {
- textChat = "🚫 _"+"You were deleted this message".localized()+"_"
- } else {
- textChat = "🚫 _"+"This message was deleted".localized()+"_"
- }
- }
-
- let imageSticker = UIImageView()
- if let attachmentFlag = data["attachment_flag"], let attachmentFlag = attachmentFlag as? String {
- if attachmentFlag == "27" || attachmentFlag == "26" { // live streaming
- let data = textChat
- if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [String: Any] {
- Database().database?.inTransaction({ fmdb, rollback in
- let title = json["title"] as? String ?? ""
- let description = json["description"] as? String ?? ""
- let start = json["time"] as? Int64 ?? 0
- let by = json["by"] as? String ?? ""
- var type = "*Live Streaming*"
- if attachmentFlag == "26" {
- type = "*Seminar*"
- }
- if let c = Database().getRecords(fmdb: fmdb, query: "select first_name || ' ' || last_name from BUDDY where f_pin = '\(by)'"), c.next() {
- let name = c.string(forColumnIndex: 0)!
- messageText.attributedText = "\(type) \nTitle: \(title) \nDescription: \(description) \nStart: \(Date(milliseconds: start).format(dateFormat: "dd/MM/yyyy HH:mm")) \nBroadcaster: \(name)".richText()
- c.close()
- } else {
- messageText.attributedText = ("\(type) \nTitle: \(title) \nDescription: \(description) \nStart: \(Date(milliseconds: start).format(dateFormat: "dd/MM/yyyy HH:mm"))").richText()
- }
- })
- }
- }
- else if attachmentFlag == "11" {
- messageText.text = ""
- topMarginText.constant = topMarginText.constant + 100
- containerMessage.addSubview(imageSticker)
- imageSticker.translatesAutoresizingMaskIntoConstraints = false
- if (reffChat == "") {
- imageSticker.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
- imageSticker.widthAnchor.constraint(equalToConstant: 80).isActive = true
- } else {
- imageSticker.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
- }
- imageSticker.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
- imageSticker.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
- imageSticker.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
- imageSticker.image = UIImage(named: (textChat.components(separatedBy: "/")[1]), in: Bundle.resourceBundle(for: DigiX.self), with: nil) //resourcesMediaBundle
- imageSticker.contentMode = .scaleAspectFit
- } else if data["message_scope_id"] as! String == "18" {
- let data = textChat
- if let jsonForm = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [String: Any] {
- let form_title = jsonForm["form_title"] as! String
- let club_type = jsonForm["club_type"] as! String
- let province = jsonForm["province"] as! String
- let club = jsonForm["club"] as! String
- messageText.attributedText = "*\(form_title.replacingOccurrences(of: "+", with: " "))* \nClub Type: \(club_type) \nProvince: \(province) \nClub Name: \(club) ".richText()
- }
- }
- else {
- messageText.attributedText = textChat.richText()
- }
- } else {
- messageText.attributedText = textChat.richText()
- }
- messageText.isUserInteractionEnabled = true
- if !textChat.isEmpty {
- let listText = textChat.split(separator: " ")
- for i in 0...listText.count - 1 {
- if listText[i].lowercased().checkStartWithLink() {
- if ((listText[i].lowercased().starts(with: "www.") && listText[i].lowercased().split(separator: ".").count >= 3) || (!listText[i].lowercased().starts(with: "www.") && listText[i].lowercased().split(separator: ".").count >= 2)) && listText[i].lowercased().split(separator: ".").last!.count >= 2 {
- let objectGesture = ObjectGesture(target: self, action: #selector(tapMessageText(_:)))
- objectGesture.message_id = "\(listText[i])"
- messageText.addGestureRecognizer(objectGesture)
- }
- }
- }
- }
-
- let stringDate = (data["server_date"] as? String) ?? ""
- if !stringDate.isEmpty {
- let date = Date(milliseconds: Int64(stringDate) ?? 100)
- let formatter = DateFormatter()
- formatter.dateFormat = "HH:mm"
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- timeMessage.text = formatter.string(from: date as Date)
- timeMessage.font = UIFont.systemFont(ofSize: 10, weight: .medium)
- timeMessage.textColor = .lightGray
- }
-
- let imageThumb = UIImageView()
- let containerViewFile = UIView()
-
- if (!thumbChat.isEmpty) {
- topMarginText.constant = topMarginText.constant + ListGroupImages.getImageSize(image: thumbChat, screenWidth: self.view.frame.size.width * 0.6, screenHeight: 305)!.height
-
- containerMessage.addSubview(imageThumb)
- imageThumb.translatesAutoresizingMaskIntoConstraints = false
- let dataReply = queryMessageReply(message_id: reffChat)
- if (dataReply.count == 0) {
- imageThumb.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
- }
- imageThumb.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
- imageThumb.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
- imageThumb.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
- imageThumb.widthAnchor.constraint(equalToConstant: ListGroupImages.getImageSize(image: thumbChat, screenWidth: self.view.frame.size.width * 0.6, screenHeight: 305)!.width).isActive = true
- imageThumb.layer.cornerRadius = 5.0
- imageThumb.clipsToBounds = true
- imageThumb.contentMode = .scaleAspectFill
-
- let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
- let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
- let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
- if let dirPath = paths.first {
- let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumbChat)
- let image = UIImage(contentsOfFile: thumbURL.path)
- // let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
- imageThumb.image = image
-
- let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(videoChat)
- let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(imageChat)
- if !FileManager.default.fileExists(atPath: imageURL.path) || !FileManager.default.fileExists(atPath: videoURL.path) {
- let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
- let blurEffectView = UIVisualEffectView(effect: blurEffect)
- blurEffectView.frame = CGRect(x: 0, y: 0, width: imageThumb.frame.size.width, height: imageThumb.frame.size.height)
- blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- imageThumb.addSubview(blurEffectView)
- if !imageChat.isEmpty {
- let imageDownload = UIImageView(image: UIImage(systemName: "arrow.down.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 50, weight: .bold, scale: .default)))
- imageThumb.addSubview(imageDownload)
- imageDownload.tintColor = .black.withAlphaComponent(0.3)
- imageDownload.translatesAutoresizingMaskIntoConstraints = false
- imageDownload.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
- imageDownload.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
- }
- }
-
- }
-
- if (videoChat != "") {
- let imagePlay = UIImageView(image: UIImage(systemName: "play.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .bold, scale: .default))?.imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))?.withTintColor(.white))
- imagePlay.circle()
- imageThumb.addSubview(imagePlay)
- imagePlay.backgroundColor = .black.withAlphaComponent(0.3)
- imagePlay.translatesAutoresizingMaskIntoConstraints = false
- imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
- imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
- }
-
- if (data["progress"] as! Double != 100.0 && data["f_pin"] as? String == idMe) {
- let container = UIView()
- imageThumb.addSubview(container)
- container.translatesAutoresizingMaskIntoConstraints = false
- container.bottomAnchor.constraint(equalTo: imageThumb.bottomAnchor, constant: -10).isActive = true
- container.leadingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: 10).isActive = true
- container.widthAnchor.constraint(equalToConstant: 30).isActive = true
- container.heightAnchor.constraint(equalToConstant: 30).isActive = true
- container.backgroundColor = .white.withAlphaComponent(0.1)
- let circlePath = UIBezierPath(arcCenter: CGPoint(x: 10, y: 20), radius: 15, startAngle: -(.pi / 2), endAngle: .pi * 2, clockwise: true)
- let trackShape = CAShapeLayer()
- trackShape.path = circlePath.cgPath
- trackShape.fillColor = UIColor.black.withAlphaComponent(0.3).cgColor
- trackShape.lineWidth = 3
- trackShape.strokeColor = UIColor.blueBubbleColor.withAlphaComponent(0.3).cgColor
- container.backgroundColor = .clear
- container.layer.addSublayer(trackShape)
- let shapeLoading = CAShapeLayer()
- shapeLoading.path = circlePath.cgPath
- shapeLoading.fillColor = UIColor.clear.cgColor
- shapeLoading.lineWidth = 3
- shapeLoading.strokeEnd = 0
- shapeLoading.strokeColor = UIColor.blueBubbleColor.cgColor
- container.layer.addSublayer(shapeLoading)
- let imageupload = UIImageView(image: UIImage(systemName: "arrow.up", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
- imageupload.tintColor = .white
- container.addSubview(imageupload)
- imageupload.translatesAutoresizingMaskIntoConstraints = false
- imageupload.bottomAnchor.constraint(equalTo: imageThumb.bottomAnchor, constant: -10).isActive = true
- imageupload.leadingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: 10).isActive = true
- imageupload.widthAnchor.constraint(equalToConstant: 20).isActive = true
- imageupload.heightAnchor.constraint(equalToConstant: 20).isActive = true
- }
- }
-
- if (fileChat != "") && data["message_scope_id"] as! String != "18" {
- topMarginText.constant = topMarginText.constant + 55
-
- let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
- let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
- let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
- let arrExtFile = (textChat.components(separatedBy: "|")[0]).split(separator: ".")
- let finalExtFile = arrExtFile[arrExtFile.count - 1]
- if let dirPath = paths.first {
- let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(fileChat)
- if let dataFile = try? Data(contentsOf: fileURL) {
- var sizeOfFile = Int(dataFile.count / 1000000)
- if (sizeOfFile < 1) {
- sizeOfFile = Int(dataFile.count / 1000)
- if (finalExtFile.count > 4) {
- messageText.text = "\(sizeOfFile) kB \u{2022} TXT"
- }else {
- messageText.text = "\(sizeOfFile) kB \u{2022} \(finalExtFile.uppercased())"
- }
- } else {
- if (finalExtFile.count > 4) {
- messageText.text = "\(sizeOfFile) MB \u{2022} TXT"
- }else {
- messageText.text = "\(sizeOfFile) MB \u{2022} \(finalExtFile.uppercased())"
- }
- }
- } else {
- messageText.text = ""
- }
- }
-
- containerMessage.addSubview(containerViewFile)
- containerViewFile.translatesAutoresizingMaskIntoConstraints = false
- let dataReply = queryMessageReply(message_id: reffChat)
- if (dataReply.count == 0) {
- containerViewFile.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
- }
- containerViewFile.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
- containerViewFile.bottomAnchor.constraint(equalTo:messageText.topAnchor, constant: -5).isActive = true
- containerViewFile.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
- containerViewFile.heightAnchor.constraint(equalToConstant: 50).isActive = true
- containerViewFile.backgroundColor = .black.withAlphaComponent(0.2)
- containerViewFile.layer.cornerRadius = 5.0
- containerViewFile.clipsToBounds = true
-
- let imageFile = UIImageView(image: UIImage(systemName: "doc.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .bold, scale: .default)))
- containerViewFile.addSubview(imageFile)
- let nameFile = UILabel()
- containerViewFile.addSubview(nameFile)
-
- imageFile.translatesAutoresizingMaskIntoConstraints = false
- imageFile.leadingAnchor.constraint(equalTo: containerViewFile.leadingAnchor, constant: 5).isActive = true
- imageFile.trailingAnchor.constraint(equalTo: nameFile.leadingAnchor, constant: -5).isActive = true
- imageFile.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
- imageFile.widthAnchor.constraint(equalToConstant: 30).isActive = true
- imageFile.heightAnchor.constraint(equalToConstant: 30).isActive = true
- imageFile.tintColor = .docColor
-
- nameFile.translatesAutoresizingMaskIntoConstraints = false
- nameFile.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
- nameFile.widthAnchor.constraint(lessThanOrEqualToConstant: 200).isActive = true
- nameFile.font = UIFont.systemFont(ofSize: 12, weight: .medium)
- nameFile.textColor = .white
- nameFile.text = textChat.components(separatedBy: "|")[0]
-
- if (data["progress"] as! Double != 100.0) {
- let containerLoading = UIView()
- containerViewFile.addSubview(containerLoading)
- containerLoading.translatesAutoresizingMaskIntoConstraints = false
- containerLoading.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
- containerLoading.leadingAnchor.constraint(equalTo: nameFile.trailingAnchor, constant: 5).isActive = true
- containerLoading.trailingAnchor.constraint(equalTo: containerViewFile.trailingAnchor, constant: -5).isActive = true
- containerLoading.widthAnchor.constraint(equalToConstant: 30).isActive = true
- containerLoading.heightAnchor.constraint(equalToConstant: 30).isActive = true
- let circlePath = UIBezierPath(arcCenter: CGPoint(x: 15, y: 15), radius: 10, startAngle: -(.pi / 2), endAngle: .pi * 2, clockwise: true)
- let trackShape = CAShapeLayer()
- trackShape.path = circlePath.cgPath
- trackShape.fillColor = UIColor.clear.cgColor
- trackShape.lineWidth = 5
- trackShape.strokeColor = UIColor.blueBubbleColor.withAlphaComponent(0.3).cgColor
- containerLoading.layer.addSublayer(trackShape)
- let shapeLoading = CAShapeLayer()
- shapeLoading.path = circlePath.cgPath
- shapeLoading.fillColor = UIColor.clear.cgColor
- shapeLoading.lineWidth = 3
- shapeLoading.strokeEnd = 0
- shapeLoading.strokeColor = UIColor.secondaryColor.cgColor
- containerLoading.layer.addSublayer(shapeLoading)
- var imageupload = UIImageView(image: UIImage(systemName: "arrow.up", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
- if data["f_pin"] as? String != idMe {
- imageupload = UIImageView(image: UIImage(systemName: "arrow.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
- shapeLoading.strokeColor = UIColor.blueBubbleColor.cgColor
- }
- imageupload.tintColor = .white
- containerLoading.addSubview(imageupload)
- imageupload.translatesAutoresizingMaskIntoConstraints = false
- imageupload.centerYAnchor.constraint(equalTo: containerLoading.centerYAnchor).isActive = true
- imageupload.centerXAnchor.constraint(equalTo: containerLoading.centerXAnchor).isActive = true
- } else {
- nameFile.trailingAnchor.constraint(equalTo: containerViewFile.trailingAnchor, constant: -5).isActive = true
- }
- }
-
- if (reffChat != "" && data["message_scope_id"] as! String != "18") {
- let dataReply = queryMessageReply(message_id: reffChat)
- if dataReply.count != 0 {
- topMarginText.constant = topMarginText.constant + 55
-
- let containerReply = UIView()
- containerMessage.addSubview(containerReply)
- containerReply.translatesAutoresizingMaskIntoConstraints = false
- containerReply.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
- containerReply.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
- if thumbChat != "" {
- containerReply.bottomAnchor.constraint(equalTo: imageThumb.topAnchor, constant: -5).isActive = true
- } else if fileChat != "" {
- containerReply.bottomAnchor.constraint(equalTo: containerViewFile.topAnchor, constant: -5).isActive = true
- } else if data["attachment_flag"] as? String == "11" {
- containerReply.bottomAnchor.constraint(equalTo: imageSticker.topAnchor, constant: -5).isActive = true
- } else {
- containerReply.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
- }
- containerReply.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
- containerReply.heightAnchor.constraint(equalToConstant: 50).isActive = true
- containerReply.backgroundColor = .black.withAlphaComponent(0.2)
- containerReply.layer.cornerRadius = 5
- containerReply.clipsToBounds = true
-
- let leftReply = UIView()
- containerReply.addSubview(leftReply)
- leftReply.translatesAutoresizingMaskIntoConstraints = false
- leftReply.leadingAnchor.constraint(equalTo: containerReply.leadingAnchor).isActive = true
- leftReply.topAnchor.constraint(equalTo: containerReply.topAnchor).isActive = true
- leftReply.bottomAnchor.constraint(equalTo: containerReply.bottomAnchor).isActive = true
- leftReply.widthAnchor.constraint(equalToConstant: 3).isActive = true
- leftReply.layer.cornerRadius = 5
- leftReply.clipsToBounds = true
- leftReply.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMinXMinYCorner]
-
- let titleReply = UILabel()
- containerReply.addSubview(titleReply)
- titleReply.translatesAutoresizingMaskIntoConstraints = false
- titleReply.leadingAnchor.constraint(equalTo: leftReply.leadingAnchor, constant: 10).isActive = true
- titleReply.topAnchor.constraint(equalTo: containerReply.topAnchor, constant: 10).isActive = true
- titleReply.trailingAnchor.constraint(lessThanOrEqualTo: containerReply.trailingAnchor, constant: -20).isActive = true
- titleReply.font = UIFont.systemFont(ofSize: 12).bold
- if (data["f_pin"] as? String == idMe) {
- titleReply.text = "You".localized()
- if data["f_pin"] as? String == idMe {
- titleReply.textColor = .white
- leftReply.backgroundColor = .white
- } else {
- titleReply.textColor = .mainColor
- leftReply.backgroundColor = .mainColor
- }
- } else {
- titleReply.text = dataPerson["name"]!!
- if data["f_pin"] as? String == idMe {
- titleReply.textColor = .white
- leftReply.backgroundColor = .white
- }
- }
-
- let contentReply = UILabel()
- containerReply.addSubview(contentReply)
- contentReply.translatesAutoresizingMaskIntoConstraints = false
- contentReply.leadingAnchor.constraint(equalTo: leftReply.leadingAnchor, constant: 10).isActive = true
- contentReply.bottomAnchor.constraint(equalTo: containerReply.bottomAnchor, constant: -10).isActive = true
- contentReply.font = UIFont.systemFont(ofSize: 10)
- let message_text = data["message_text"] as! String
- let attachment_flag = data["attachment_flag"] as! String
- let thumb_chat = data["thumb_id"] as! String
- let image_chat = data["image_id"] as! String
- let video_chat = data["video_id"] as! String
- let file_chat = data["file_id"] as! String
- if (attachment_flag == "0" && thumb_chat == "") {
- contentReply.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -20).isActive = true
- contentReply.attributedText = message_text.richText()
- } else if (attachment_flag == "1" || image_chat != "") {
- if (message_text == "") {
- contentReply.text = "📷 Photo".localized()
- } else {
- contentReply.attributedText = message_text.richText()
- }
- } else if (attachment_flag == "2" || video_chat != "") {
- if (message_text == "") {
- contentReply.text = "📹 Video".localized()
- } else {
- contentReply.attributedText = message_text.richText()
- }
- } else if (attachment_flag == "6" || file_chat != ""){
- contentReply.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -20).isActive = true
- contentReply.text = "📄 \(message_text.components(separatedBy: "|")[0])"
- } else if (attachment_flag == "11") {
- contentReply.text = "❤️ Sticker"
- }
- contentReply.textColor = .white.withAlphaComponent(0.8)
-
- if (attachment_flag == "1" || attachment_flag == "2" || image_chat != "" || video_chat != "") {
- let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
- let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
- let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
- if let dirPath = paths.first {
- let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
- let image = UIImage(contentsOfFile: thumbURL.path)
- let imageThumb = UIImageView(image: image)
- containerReply.addSubview(imageThumb)
- imageThumb.layer.cornerRadius = 2.0
- imageThumb.clipsToBounds = true
- imageThumb.contentMode = .scaleAspectFill
- imageThumb.translatesAutoresizingMaskIntoConstraints = false
- imageThumb.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
- imageThumb.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
- imageThumb.widthAnchor.constraint(equalToConstant: 30).isActive = true
- imageThumb.heightAnchor.constraint(equalToConstant: 30).isActive = true
-
- if (attachment_flag == "2") {
- let imagePlay = UIImageView(image: UIImage(systemName: "play.circle.fill"))
- imageThumb.addSubview(imagePlay)
- imagePlay.clipsToBounds = true
- imagePlay.translatesAutoresizingMaskIntoConstraints = false
- imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
- imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
- imagePlay.widthAnchor.constraint(equalToConstant: 10).isActive = true
- imagePlay.heightAnchor.constraint(equalToConstant: 10).isActive = true
- imagePlay.tintColor = .white
- }
- titleReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
- contentReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
- }
- }
- if (attachment_flag == "11" && message_text.components(separatedBy: "/").count > 1) {
- let imageSticker = UIImageView(image: UIImage(named: (message_text.components(separatedBy: "/")[1]), in: Bundle.resourceBundle(for: DigiX.self), with: nil))
- containerReply.addSubview(imageSticker)
- imageSticker.layer.cornerRadius = 2.0
- imageSticker.clipsToBounds = true
- imageSticker.translatesAutoresizingMaskIntoConstraints = false
- imageSticker.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
- imageSticker.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
- imageSticker.widthAnchor.constraint(equalToConstant: 30).isActive = true
- imageSticker.heightAnchor.constraint(equalToConstant: 30).isActive = true
- titleReply.trailingAnchor.constraint(equalTo: imageSticker.leadingAnchor, constant: -20).isActive = true
- contentReply.trailingAnchor.constraint(equalTo: imageSticker.leadingAnchor, constant: -20).isActive = true
- }
- }
- }
- }
- return cell
- }
-
- private func chatDate(stringDate: String) -> String {
- let date = Date(milliseconds: Int64(stringDate)!)
- let calendar = Calendar.current
- if (calendar.isDateInToday(date)) {
- return "Today".localized()
- } else {
- let startOfNow = calendar.startOfDay(for: Date())
- let startOfTimeStamp = calendar.startOfDay(for: date)
- let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
- let day = -(components.day!)
- if day == 1 {
- return "Yesterday".localized()
- } else if day < 7 {
- return "\(day) " + "days".localized() + " " + "ago".localized()
- } else {
- let formatter = DateFormatter()
- formatter.dateFormat = "EE, dd MMM"
- let lang = UserDefaults.standard.string(forKey: "i18n_language")
- if lang == "id" {
- formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
- }
- let stringFormat = formatter.string(from: date as Date)
- return stringFormat
- }
- }
- }
-
- private func generateQRCode(from string: String) -> UIImage? {
- let data = string.data(using: String.Encoding.ascii)
- if let filter = CIFilter(name: "CIQRCodeGenerator") {
- filter.setValue(data, forKey: "inputMessage")
- let transform = CGAffineTransform(scaleX: 3, y: 3)
- if let output = filter.outputImage?.transformed(by: transform) {
- return UIImage(ciImage: output)
- }
- }
- return nil
- }
-
- private func queryMessageReply(message_id: String) -> [String: Any?] {
- var dataQuery: [String: Any] = [:]
- Database().database?.inTransaction({ fmdb, rollback in
- if let c = Database().getRecords(fmdb: fmdb, query: "SELECT message_id, f_pin, message_text, attachment_flag, thumb_id, image_id, video_id, file_id FROM MESSAGE where message_id='\(message_id)'"), c.next() {
- dataQuery["message_id"] = c.string(forColumnIndex: 0)
- dataQuery["f_pin"] = c.string(forColumnIndex: 1)
- dataQuery["message_text"] = c.string(forColumnIndex: 2)
- dataQuery["attachment_flag"] = c.string(forColumnIndex: 3)
- dataQuery["thumb_id"] = c.string(forColumnIndex: 4)
- dataQuery["image_id"] = c.string(forColumnIndex: 5)
- dataQuery["video_id"] = c.string(forColumnIndex: 6)
- dataQuery["file_id"] = c.string(forColumnIndex: 7)
- c.close()
- }
- })
- return dataQuery
- }
-
- private func getDataProfile(f_pin: String, message_id: String) -> [String: String]{
- var data: [String: String] = [:]
- Database().database?.inTransaction({ fmdb, rollback in
- if let c = Database().getRecords(fmdb: fmdb, query: "select first_name || ' ' || last_name, image_id from BUDDY where f_pin = '\(f_pin)'"), c.next() {
- data["name"] = c.string(forColumnIndex: 0)!.trimmingCharacters(in: .whitespacesAndNewlines)
- data["image_id"] = c.string(forColumnIndex: 1)!
- c.close()
- }
- else if let c = Database().getRecords(fmdb: fmdb, query: "select first_name || ' ' || last_name, thumb_id from GROUPZ_MEMBER where f_pin = '\(f_pin)' AND group_id = '\(dataGroup["group_id"]!!)'"), c.next() {
- data["name"] = c.string(forColumnIndex: 0)!.trimmingCharacters(in: .whitespacesAndNewlines)
- data["image_id"] = c.string(forColumnIndex: 1)!
- c.close()
- } else if let c = Database().getRecords(fmdb: fmdb, query: "select f_display_name from MESSAGE where message_id = '\(message_id)'"), c.next() {
- data["name"] = c.string(forColumnIndex: 0)!
- data["image_id"] = ""
- c.close()
- } else {
- data["name"] = "Unknown".localized()
- }
- })
- return data
- }
-
- @objc func tapMessageText(_ sender: ObjectGesture) {
- var stringURl = sender.message_id.lowercased()
- if stringURl.starts(with: "www.") {
- stringURl = "https://" + stringURl.replacingOccurrences(of: "www.", with: "")
- }
- guard let url = URL(string: stringURl) else { return }
- UIApplication.shared.open(url)
- }
- }
|