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