MessageInfo.swift 80 KB

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