MessageInfo.swift 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. //
  2. // MessageInfo.swift
  3. // NexilisLite
  4. //
  5. // Created by Qindi on 11/08/22.
  6. //
  7. import UIKit
  8. class MessageInfo: UIViewController, UITableViewDelegate, UITableViewDataSource {
  9. var data: [String: Any?] = [:]
  10. var dataStatus: [[String: Any?]] = []
  11. private var tableStatus: UITableView!
  12. var dateMessage = ""
  13. var dataPerson: [String: String?] = [:]
  14. var dataGroup: [String: Any?] = [:]
  15. var isPersonal = true
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. title = "Message Info".localized()
  19. view.backgroundColor = .white
  20. navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
  21. navigationController?.navigationBar.topItem?.backButtonTitle = "Back".localized()
  22. tableStatus = UITableView(frame: .zero, style: .grouped)
  23. tableStatus.register(UITableViewCell.self, forCellReuseIdentifier: "cellStatus")
  24. tableStatus.dataSource = self
  25. tableStatus.delegate = self
  26. tableStatus.separatorStyle = .none
  27. tableStatus.bounces = false
  28. getData()
  29. dateMessage = chatDate(stringDate: data["server_date"] as! String)
  30. tableStatus.reloadData()
  31. view.addSubview(tableStatus)
  32. tableStatus.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor)
  33. NotificationCenter.default.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusChat), object: nil)
  34. }
  35. @objc func onStatusChat(notification: NSNotification) {
  36. DispatchQueue.main.async{
  37. self.dataStatus.removeAll()
  38. self.getData()
  39. self.tableStatus.reloadData()
  40. }
  41. }
  42. private func getData() {
  43. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  44. 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"]!!)'") {
  45. var listStatus: [Int] = []
  46. while cursorData.next() {
  47. var row: [String: Any?] = [:]
  48. row["f_pin"] = cursorData.string(forColumnIndex: 0) ?? ""
  49. row["status"] = cursorData.string(forColumnIndex: 1) ?? ""
  50. row["time_delivered"] = cursorData.string(forColumnIndex: 2) ?? ""
  51. row["time_read"] = cursorData.string(forColumnIndex: 3) ?? ""
  52. row["time_ack"] = cursorData.string(forColumnIndex: 4) ?? ""
  53. dataStatus.append(row)
  54. listStatus.append(Int(row["status"] as! String)!)
  55. }
  56. data["status"] = "\(listStatus.min() ?? 2)"
  57. cursorData.close()
  58. }
  59. })
  60. }
  61. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  62. let containerViewStatus = UIView()
  63. containerViewStatus.backgroundColor = .darkGray
  64. let viewStatus = UIView()
  65. containerViewStatus.addSubview(viewStatus)
  66. viewStatus.anchor(top: containerViewStatus.topAnchor, left: containerViewStatus.leftAnchor, bottom: containerViewStatus.bottomAnchor, right: containerViewStatus.rightAnchor, height: 50)
  67. let imageStatus = UIImageView()
  68. viewStatus.addSubview(imageStatus)
  69. imageStatus.anchor(left: viewStatus.leftAnchor, bottom: viewStatus.bottomAnchor, paddingLeft: 15, paddingBottom: 5, width: 15, height: 15)
  70. let textStatus = UILabel()
  71. textStatus.font = .systemFont(ofSize: 12)
  72. textStatus.textColor = .white
  73. viewStatus.addSubview(textStatus)
  74. textStatus.anchor(left: imageStatus.rightAnchor, bottom: viewStatus.bottomAnchor, paddingLeft: 5.0, paddingBottom: 5.0)
  75. if section == 0 {
  76. let containerView = UIView()
  77. containerView.backgroundColor = .clear
  78. let dateView = UIView()
  79. containerView.addSubview(dateView)
  80. dateView.translatesAutoresizingMaskIntoConstraints = false
  81. var topAnchor = dateView.topAnchor.constraint(equalTo: containerView.topAnchor)
  82. if section == 0 {
  83. topAnchor = dateView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 10.0)
  84. }
  85. NSLayoutConstraint.activate([
  86. topAnchor,
  87. dateView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
  88. dateView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor),
  89. dateView.heightAnchor.constraint(equalToConstant: 30),
  90. dateView.widthAnchor.constraint(greaterThanOrEqualToConstant: 60)
  91. ])
  92. dateView.backgroundColor = .orangeColor
  93. dateView.layer.cornerRadius = 15.0
  94. dateView.clipsToBounds = true
  95. let labelDate = UILabel()
  96. dateView.addSubview(labelDate)
  97. labelDate.translatesAutoresizingMaskIntoConstraints = false
  98. NSLayoutConstraint.activate([
  99. labelDate.centerYAnchor.constraint(equalTo: dateView.centerYAnchor),
  100. labelDate.centerXAnchor.constraint(equalTo: dateView.centerXAnchor),
  101. labelDate.leadingAnchor.constraint(equalTo: dateView.leadingAnchor, constant: 10),
  102. labelDate.trailingAnchor.constraint(equalTo: dateView.trailingAnchor, constant: -10),
  103. ])
  104. labelDate.textAlignment = .center
  105. labelDate.textColor = .secondaryColor
  106. labelDate.font = UIFont.systemFont(ofSize: 12, weight: .medium)
  107. labelDate.text = dateMessage
  108. return containerView
  109. } else if section == 1 {
  110. if !data.isEmpty && data["read_receipts"] as? String == "8" {
  111. imageStatus.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  112. textStatus.text = ("Confirmed".localized() + " " + "by".localized()).uppercased()
  113. } else {
  114. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  115. textStatus.text = ("Read".localized() + " " + "by".localized()).uppercased()
  116. }
  117. } else if section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
  118. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  119. textStatus.text = ("Read".localized() + " " + "by".localized()).uppercased()
  120. } else {
  121. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  122. textStatus.text = ("Delivered".localized() + " " + "to".localized()).uppercased()
  123. }
  124. return containerViewStatus
  125. }
  126. func numberOfSections(in tableView: UITableView) -> Int {
  127. if !isPersonal {
  128. var numberSection = 4
  129. if !data.isEmpty && data["read_receipts"] as? String != "8" {
  130. numberSection -= 1
  131. }
  132. if dataStatus.count == dataStatus.filter({ !($0["time_delivered"] as! String).isEmpty && !($0["time_read"] as! String).isEmpty }).count {
  133. numberSection -= 1
  134. }
  135. if !data.isEmpty && data["read_receipts"] as? String == "8" {
  136. if dataStatus.count == dataStatus.filter({ !($0["time_read"] as! String).isEmpty && !($0["time_ack"] as! String).isEmpty }).count{
  137. numberSection -= 1
  138. }
  139. }
  140. return numberSection
  141. }
  142. return 1
  143. }
  144. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  145. if isPersonal {
  146. if !data.isEmpty && data["read_receipts"] as? String == "8" {
  147. return 4
  148. }
  149. return 3
  150. }
  151. if section == 0 {
  152. return 1
  153. } else if section == 1 {
  154. if !data.isEmpty && data["read_receipts"] as? String == "8" {
  155. return dataStatus.filter({ ($0["status"] as! String) == "8" }).count + 1
  156. } else {
  157. return dataStatus.filter({ ($0["status"] as! String) == "4" }).count + 1
  158. }
  159. } else if section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
  160. return dataStatus.filter({ ($0["status"] as! String) == "4" }).count + 1
  161. }
  162. return dataStatus.filter({ ($0["status"] as! String) == "3" }).count + 1
  163. }
  164. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  165. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  166. let cell = tableView.dequeueReusableCell(withIdentifier: "cellStatus", for: indexPath as IndexPath)
  167. cell.selectionStyle = .none
  168. cell.backgroundColor = .clear
  169. cell.accessoryView = nil
  170. cell.contentConfiguration = nil
  171. cell.contentView.subviews.forEach({ $0.removeFromSuperview() })
  172. if !isPersonal {
  173. if indexPath.section != 0 {
  174. let imageNil = UIImageView()
  175. imageNil.image = UIImage(systemName: "ellipsis")
  176. imageNil.contentMode = .center
  177. imageNil.tintColor = .black
  178. let dataStatusAck = dataStatus.filter({ ($0["status"] as! String) == "8" })
  179. let dataStatusRead = dataStatus.filter({ ($0["status"] as! String) == "4" })
  180. let dataStatusDelivered = dataStatus.filter({ ($0["status"] as! String) == "3" })
  181. cell.backgroundColor = .white
  182. var content = cell.defaultContentConfiguration()
  183. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  184. content.imageProperties.maximumSize = CGSize(width: 24, height: 24)
  185. if indexPath.section == 1 {
  186. if !data.isEmpty && data["read_receipts"] as? String == "8" {
  187. if dataStatusAck.count == 0 || indexPath.row == dataStatusAck.count {
  188. cell.contentView.addSubview(imageNil)
  189. imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
  190. } else {
  191. let dataProfile = getDataProfile(f_pin: dataStatusAck[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
  192. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  193. getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  194. content.image = image
  195. })
  196. content.text = dataProfile["name"]!
  197. let date = Date(milliseconds: Int64(dataStatusAck[indexPath.row]["time_ack"] as! String) ?? 100)
  198. let formatter = DateFormatter()
  199. formatter.dateFormat = "HH:mm"
  200. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  201. let time = formatter.string(from: date as Date)
  202. let viewTimeStatus = UIView()
  203. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  204. let titleTime = UILabel()
  205. viewTimeStatus.addSubview(titleTime)
  206. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  207. titleTime.font = .systemFont(ofSize: 12)
  208. titleTime.text = "\(chatDate(stringDate: dataStatusAck[indexPath.row]["time_ack"] as! String)) \(time)"
  209. cell.accessoryView = viewTimeStatus
  210. cell.contentConfiguration = content
  211. }
  212. } else {
  213. if dataStatusRead.count == 0 || indexPath.row == dataStatusRead.count {
  214. cell.contentView.addSubview(imageNil)
  215. imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
  216. } else {
  217. let dataProfile = getDataProfile(f_pin: dataStatusRead[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
  218. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  219. getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  220. content.image = image
  221. })
  222. content.text = dataProfile["name"]!
  223. let date = Date(milliseconds: Int64(dataStatusRead[indexPath.row]["time_read"] as! String) ?? 100)
  224. let formatter = DateFormatter()
  225. formatter.dateFormat = "HH:mm"
  226. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  227. let time = formatter.string(from: date as Date)
  228. let viewTimeStatus = UIView()
  229. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  230. let titleTime = UILabel()
  231. viewTimeStatus.addSubview(titleTime)
  232. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  233. titleTime.font = .systemFont(ofSize: 12)
  234. titleTime.text = "\(chatDate(stringDate: dataStatusRead[indexPath.row]["time_read"] as! String)) \(time)"
  235. cell.accessoryView = viewTimeStatus
  236. cell.contentConfiguration = content
  237. }
  238. }
  239. } else if indexPath.section == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
  240. if dataStatusRead.count == 0 || indexPath.row == dataStatusRead.count {
  241. cell.contentView.addSubview(imageNil)
  242. imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
  243. } else {
  244. let dataProfile = getDataProfile(f_pin: dataStatusRead[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
  245. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  246. getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  247. content.image = image
  248. })
  249. content.text = dataProfile["name"]!
  250. let date = Date(milliseconds: Int64(dataStatusRead[indexPath.row]["time_read"] as! String) ?? 100)
  251. let formatter = DateFormatter()
  252. formatter.dateFormat = "HH:mm"
  253. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  254. let time = formatter.string(from: date as Date)
  255. let viewTimeStatus = UIView()
  256. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  257. let titleTime = UILabel()
  258. viewTimeStatus.addSubview(titleTime)
  259. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  260. titleTime.font = .systemFont(ofSize: 12)
  261. titleTime.text = "\(chatDate(stringDate: dataStatusRead[indexPath.row]["time_read"] as! String)) \(time)"
  262. cell.accessoryView = viewTimeStatus
  263. cell.contentConfiguration = content
  264. }
  265. } else {
  266. if dataStatusDelivered.count == 0 || indexPath.row == dataStatusDelivered.count {
  267. cell.contentView.addSubview(imageNil)
  268. imageNil.anchor(centerX: cell.centerXAnchor, centerY: cell.centerYAnchor, width: 50, height: 20)
  269. } else {
  270. let dataProfile = getDataProfile(f_pin: dataStatusDelivered[indexPath.row]["f_pin"] as! String, message_id: data["message_id"] as! String)
  271. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  272. getImage(name: dataProfile["image_id"]!, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  273. content.image = image
  274. })
  275. content.text = dataProfile["name"]!
  276. let date = Date(milliseconds: Int64(dataStatusDelivered[indexPath.row]["time_delivered"] as! String) ?? 100)
  277. let formatter = DateFormatter()
  278. formatter.dateFormat = "HH:mm"
  279. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  280. let time = formatter.string(from: date as Date)
  281. let viewTimeStatus = UIView()
  282. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  283. let titleTime = UILabel()
  284. viewTimeStatus.addSubview(titleTime)
  285. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  286. titleTime.font = .systemFont(ofSize: 12)
  287. titleTime.text = "\(chatDate(stringDate: dataStatusDelivered[indexPath.row]["time_delivered"] as! String)) \(time)"
  288. cell.accessoryView = viewTimeStatus
  289. cell.contentConfiguration = content
  290. }
  291. }
  292. return cell
  293. }
  294. }
  295. if indexPath.row != 0 {
  296. cell.backgroundColor = .white
  297. var content = cell.defaultContentConfiguration()
  298. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  299. content.imageProperties.maximumSize = CGSize(width: 24, height: 24)
  300. let noStatus = UIImageView(frame: CGRect(x: 0, y: cell.frame.height / 2, width: 50, height: 20))
  301. noStatus.image = UIImage(systemName: "ellipsis")
  302. noStatus.contentMode = .center
  303. noStatus.tintColor = .black
  304. if indexPath.row == 1 {
  305. if !data.isEmpty && data["read_receipts"] as? String == "8"{
  306. content.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  307. content.text = "Confirmed".localized()
  308. if dataStatus.count != 0 {
  309. if (dataStatus[0]["time_ack"] as! String).isEmpty {
  310. cell.accessoryView = noStatus
  311. } else {
  312. let date = Date(milliseconds: Int64(dataStatus[0]["time_ack"] as! String) ?? 100)
  313. let formatter = DateFormatter()
  314. formatter.dateFormat = "HH:mm"
  315. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  316. let time = formatter.string(from: date as Date)
  317. let viewTimeStatus = UIView()
  318. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  319. let titleTime = UILabel()
  320. viewTimeStatus.addSubview(titleTime)
  321. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  322. titleTime.font = .systemFont(ofSize: 12)
  323. titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_ack"] as! String)) \(time)"
  324. cell.accessoryView = viewTimeStatus
  325. }
  326. }
  327. } else {
  328. content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  329. content.text = "Read".localized()
  330. if dataStatus.count != 0 {
  331. if (dataStatus[0]["time_read"] as! String).isEmpty {
  332. cell.accessoryView = noStatus
  333. } else {
  334. let date = Date(milliseconds: Int64(dataStatus[0]["time_read"] as! String) ?? 100)
  335. let formatter = DateFormatter()
  336. formatter.dateFormat = "HH:mm"
  337. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  338. let time = formatter.string(from: date as Date)
  339. let viewTimeStatus = UIView()
  340. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  341. let titleTime = UILabel()
  342. viewTimeStatus.addSubview(titleTime)
  343. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  344. titleTime.font = .systemFont(ofSize: 12)
  345. titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_read"] as! String)) \(time)"
  346. cell.accessoryView = viewTimeStatus
  347. }
  348. }
  349. }
  350. } else if indexPath.row == 2 && !data.isEmpty && data["read_receipts"] as? String == "8" {
  351. content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  352. content.text = "Read".localized()
  353. if dataStatus.count != 0 {
  354. if (dataStatus[0]["time_read"] as! String).isEmpty {
  355. cell.accessoryView = noStatus
  356. } else {
  357. let date = Date(milliseconds: Int64(dataStatus[0]["time_read"] as! String) ?? 100)
  358. let formatter = DateFormatter()
  359. formatter.dateFormat = "HH:mm"
  360. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  361. let time = formatter.string(from: date as Date)
  362. let viewTimeStatus = UIView()
  363. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  364. let titleTime = UILabel()
  365. viewTimeStatus.addSubview(titleTime)
  366. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  367. titleTime.font = .systemFont(ofSize: 12)
  368. titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_read"] as! String)) \(time)"
  369. cell.accessoryView = viewTimeStatus
  370. }
  371. }
  372. } else {
  373. content.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  374. content.text = "Delivered".localized()
  375. if (dataStatus[0]["time_delivered"] as! String).isEmpty {
  376. cell.accessoryView = noStatus
  377. } else {
  378. let date = Date(milliseconds: Int64(dataStatus[0]["time_delivered"] as! String) ?? 100)
  379. let formatter = DateFormatter()
  380. formatter.dateFormat = "HH:mm"
  381. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  382. let time = formatter.string(from: date as Date)
  383. let viewTimeStatus = UIView()
  384. viewTimeStatus.frame = CGRect(x: 0, y: 0, width: 80, height: cell.frame.height)
  385. let titleTime = UILabel()
  386. viewTimeStatus.addSubview(titleTime)
  387. titleTime.anchor(centerX: viewTimeStatus.centerXAnchor, centerY: viewTimeStatus.centerYAnchor)
  388. titleTime.font = .systemFont(ofSize: 12)
  389. titleTime.text = "\(chatDate(stringDate: dataStatus[0]["time_delivered"] as! String)) \(time)"
  390. cell.accessoryView = viewTimeStatus
  391. }
  392. }
  393. cell.contentConfiguration = content
  394. } else {
  395. let thumbChat = (data["thumb_id"] as? String) ?? ""
  396. let imageChat = (data["image_id"] as? String) ?? ""
  397. let videoChat = (data["video_id"] as? String) ?? ""
  398. let fileChat = (data["file_id"] as? String) ?? ""
  399. let reffChat = (data["reff_id"] as? String) ?? ""
  400. cell.backgroundColor = .clear
  401. cell.selectionStyle = .none
  402. let containerMessage = UIView()
  403. cell.contentView.addSubview(containerMessage)
  404. containerMessage.translatesAutoresizingMaskIntoConstraints = false
  405. let timeMessage = UILabel()
  406. cell.contentView.addSubview(timeMessage)
  407. timeMessage.translatesAutoresizingMaskIntoConstraints = false
  408. if (data["read_receipts"] as? String) == "8" {
  409. timeMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -40).isActive = true
  410. } else {
  411. timeMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -5).isActive = true
  412. }
  413. let statusMessage = UIImageView()
  414. containerMessage.leadingAnchor.constraint(greaterThanOrEqualTo: cell.contentView.leadingAnchor, constant: 60).isActive = true
  415. if (data["read_receipts"] as? String) == "8" {
  416. containerMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -40).isActive = true
  417. } else {
  418. containerMessage.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: -5).isActive = true
  419. }
  420. containerMessage.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 5).isActive = true
  421. containerMessage.trailingAnchor.constraint(equalTo: cell.contentView.trailingAnchor, constant: -15).isActive = true
  422. containerMessage.widthAnchor.constraint(greaterThanOrEqualToConstant: 46).isActive = true
  423. if (data["attachment_flag"] as? String == "11" && data["reff_id"]as? String == "") {
  424. containerMessage.backgroundColor = .clear
  425. } else {
  426. containerMessage.backgroundColor = .blueBubbleColor
  427. }
  428. containerMessage.layer.cornerRadius = 10.0
  429. containerMessage.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner, .layerMinXMinYCorner]
  430. containerMessage.clipsToBounds = true
  431. timeMessage.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
  432. cell.contentView.addSubview(statusMessage)
  433. statusMessage.translatesAutoresizingMaskIntoConstraints = false
  434. statusMessage.bottomAnchor.constraint(equalTo: timeMessage.topAnchor).isActive = true
  435. statusMessage.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
  436. statusMessage.widthAnchor.constraint(equalToConstant: 15).isActive = true
  437. statusMessage.heightAnchor.constraint(equalToConstant: 15).isActive = true
  438. if (data["status"]! as! String == "1" || data["status"]! as! String == "2" ) {
  439. statusMessage.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  440. } else if (data["status"]! as! String == "3") {
  441. statusMessage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  442. } else if (data["status"]! as! String == "8") {
  443. statusMessage.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  444. } else {
  445. statusMessage.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  446. }
  447. if data["is_stared"] as? String == "1" {
  448. let imageStared = UIImageView()
  449. cell.contentView.addSubview(imageStared)
  450. imageStared.translatesAutoresizingMaskIntoConstraints = false
  451. imageStared.bottomAnchor.constraint(equalTo: statusMessage.topAnchor).isActive = true
  452. imageStared.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: -8).isActive = true
  453. imageStared.widthAnchor.constraint(equalToConstant: 15).isActive = true
  454. imageStared.heightAnchor.constraint(equalToConstant: 15).isActive = true
  455. imageStared.image = UIImage(systemName: "star.fill")
  456. imageStared.backgroundColor = .clear
  457. imageStared.tintColor = .systemYellow
  458. }
  459. if data["read_receipts"] as? String == "8" {
  460. let imageAckView = UIImageView()
  461. var imageAck = UIImage(named: "ack_icon_gray", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  462. if data["status"] as? String == "8" {
  463. imageAck = UIImage(named: "ack_icon", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  464. }
  465. imageAckView.image = imageAck
  466. cell.contentView.addSubview(imageAckView)
  467. imageAckView.translatesAutoresizingMaskIntoConstraints = false
  468. imageAckView.widthAnchor.constraint(equalToConstant: 30).isActive = true
  469. imageAckView.heightAnchor.constraint(equalToConstant: 30).isActive = true
  470. imageAckView.topAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: 5).isActive = true
  471. imageAckView.trailingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 30).isActive = true
  472. }
  473. let messageText = UILabel()
  474. messageText.numberOfLines = 0
  475. messageText.lineBreakMode = .byWordWrapping
  476. containerMessage.addSubview(messageText)
  477. messageText.translatesAutoresizingMaskIntoConstraints = false
  478. let topMarginText = messageText.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15)
  479. topMarginText.isActive = true
  480. messageText.textColor = .black
  481. if data["attachment_flag"] as? String == "27" || data["attachment_flag"] as? String == "26" || data["message_scope_id"] as? String == "18" {
  482. messageText.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 85).isActive = true
  483. let imageLS = UIImageView()
  484. containerMessage.addSubview(imageLS)
  485. imageLS.translatesAutoresizingMaskIntoConstraints = false
  486. NSLayoutConstraint.activate([
  487. imageLS.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15.0),
  488. imageLS.trailingAnchor.constraint(equalTo: messageText.leadingAnchor, constant: -10.0),
  489. imageLS.centerYAnchor.constraint(equalTo: containerMessage.centerYAnchor),
  490. imageLS.heightAnchor.constraint(equalToConstant: 60.0)
  491. ])
  492. if data["attachment_flag"] as! String == "26" {
  493. imageLS.image = UIImage(named: "pb_seminar_wpr", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  494. } else if data["attachment_flag"] as! String == "27" {
  495. imageLS.image = UIImage(named: "pb_live_tv", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  496. } else if data["message_scope_id"] as? String == "18" {
  497. imageLS.image = UIImage(systemName: "doc.richtext.fill")
  498. imageLS.tintColor = .mainColor
  499. }
  500. } else {
  501. messageText.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
  502. }
  503. 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") {
  504. messageText.bottomAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: -115).isActive = true
  505. let imageQR = UIImageView()
  506. containerMessage.addSubview(imageQR)
  507. imageQR.translatesAutoresizingMaskIntoConstraints = false
  508. NSLayoutConstraint.activate([
  509. imageQR.centerXAnchor.constraint(equalTo: containerMessage.centerXAnchor),
  510. imageQR.topAnchor.constraint(equalTo: messageText.bottomAnchor),
  511. imageQR.widthAnchor.constraint(equalToConstant: 100.0),
  512. imageQR.heightAnchor.constraint(equalToConstant: 100.0)
  513. ])
  514. imageQR.image = generateQRCode(from: data["blog_id"] as! String)
  515. } else {
  516. messageText.bottomAnchor.constraint(equalTo: containerMessage.bottomAnchor, constant: -15).isActive = true
  517. }
  518. messageText.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
  519. var textChat = (data["message_text"] as? String) ?? ""
  520. if (data["lock"] != nil && (data["lock"])! as? String == "1") {
  521. if (data["f_pin"] as? String == idMe) {
  522. textChat = "🚫 _"+"You were deleted this message".localized()+"_"
  523. } else {
  524. textChat = "🚫 _"+"This message was deleted".localized()+"_"
  525. }
  526. }
  527. let imageSticker = UIImageView()
  528. if let attachmentFlag = data["attachment_flag"], let attachmentFlag = attachmentFlag as? String {
  529. if attachmentFlag == "27" || attachmentFlag == "26" { // live streaming
  530. let data = textChat
  531. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [String: Any] {
  532. Database().database?.inTransaction({ fmdb, rollback in
  533. let title = json["title"] as? String ?? ""
  534. let description = json["description"] as? String ?? ""
  535. let start = json["time"] as? Int64 ?? 0
  536. let by = json["by"] as? String ?? ""
  537. var type = "*Live Streaming*"
  538. if attachmentFlag == "26" {
  539. type = "*Seminar*"
  540. }
  541. if let c = Database().getRecords(fmdb: fmdb, query: "select first_name || ' ' || last_name from BUDDY where f_pin = '\(by)'"), c.next() {
  542. let name = c.string(forColumnIndex: 0)!
  543. messageText.attributedText = "\(type) \nTitle: \(title) \nDescription: \(description) \nStart: \(Date(milliseconds: start).format(dateFormat: "dd/MM/yyyy HH:mm")) \nBroadcaster: \(name)".richText()
  544. c.close()
  545. } else {
  546. messageText.attributedText = ("\(type) \nTitle: \(title) \nDescription: \(description) \nStart: \(Date(milliseconds: start).format(dateFormat: "dd/MM/yyyy HH:mm"))").richText()
  547. }
  548. })
  549. }
  550. }
  551. else if attachmentFlag == "11" {
  552. messageText.text = ""
  553. topMarginText.constant = topMarginText.constant + 100
  554. containerMessage.addSubview(imageSticker)
  555. imageSticker.translatesAutoresizingMaskIntoConstraints = false
  556. if (reffChat == "") {
  557. imageSticker.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
  558. imageSticker.widthAnchor.constraint(equalToConstant: 80).isActive = true
  559. } else {
  560. imageSticker.widthAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true
  561. }
  562. imageSticker.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
  563. imageSticker.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
  564. imageSticker.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
  565. imageSticker.image = UIImage(named: (textChat.components(separatedBy: "/")[1]), in: Bundle.resourceBundle(for: Nexilis.self), with: nil) //resourcesMediaBundle
  566. imageSticker.contentMode = .scaleAspectFit
  567. } else if data["message_scope_id"] as! String == "18" {
  568. let data = textChat
  569. if let jsonForm = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [String: Any] {
  570. let form_title = jsonForm["form_title"] as! String
  571. let club_type = jsonForm["club_type"] as! String
  572. let province = jsonForm["province"] as! String
  573. let club = jsonForm["club"] as! String
  574. messageText.attributedText = "*\(form_title.replacingOccurrences(of: "+", with: " "))* \nClub Type: \(club_type) \nProvince: \(province) \nClub Name: \(club) ".richText()
  575. }
  576. }
  577. else {
  578. messageText.attributedText = textChat.richText()
  579. }
  580. } else {
  581. messageText.attributedText = textChat.richText()
  582. }
  583. messageText.isUserInteractionEnabled = true
  584. if !textChat.isEmpty {
  585. let listText = textChat.split(separator: " ")
  586. for i in 0...listText.count - 1 {
  587. if listText[i].lowercased().checkStartWithLink() {
  588. 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 {
  589. let objectGesture = ObjectGesture(target: self, action: #selector(tapMessageText(_:)))
  590. objectGesture.message_id = "\(listText[i])"
  591. messageText.addGestureRecognizer(objectGesture)
  592. }
  593. }
  594. }
  595. }
  596. let stringDate = (data["server_date"] as? String) ?? ""
  597. if !stringDate.isEmpty {
  598. let date = Date(milliseconds: Int64(stringDate) ?? 100)
  599. let formatter = DateFormatter()
  600. formatter.dateFormat = "HH:mm"
  601. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  602. timeMessage.text = formatter.string(from: date as Date)
  603. timeMessage.font = UIFont.systemFont(ofSize: 10, weight: .medium)
  604. timeMessage.textColor = .lightGray
  605. }
  606. let imageThumb = UIImageView()
  607. let containerViewFile = UIView()
  608. if (!thumbChat.isEmpty) {
  609. topMarginText.constant = topMarginText.constant + ListGroupImages.getImageSize(image: thumbChat, screenWidth: self.view.frame.size.width * 0.6, screenHeight: 305)!.height
  610. containerMessage.addSubview(imageThumb)
  611. imageThumb.translatesAutoresizingMaskIntoConstraints = false
  612. let dataReply = queryMessageReply(message_id: reffChat)
  613. if (dataReply.count == 0) {
  614. imageThumb.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
  615. }
  616. imageThumb.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
  617. imageThumb.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
  618. imageThumb.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
  619. imageThumb.widthAnchor.constraint(equalToConstant: ListGroupImages.getImageSize(image: thumbChat, screenWidth: self.view.frame.size.width * 0.6, screenHeight: 305)!.width).isActive = true
  620. imageThumb.layer.cornerRadius = 5.0
  621. imageThumb.clipsToBounds = true
  622. imageThumb.contentMode = .scaleAspectFill
  623. let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
  624. let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
  625. let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
  626. if let dirPath = paths.first {
  627. let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumbChat)
  628. let image = UIImage(contentsOfFile: thumbURL.path)
  629. // let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
  630. imageThumb.image = image
  631. let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(videoChat)
  632. let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(imageChat)
  633. if !FileManager.default.fileExists(atPath: imageURL.path) || !FileManager.default.fileExists(atPath: videoURL.path) {
  634. let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)
  635. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  636. blurEffectView.frame = CGRect(x: 0, y: 0, width: imageThumb.frame.size.width, height: imageThumb.frame.size.height)
  637. blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  638. imageThumb.addSubview(blurEffectView)
  639. if !imageChat.isEmpty {
  640. let imageDownload = UIImageView(image: UIImage(systemName: "arrow.down.circle.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 50, weight: .bold, scale: .default)))
  641. imageThumb.addSubview(imageDownload)
  642. imageDownload.tintColor = .black.withAlphaComponent(0.3)
  643. imageDownload.translatesAutoresizingMaskIntoConstraints = false
  644. imageDownload.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
  645. imageDownload.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
  646. }
  647. }
  648. }
  649. if (videoChat != "") {
  650. 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))
  651. imagePlay.circle()
  652. imageThumb.addSubview(imagePlay)
  653. imagePlay.backgroundColor = .black.withAlphaComponent(0.3)
  654. imagePlay.translatesAutoresizingMaskIntoConstraints = false
  655. imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
  656. imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
  657. }
  658. if (data["progress"] as! Double != 100.0 && data["f_pin"] as? String == idMe) {
  659. let container = UIView()
  660. imageThumb.addSubview(container)
  661. container.translatesAutoresizingMaskIntoConstraints = false
  662. container.bottomAnchor.constraint(equalTo: imageThumb.bottomAnchor, constant: -10).isActive = true
  663. container.leadingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: 10).isActive = true
  664. container.widthAnchor.constraint(equalToConstant: 30).isActive = true
  665. container.heightAnchor.constraint(equalToConstant: 30).isActive = true
  666. container.backgroundColor = .white.withAlphaComponent(0.1)
  667. let circlePath = UIBezierPath(arcCenter: CGPoint(x: 10, y: 20), radius: 15, startAngle: -(.pi / 2), endAngle: .pi * 2, clockwise: true)
  668. let trackShape = CAShapeLayer()
  669. trackShape.path = circlePath.cgPath
  670. trackShape.fillColor = UIColor.black.withAlphaComponent(0.3).cgColor
  671. trackShape.lineWidth = 3
  672. trackShape.strokeColor = UIColor.blueBubbleColor.withAlphaComponent(0.3).cgColor
  673. container.backgroundColor = .clear
  674. container.layer.addSublayer(trackShape)
  675. let shapeLoading = CAShapeLayer()
  676. shapeLoading.path = circlePath.cgPath
  677. shapeLoading.fillColor = UIColor.clear.cgColor
  678. shapeLoading.lineWidth = 3
  679. shapeLoading.strokeEnd = 0
  680. shapeLoading.strokeColor = UIColor.blueBubbleColor.cgColor
  681. container.layer.addSublayer(shapeLoading)
  682. let imageupload = UIImageView(image: UIImage(systemName: "arrow.up", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
  683. imageupload.tintColor = .white
  684. container.addSubview(imageupload)
  685. imageupload.translatesAutoresizingMaskIntoConstraints = false
  686. imageupload.bottomAnchor.constraint(equalTo: imageThumb.bottomAnchor, constant: -10).isActive = true
  687. imageupload.leadingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: 10).isActive = true
  688. imageupload.widthAnchor.constraint(equalToConstant: 20).isActive = true
  689. imageupload.heightAnchor.constraint(equalToConstant: 20).isActive = true
  690. }
  691. }
  692. if (fileChat != "") && data["message_scope_id"] as! String != "18" {
  693. topMarginText.constant = topMarginText.constant + 55
  694. let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
  695. let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
  696. let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
  697. let arrExtFile = (textChat.components(separatedBy: "|")[0]).split(separator: ".")
  698. let finalExtFile = arrExtFile[arrExtFile.count - 1]
  699. if let dirPath = paths.first {
  700. let fileURL = URL(fileURLWithPath: dirPath).appendingPathComponent(fileChat)
  701. if let dataFile = try? Data(contentsOf: fileURL) {
  702. var sizeOfFile = Int(dataFile.count / 1000000)
  703. if (sizeOfFile < 1) {
  704. sizeOfFile = Int(dataFile.count / 1000)
  705. if (finalExtFile.count > 4) {
  706. messageText.text = "\(sizeOfFile) kB \u{2022} TXT"
  707. }else {
  708. messageText.text = "\(sizeOfFile) kB \u{2022} \(finalExtFile.uppercased())"
  709. }
  710. } else {
  711. if (finalExtFile.count > 4) {
  712. messageText.text = "\(sizeOfFile) MB \u{2022} TXT"
  713. }else {
  714. messageText.text = "\(sizeOfFile) MB \u{2022} \(finalExtFile.uppercased())"
  715. }
  716. }
  717. } else {
  718. messageText.text = ""
  719. }
  720. }
  721. containerMessage.addSubview(containerViewFile)
  722. containerViewFile.translatesAutoresizingMaskIntoConstraints = false
  723. let dataReply = queryMessageReply(message_id: reffChat)
  724. if (dataReply.count == 0) {
  725. containerViewFile.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
  726. }
  727. containerViewFile.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
  728. containerViewFile.bottomAnchor.constraint(equalTo:messageText.topAnchor, constant: -5).isActive = true
  729. containerViewFile.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
  730. containerViewFile.heightAnchor.constraint(equalToConstant: 50).isActive = true
  731. containerViewFile.backgroundColor = .black.withAlphaComponent(0.2)
  732. containerViewFile.layer.cornerRadius = 5.0
  733. containerViewFile.clipsToBounds = true
  734. let imageFile = UIImageView(image: UIImage(systemName: "doc.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 30, weight: .bold, scale: .default)))
  735. containerViewFile.addSubview(imageFile)
  736. let nameFile = UILabel()
  737. containerViewFile.addSubview(nameFile)
  738. imageFile.translatesAutoresizingMaskIntoConstraints = false
  739. imageFile.leadingAnchor.constraint(equalTo: containerViewFile.leadingAnchor, constant: 5).isActive = true
  740. imageFile.trailingAnchor.constraint(equalTo: nameFile.leadingAnchor, constant: -5).isActive = true
  741. imageFile.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
  742. imageFile.widthAnchor.constraint(equalToConstant: 30).isActive = true
  743. imageFile.heightAnchor.constraint(equalToConstant: 30).isActive = true
  744. imageFile.tintColor = .docColor
  745. nameFile.translatesAutoresizingMaskIntoConstraints = false
  746. nameFile.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
  747. nameFile.widthAnchor.constraint(lessThanOrEqualToConstant: 200).isActive = true
  748. nameFile.font = UIFont.systemFont(ofSize: 12, weight: .medium)
  749. nameFile.textColor = .white
  750. nameFile.text = textChat.components(separatedBy: "|")[0]
  751. if (data["progress"] as! Double != 100.0) {
  752. let containerLoading = UIView()
  753. containerViewFile.addSubview(containerLoading)
  754. containerLoading.translatesAutoresizingMaskIntoConstraints = false
  755. containerLoading.centerYAnchor.constraint(equalTo: containerViewFile.centerYAnchor).isActive = true
  756. containerLoading.leadingAnchor.constraint(equalTo: nameFile.trailingAnchor, constant: 5).isActive = true
  757. containerLoading.trailingAnchor.constraint(equalTo: containerViewFile.trailingAnchor, constant: -5).isActive = true
  758. containerLoading.widthAnchor.constraint(equalToConstant: 30).isActive = true
  759. containerLoading.heightAnchor.constraint(equalToConstant: 30).isActive = true
  760. let circlePath = UIBezierPath(arcCenter: CGPoint(x: 15, y: 15), radius: 10, startAngle: -(.pi / 2), endAngle: .pi * 2, clockwise: true)
  761. let trackShape = CAShapeLayer()
  762. trackShape.path = circlePath.cgPath
  763. trackShape.fillColor = UIColor.clear.cgColor
  764. trackShape.lineWidth = 5
  765. trackShape.strokeColor = UIColor.blueBubbleColor.withAlphaComponent(0.3).cgColor
  766. containerLoading.layer.addSublayer(trackShape)
  767. let shapeLoading = CAShapeLayer()
  768. shapeLoading.path = circlePath.cgPath
  769. shapeLoading.fillColor = UIColor.clear.cgColor
  770. shapeLoading.lineWidth = 3
  771. shapeLoading.strokeEnd = 0
  772. shapeLoading.strokeColor = UIColor.secondaryColor.cgColor
  773. containerLoading.layer.addSublayer(shapeLoading)
  774. var imageupload = UIImageView(image: UIImage(systemName: "arrow.up", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
  775. if data["f_pin"] as? String != idMe {
  776. imageupload = UIImageView(image: UIImage(systemName: "arrow.down", withConfiguration: UIImage.SymbolConfiguration(pointSize: 10, weight: .bold, scale: .default)))
  777. shapeLoading.strokeColor = UIColor.blueBubbleColor.cgColor
  778. }
  779. imageupload.tintColor = .white
  780. containerLoading.addSubview(imageupload)
  781. imageupload.translatesAutoresizingMaskIntoConstraints = false
  782. imageupload.centerYAnchor.constraint(equalTo: containerLoading.centerYAnchor).isActive = true
  783. imageupload.centerXAnchor.constraint(equalTo: containerLoading.centerXAnchor).isActive = true
  784. } else {
  785. nameFile.trailingAnchor.constraint(equalTo: containerViewFile.trailingAnchor, constant: -5).isActive = true
  786. }
  787. }
  788. if (reffChat != "" && data["message_scope_id"] as! String != "18") {
  789. let dataReply = queryMessageReply(message_id: reffChat)
  790. if dataReply.count != 0 {
  791. topMarginText.constant = topMarginText.constant + 55
  792. let containerReply = UIView()
  793. containerMessage.addSubview(containerReply)
  794. containerReply.translatesAutoresizingMaskIntoConstraints = false
  795. containerReply.leadingAnchor.constraint(equalTo: containerMessage.leadingAnchor, constant: 15).isActive = true
  796. containerReply.topAnchor.constraint(equalTo: containerMessage.topAnchor, constant: 15).isActive = true
  797. if thumbChat != "" {
  798. containerReply.bottomAnchor.constraint(equalTo: imageThumb.topAnchor, constant: -5).isActive = true
  799. } else if fileChat != "" {
  800. containerReply.bottomAnchor.constraint(equalTo: containerViewFile.topAnchor, constant: -5).isActive = true
  801. } else if data["attachment_flag"] as? String == "11" {
  802. containerReply.bottomAnchor.constraint(equalTo: imageSticker.topAnchor, constant: -5).isActive = true
  803. } else {
  804. containerReply.bottomAnchor.constraint(equalTo: messageText.topAnchor, constant: -5).isActive = true
  805. }
  806. containerReply.trailingAnchor.constraint(equalTo: containerMessage.trailingAnchor, constant: -15).isActive = true
  807. containerReply.heightAnchor.constraint(equalToConstant: 50).isActive = true
  808. containerReply.backgroundColor = .black.withAlphaComponent(0.2)
  809. containerReply.layer.cornerRadius = 5
  810. containerReply.clipsToBounds = true
  811. let leftReply = UIView()
  812. containerReply.addSubview(leftReply)
  813. leftReply.translatesAutoresizingMaskIntoConstraints = false
  814. leftReply.leadingAnchor.constraint(equalTo: containerReply.leadingAnchor).isActive = true
  815. leftReply.topAnchor.constraint(equalTo: containerReply.topAnchor).isActive = true
  816. leftReply.bottomAnchor.constraint(equalTo: containerReply.bottomAnchor).isActive = true
  817. leftReply.widthAnchor.constraint(equalToConstant: 3).isActive = true
  818. leftReply.layer.cornerRadius = 5
  819. leftReply.clipsToBounds = true
  820. leftReply.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMinXMinYCorner]
  821. let titleReply = UILabel()
  822. containerReply.addSubview(titleReply)
  823. titleReply.translatesAutoresizingMaskIntoConstraints = false
  824. titleReply.leadingAnchor.constraint(equalTo: leftReply.leadingAnchor, constant: 10).isActive = true
  825. titleReply.topAnchor.constraint(equalTo: containerReply.topAnchor, constant: 10).isActive = true
  826. titleReply.trailingAnchor.constraint(lessThanOrEqualTo: containerReply.trailingAnchor, constant: -20).isActive = true
  827. titleReply.font = UIFont.systemFont(ofSize: 12).bold
  828. if (data["f_pin"] as? String == idMe) {
  829. titleReply.text = "You".localized()
  830. if data["f_pin"] as? String == idMe {
  831. titleReply.textColor = .white
  832. leftReply.backgroundColor = .white
  833. } else {
  834. titleReply.textColor = .mainColor
  835. leftReply.backgroundColor = .mainColor
  836. }
  837. } else {
  838. titleReply.text = dataPerson["name"]!!
  839. if data["f_pin"] as? String == idMe {
  840. titleReply.textColor = .white
  841. leftReply.backgroundColor = .white
  842. }
  843. }
  844. let contentReply = UILabel()
  845. containerReply.addSubview(contentReply)
  846. contentReply.translatesAutoresizingMaskIntoConstraints = false
  847. contentReply.leadingAnchor.constraint(equalTo: leftReply.leadingAnchor, constant: 10).isActive = true
  848. contentReply.bottomAnchor.constraint(equalTo: containerReply.bottomAnchor, constant: -10).isActive = true
  849. contentReply.font = UIFont.systemFont(ofSize: 10)
  850. let message_text = data["message_text"] as! String
  851. let attachment_flag = data["attachment_flag"] as! String
  852. let thumb_chat = data["thumb_id"] as! String
  853. let image_chat = data["image_id"] as! String
  854. let video_chat = data["video_id"] as! String
  855. let file_chat = data["file_id"] as! String
  856. if (attachment_flag == "0" && thumb_chat == "") {
  857. contentReply.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -20).isActive = true
  858. contentReply.attributedText = message_text.richText()
  859. } else if (attachment_flag == "1" || image_chat != "") {
  860. if (message_text == "") {
  861. contentReply.text = "📷 Photo".localized()
  862. } else {
  863. contentReply.attributedText = message_text.richText()
  864. }
  865. } else if (attachment_flag == "2" || video_chat != "") {
  866. if (message_text == "") {
  867. contentReply.text = "📹 Video".localized()
  868. } else {
  869. contentReply.attributedText = message_text.richText()
  870. }
  871. } else if (attachment_flag == "6" || file_chat != ""){
  872. contentReply.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -20).isActive = true
  873. contentReply.text = "📄 \(message_text.components(separatedBy: "|")[0])"
  874. } else if (attachment_flag == "11") {
  875. contentReply.text = "❤️ Sticker"
  876. }
  877. contentReply.textColor = .white.withAlphaComponent(0.8)
  878. if (attachment_flag == "1" || attachment_flag == "2" || image_chat != "" || video_chat != "") {
  879. let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
  880. let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
  881. let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
  882. if let dirPath = paths.first {
  883. let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
  884. let image = UIImage(contentsOfFile: thumbURL.path)
  885. let imageThumb = UIImageView(image: image)
  886. containerReply.addSubview(imageThumb)
  887. imageThumb.layer.cornerRadius = 2.0
  888. imageThumb.clipsToBounds = true
  889. imageThumb.contentMode = .scaleAspectFill
  890. imageThumb.translatesAutoresizingMaskIntoConstraints = false
  891. imageThumb.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
  892. imageThumb.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
  893. imageThumb.widthAnchor.constraint(equalToConstant: 30).isActive = true
  894. imageThumb.heightAnchor.constraint(equalToConstant: 30).isActive = true
  895. if (attachment_flag == "2") {
  896. let imagePlay = UIImageView(image: UIImage(systemName: "play.circle.fill"))
  897. imageThumb.addSubview(imagePlay)
  898. imagePlay.clipsToBounds = true
  899. imagePlay.translatesAutoresizingMaskIntoConstraints = false
  900. imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
  901. imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
  902. imagePlay.widthAnchor.constraint(equalToConstant: 10).isActive = true
  903. imagePlay.heightAnchor.constraint(equalToConstant: 10).isActive = true
  904. imagePlay.tintColor = .white
  905. }
  906. titleReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
  907. contentReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
  908. }
  909. }
  910. if (attachment_flag == "11" && message_text.components(separatedBy: "/").count > 1) {
  911. let imageSticker = UIImageView(image: UIImage(named: (message_text.components(separatedBy: "/")[1]), in: Bundle.resourceBundle(for: Nexilis.self), with: nil))
  912. containerReply.addSubview(imageSticker)
  913. imageSticker.layer.cornerRadius = 2.0
  914. imageSticker.clipsToBounds = true
  915. imageSticker.translatesAutoresizingMaskIntoConstraints = false
  916. imageSticker.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
  917. imageSticker.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
  918. imageSticker.widthAnchor.constraint(equalToConstant: 30).isActive = true
  919. imageSticker.heightAnchor.constraint(equalToConstant: 30).isActive = true
  920. titleReply.trailingAnchor.constraint(equalTo: imageSticker.leadingAnchor, constant: -20).isActive = true
  921. contentReply.trailingAnchor.constraint(equalTo: imageSticker.leadingAnchor, constant: -20).isActive = true
  922. }
  923. }
  924. }
  925. }
  926. return cell
  927. }
  928. private func chatDate(stringDate: String) -> String {
  929. let date = Date(milliseconds: Int64(stringDate)!)
  930. let calendar = Calendar.current
  931. if (calendar.isDateInToday(date)) {
  932. return "Today".localized()
  933. } else {
  934. let startOfNow = calendar.startOfDay(for: Date())
  935. let startOfTimeStamp = calendar.startOfDay(for: date)
  936. let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
  937. let day = -(components.day!)
  938. if day == 1 {
  939. return "Yesterday".localized()
  940. } else if day < 7 {
  941. return "\(day) " + "days".localized() + " " + "ago".localized()
  942. } else {
  943. let formatter = DateFormatter()
  944. formatter.dateFormat = "EE, dd MMM"
  945. let lang = UserDefaults.standard.string(forKey: "i18n_language")
  946. if lang == "id" {
  947. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  948. }
  949. let stringFormat = formatter.string(from: date as Date)
  950. return stringFormat
  951. }
  952. }
  953. }
  954. private func generateQRCode(from string: String) -> UIImage? {
  955. let data = string.data(using: String.Encoding.ascii)
  956. if let filter = CIFilter(name: "CIQRCodeGenerator") {
  957. filter.setValue(data, forKey: "inputMessage")
  958. let transform = CGAffineTransform(scaleX: 3, y: 3)
  959. if let output = filter.outputImage?.transformed(by: transform) {
  960. return UIImage(ciImage: output)
  961. }
  962. }
  963. return nil
  964. }
  965. private func queryMessageReply(message_id: String) -> [String: Any?] {
  966. var dataQuery: [String: Any] = [:]
  967. Database().database?.inTransaction({ fmdb, rollback in
  968. 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() {
  969. dataQuery["message_id"] = c.string(forColumnIndex: 0)
  970. dataQuery["f_pin"] = c.string(forColumnIndex: 1)
  971. dataQuery["message_text"] = c.string(forColumnIndex: 2)
  972. dataQuery["attachment_flag"] = c.string(forColumnIndex: 3)
  973. dataQuery["thumb_id"] = c.string(forColumnIndex: 4)
  974. dataQuery["image_id"] = c.string(forColumnIndex: 5)
  975. dataQuery["video_id"] = c.string(forColumnIndex: 6)
  976. dataQuery["file_id"] = c.string(forColumnIndex: 7)
  977. c.close()
  978. }
  979. })
  980. return dataQuery
  981. }
  982. private func getDataProfile(f_pin: String, message_id: String) -> [String: String]{
  983. var data: [String: String] = [:]
  984. Database().database?.inTransaction({ fmdb, rollback in
  985. if let c = Database().getRecords(fmdb: fmdb, query: "select first_name || ' ' || last_name, image_id from BUDDY where f_pin = '\(f_pin)'"), c.next() {
  986. data["name"] = c.string(forColumnIndex: 0)!.trimmingCharacters(in: .whitespacesAndNewlines)
  987. data["image_id"] = c.string(forColumnIndex: 1)!
  988. c.close()
  989. }
  990. 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() {
  991. data["name"] = c.string(forColumnIndex: 0)!.trimmingCharacters(in: .whitespacesAndNewlines)
  992. data["image_id"] = c.string(forColumnIndex: 1)!
  993. c.close()
  994. } else if let c = Database().getRecords(fmdb: fmdb, query: "select f_display_name from MESSAGE where message_id = '\(message_id)'"), c.next() {
  995. data["name"] = c.string(forColumnIndex: 0)!
  996. data["image_id"] = ""
  997. c.close()
  998. } else {
  999. data["name"] = "Unknown".localized()
  1000. }
  1001. })
  1002. return data
  1003. }
  1004. @objc func tapMessageText(_ sender: ObjectGesture) {
  1005. var stringURl = sender.message_id.lowercased()
  1006. if stringURl.starts(with: "www.") {
  1007. stringURl = "https://" + stringURl.replacingOccurrences(of: "www.", with: "")
  1008. }
  1009. guard let url = URL(string: stringURl) else { return }
  1010. UIApplication.shared.open(url)
  1011. }
  1012. }