ContactChatViewController.swift 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. //
  2. // ContactChatViewController.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 22/09/21.
  6. //
  7. import UIKit
  8. import FMDB
  9. import NotificationBannerSwift
  10. import Toast_Swift
  11. class ContactChatViewController: UITableViewController {
  12. deinit {
  13. //print(#function, ">>>> TADAA")
  14. NotificationCenter.default.removeObserver(self)
  15. }
  16. var isChooser: ((String, String) -> ())?
  17. var isAdmin: Bool = false
  18. var chats: [Chat] = []
  19. var contacts: [User] = []
  20. var groups: [Group] = []
  21. var groupMap: [String:Int] = [:]
  22. var searchController: UISearchController!
  23. var segment: UISegmentedControl!
  24. var fillteredData: [Any] = []
  25. var isSearchBarEmpty: Bool {
  26. return searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  27. }
  28. var isFilltering: Bool {
  29. return !isSearchBarEmpty
  30. }
  31. var noData = false
  32. var noUCList = false
  33. func filterContentForSearchText(_ searchText: String) {
  34. func filterContact() {
  35. Utils.inTabChats = false
  36. fillteredData = self.contacts.filter { $0.fullName.lowercased().contains(searchText.lowercased()) }
  37. }
  38. func filterGroup() {
  39. Utils.inTabChats = false
  40. fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
  41. }
  42. if !searchText.isEmpty {
  43. if segment.numberOfSegments == 3 {
  44. switch segment.selectedSegmentIndex {
  45. case 1:
  46. Utils.inTabChats = false
  47. fillteredData = self.contacts.filter { $0.fullName.lowercased().contains(searchText.lowercased()) }
  48. case 2:
  49. Utils.inTabChats = false
  50. fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
  51. default:
  52. Utils.inTabChats = true
  53. fillteredData = self.chats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) }
  54. }
  55. } else {
  56. switch segment.selectedSegmentIndex {
  57. case 1:
  58. filterGroup()
  59. default:
  60. filterContact()
  61. }
  62. }
  63. }
  64. tableView.reloadData()
  65. }
  66. override func viewDidLoad() {
  67. super.viewDidLoad()
  68. let me = UserDefaults.standard.string(forKey: "me")!
  69. Database.shared.database?.inTransaction({ fmdb, rollback in
  70. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: "select FIRST_NAME, LAST_NAME, IMAGE_ID, USER_TYPE from BUDDY where F_PIN = '\(me)'"), cursor.next() {
  71. isAdmin = cursor.string(forColumnIndex: 3) == "23" || cursor.string(forColumnIndex: 3) == "24"
  72. cursor.close()
  73. }
  74. })
  75. // navigationController?.navigationBar.prefersLargeTitles = true
  76. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  77. let childrenMenu : [UIAction] = [
  78. UIAction(title: "Create Group".localized(), image: UIImage(systemName: "person.and.person"), handler: {[weak self](_) in
  79. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "createGroupNav") as! UINavigationController
  80. let vc = controller.topViewController as! GroupCreateViewController
  81. vc.isDismiss = { id in
  82. self?.groupMap.removeAll()
  83. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "groupDetailView") as! GroupDetailViewController
  84. controller.data = id
  85. self?.navigationController?.show(controller, sender: nil)
  86. }
  87. self?.navigationController?.present(controller, animated: true, completion: nil)
  88. }),
  89. UIAction(title: "Add Friends".localized(), image: UIImage(systemName: "person.badge.plus"), handler: {[weak self](_) in
  90. self?.addFriend(sender: UIBarButtonItem())
  91. }),
  92. // UIAction(title: "Configure Email", image: UIImage(systemName: "mail"), handler: {[weak self](_) in
  93. //
  94. // }),
  95. UIAction(title: "Favorite Messages".localized(), image: UIImage(systemName: "star"), handler: {[weak self](_) in
  96. let editorStaredVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "staredVC") as! EditorStarMessages
  97. self?.navigationController?.show(editorStaredVC, sender: nil)
  98. }),
  99. ]
  100. //debug only
  101. // isAdmin = true
  102. // if(isAdmin){
  103. // childrenMenu.append(UIAction(title: "Broadcast Message".localized(), image: UIImage(systemName: "envelope.open"), handler: {[weak self](_) in
  104. // let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "broadcastNav")
  105. // controller.modalPresentationStyle = .fullScreen
  106. // self?.navigationController?.present(controller, animated: true, completion: nil)
  107. // }))
  108. // childrenMenu.append(UIAction(title: "Live Streaming".localized(), image: UIImage(systemName: "video.bubble.left"), handler: {[weak self](_) in
  109. // let navigationController = UINavigationController(rootViewController: QmeraCreateStreamingViewController())
  110. // navigationController.modalPresentationStyle = .fullScreen
  111. // navigationController.navigationBar.tintColor = .white
  112. // navigationController.navigationBar.barTintColor = .mainColor
  113. // navigationController.navigationBar.isTranslucent = false
  114. // navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  115. // navigationController.navigationBar.barStyle = .black
  116. // let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white]
  117. // UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  118. // let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  119. // navigationController.navigationBar.titleTextAttributes = textAttributes
  120. // navigationController.view.backgroundColor = .mainColor
  121. // self?.navigationController?.present(navigationController, animated: true, completion: nil)
  122. // }))
  123. // }
  124. if noUCList {
  125. let buttonAddFriend = UIBarButtonItem(image: UIImage(systemName: "person.badge.plus", withConfiguration: UIImage.SymbolConfiguration(pointSize: 18, weight: .regular, scale: .default))?.withTintColor(.white), style: .plain, target: self, action: #selector(addFriend(sender:)))
  126. navigationItem.rightBarButtonItem = buttonAddFriend
  127. } else {
  128. let menu = UIMenu(title: "", children: childrenMenu)
  129. navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, image: UIImage(systemName: "ellipsis"), primaryAction: .none, menu: menu)
  130. }
  131. searchController = UISearchController(searchResultsController: nil)
  132. searchController.delegate = self
  133. searchController.searchResultsUpdater = self
  134. searchController.searchBar.autocapitalizationType = .none
  135. searchController.searchBar.delegate = self
  136. searchController.searchBar.barTintColor = .secondaryColor
  137. searchController.searchBar.searchTextField.backgroundColor = .secondaryColor
  138. searchController.obscuresBackgroundDuringPresentation = false
  139. searchController.searchBar.setMagnifyingGlassColorTo(color: .mainColor)
  140. searchController.searchBar.tintColor = .mainColor
  141. searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Search".localized(), attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
  142. navigationItem.searchController = searchController
  143. navigationItem.hidesSearchBarWhenScrolling = false
  144. definesPresentationContext = true
  145. var dataSegment = ["Chats".localized(), "Contacts".localized(), "Groups".localized()]
  146. if noUCList{
  147. dataSegment = ["Contacts".localized(), "Groups".localized()]
  148. }
  149. segment = UISegmentedControl(items: dataSegment)
  150. segment.sizeToFit()
  151. segment.selectedSegmentIndex = 0
  152. segment.addTarget(self, action: #selector(segmentChanged(sender:)), for: .valueChanged)
  153. segment.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12.0)], for: .normal)
  154. Utils.inTabChats = true
  155. NotificationCenter.default.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: DigiX.listenerStatusChat), object: nil)
  156. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: DigiX.listenerReceiveChat), object: nil)
  157. NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onMember"), object: nil)
  158. NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onUpdatePersonInfo"), object: nil)
  159. tableView.tableHeaderView = segment
  160. tableView.tableFooterView = UIView()
  161. pullBuddy()
  162. getData()
  163. }
  164. override func viewWillAppear(_ animated: Bool) {
  165. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  166. let navBarAppearance = UINavigationBarAppearance()
  167. navBarAppearance.configureWithOpaqueBackground()
  168. navBarAppearance.backgroundColor = UIColor.mainColor
  169. navBarAppearance.titleTextAttributes = attributes
  170. navigationController?.navigationBar.standardAppearance = navBarAppearance
  171. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  172. // removeAllData()
  173. // getData()
  174. }
  175. override func viewWillDisappear(_ animated: Bool) {
  176. Utils.inTabChats = false
  177. }
  178. override func viewDidAppear(_ animated: Bool) {
  179. if isChooser != nil {
  180. self.navigationController?.navigationBar.topItem?.title = "Forward Messages".localized()
  181. self.navigationController?.navigationBar.setNeedsLayout()
  182. } else if noUCList{
  183. self.navigationController?.navigationBar.topItem?.title = "Start Chat".localized()
  184. self.navigationController?.navigationBar.setNeedsLayout()
  185. } else {
  186. self.navigationController?.navigationBar.topItem?.title = "Start Conversation".localized()
  187. self.navigationController?.navigationBar.setNeedsLayout()
  188. }
  189. DispatchQueue.global().async {
  190. self.getOpenGroups(listGroups: self.groups, completion: { g in
  191. DispatchQueue.main.async {
  192. for og in g {
  193. if self.groups.first(where: { $0.id == og.id }) == nil {
  194. self.groups.append(og)
  195. }
  196. }
  197. DispatchQueue.main.async {
  198. self.tableView.reloadData()
  199. }
  200. }
  201. })
  202. }
  203. if segment.selectedSegmentIndex == 0 {
  204. Utils.inTabChats = true
  205. }
  206. }
  207. @objc func addFriend(sender: UIBarButtonItem) {
  208. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "addFriendNav") as! UINavigationController
  209. if let vc = controller.viewControllers.first as? AddFriendTableViewController {
  210. vc.isDismiss = {
  211. self.getContacts {
  212. DispatchQueue.main.async {
  213. self.tableView.reloadData()
  214. }
  215. }
  216. }
  217. }
  218. navigationController?.present(controller, animated: true, completion: nil)
  219. }
  220. // func removeAllData() {
  221. // groups.removeAll()
  222. // groupMap.removeAll()
  223. // chats.removeAll()
  224. // tableView.reloadData()
  225. // }
  226. @objc func onReload(notification: NSNotification) {
  227. let data:[AnyHashable : Any] = notification.userInfo!
  228. if data["member"] as? String == UserDefaults.standard.string(forKey: "me") {
  229. DispatchQueue.main.async {
  230. self.getData()
  231. }
  232. } else if data["state"] as? Int == 99 {
  233. //print(("MASUK 99")
  234. DispatchQueue.main.async {
  235. self.getData()
  236. }
  237. }
  238. }
  239. @objc func onReceiveMessage(notification: NSNotification) {
  240. DispatchQueue.main.async { [self] in
  241. let data:[AnyHashable : Any] = notification.userInfo!
  242. if let dataMessage = data["message"] as? TMessage {
  243. let chatData = dataMessage.mBodies
  244. if chatData[CoreMessage_TMessageKey.IS_CALL_CENTER] == nil || chatData[CoreMessage_TMessageKey.IS_CALL_CENTER] == "0" {
  245. var indexChat: Int?
  246. if chatData[CoreMessage_TMessageKey.MESSAGE_SCOPE_ID] == "3" && chatData[CoreMessage_TMessageKey.F_PIN] != User.getMyPin() {
  247. indexChat = chats.firstIndex(where: { $0.fpin == chatData[CoreMessage_TMessageKey.F_PIN] })
  248. } else if chatData[CoreMessage_TMessageKey.MESSAGE_SCOPE_ID] == "4" && chatData[CoreMessage_TMessageKey.F_PIN] != User.getMyPin() {
  249. indexChat = chats.firstIndex(where: { (chatData[CoreMessage_TMessageKey.CHAT_ID] ?? "").isEmpty ? $0.pin == chatData[CoreMessage_TMessageKey.L_PIN] : $0.pin == chatData[CoreMessage_TMessageKey.CHAT_ID] })
  250. }
  251. let newChat = Chat.getData(messageId: chatData[CoreMessage_TMessageKey.MESSAGE_ID] ?? "")
  252. if newChat.count > 0 {
  253. if indexChat != nil {
  254. chats.remove(at: indexChat!)
  255. chats.insert(newChat[0], at: 0)
  256. let indexPathToMove = IndexPath(row: indexChat!, section: 0)
  257. let indexPathNewPosition = IndexPath(row: 0, section: 0)
  258. tableView.performBatchUpdates({
  259. tableView.moveRow(at: indexPathToMove, to: indexPathNewPosition)
  260. }, completion: nil)
  261. tableView.beginUpdates()
  262. tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .none)
  263. tableView.endUpdates()
  264. } else {
  265. chats.insert(newChat[0], at: 0)
  266. tableView.reloadData()
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. @objc func onStatusChat(notification: NSNotification) {
  274. DispatchQueue.main.async { [self] in
  275. let data:[AnyHashable : Any] = notification.userInfo!
  276. if let dataMessage = data["message"] as? TMessage {
  277. let indexChat = chats.firstIndex(where: { (dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)).contains(",") ? $0.messageId == (dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)).components(separatedBy: ",")[1] : $0.messageId == dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID) })
  278. if indexChat != nil {
  279. tableView.beginUpdates()
  280. tableView.reloadRows(at: [IndexPath(row: indexChat!, section: 0)], with: .none)
  281. tableView.endUpdates()
  282. }
  283. }
  284. }
  285. }
  286. @objc func add(sender: Any) {
  287. }
  288. @objc func cancel(sender: Any) {
  289. navigationController?.dismiss(animated: true, completion: nil)
  290. }
  291. @objc func segmentChanged(sender: Any) {
  292. filterContentForSearchText(searchController.searchBar.text!)
  293. }
  294. // MARK: - Data source
  295. func getData() {
  296. getChats {
  297. self.getContacts {
  298. self.getGroups { g1 in
  299. self.groupMap.removeAll()
  300. self.groups = g1
  301. DispatchQueue.main.async {
  302. self.tableView.reloadData()
  303. }
  304. }
  305. }
  306. }
  307. }
  308. func getChats(completion: @escaping ()->()) {
  309. DispatchQueue.global().async {
  310. self.chats = Chat.getData()
  311. completion()
  312. }
  313. }
  314. private func getContacts(completion: @escaping ()->()) {
  315. self.contacts.removeAll()
  316. DispatchQueue.global().async {
  317. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  318. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: "SELECT f_pin, first_name, last_name, image_id, official_account, user_type FROM BUDDY where f_pin <> '\(UserDefaults.standard.string(forKey: "me")!)' order by 5 desc, 2 collate nocase asc") {
  319. while cursorData.next() {
  320. let user = User(pin: cursorData.string(forColumnIndex: 0) ?? "",
  321. firstName: cursorData.string(forColumnIndex: 1) ?? "",
  322. lastName: cursorData.string(forColumnIndex: 2) ?? "",
  323. thumb: cursorData.string(forColumnIndex: 3) ?? "",
  324. userType: cursorData.string(forColumnIndex: 5) ?? "")
  325. if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
  326. continue
  327. }
  328. user.official = cursorData.string(forColumnIndex: 4) ?? ""
  329. if !self.contacts.contains(where: {$0.pin == user.pin}) {
  330. self.contacts.append(user)
  331. }
  332. }
  333. cursorData.close()
  334. }
  335. completion()
  336. })
  337. }
  338. }
  339. private func getGroupRecursive(fmdb: FMDatabase, id: String = "", parent: String = "") -> [Group] {
  340. var data: [Group] = []
  341. var query = "select g.group_id, g.f_name, g.image_id, g.quote, g.created_by, g.created_date, g.parent, g.group_type, g.is_open, g.official, g.is_education, g.level from GROUPZ g where "
  342. if id.isEmpty {
  343. query += "g.parent = '\(parent)'"
  344. } else {
  345. query += "g.group_id = '\(id)'"
  346. }
  347. query += "order by 10 desc"
  348. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query) {
  349. while cursor.next() {
  350. let group = Group(
  351. id: cursor.string(forColumnIndex: 0) ?? "",
  352. name: cursor.string(forColumnIndex: 1) ?? "",
  353. profile: cursor.string(forColumnIndex: 2) ?? "",
  354. quote: cursor.string(forColumnIndex: 3) ?? "",
  355. by: cursor.string(forColumnIndex: 4) ?? "",
  356. date: cursor.string(forColumnIndex: 5) ?? "",
  357. parent: cursor.string(forColumnIndex: 6) ?? "",
  358. chatId: "",
  359. groupType: cursor.string(forColumnIndex: 7) ?? "",
  360. isOpen: cursor.string(forColumnIndex: 8) ?? "",
  361. official: cursor.string(forColumnIndex: 9) ?? "",
  362. isEducation: cursor.string(forColumnIndex: 10) ?? "",
  363. level: cursor.string(forColumnIndex: 11) ?? "")
  364. if group.chatId.isEmpty {
  365. let lounge = Group(id: group.id, name: "Lounge".localized(), profile: "", quote: group.quote, by: group.by, date: group.date, parent: group.id, chatId: group.chatId, groupType: group.groupType, isOpen: group.isOpen, official: group.official, isEducation: group.isEducation, isLounge: true, level: group.level != "-1" ? group.level : "2")
  366. group.childs.append(lounge)
  367. }
  368. if let topicCursor = Database.shared.getRecords(fmdb: fmdb, query: "select chat_id, title, thumb from DISCUSSION_FORUM where group_id = '\(group.id)'") {
  369. while topicCursor.next() {
  370. let topic = Group(id: group.id,
  371. name: topicCursor.string(forColumnIndex: 1) ?? "",
  372. profile: topicCursor.string(forColumnIndex: 2) ?? "",
  373. quote: group.quote,
  374. by: group.by,
  375. date: group.date,
  376. parent: group.id,
  377. chatId: topicCursor.string(forColumnIndex: 0) ?? "",
  378. groupType: group.groupType,
  379. isOpen: group.isOpen,
  380. official: group.official,
  381. isEducation: group.isEducation,
  382. level: group.level != "-1" ? group.level : "2")
  383. group.childs.append(topic)
  384. }
  385. topicCursor.close()
  386. }
  387. if !group.id.isEmpty {
  388. // if group.official == "1" {
  389. // let idMe = UserDefaults.standard.string(forKey: "me") as String?
  390. // if let cursorUser = Database.shared.getRecords(fmdb: fmdb, query: "SELECT user_type FROM BUDDY where f_pin='\(idMe!)'"), cursorUser.next() {
  391. //// if cursorUser.string(forColumnIndex: 0) == "23" || cursorUser.string(forColumnIndex: 0) == "24" {
  392. //// group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  393. //// }
  394. // group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  395. // cursorUser.close()
  396. // }
  397. // } else if group.official != "1"{
  398. // group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  399. // }
  400. group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  401. // group.childs = group.childs.sorted(by: { $0.name < $1.name })
  402. // let dataLounge = group.childs.filter({$0.name == "Lounge".localized()})
  403. // group.childs = group.childs.filter({ $0.name != "Lounge".localized() })
  404. // group.childs.insert(contentsOf: dataLounge, at: 0)
  405. }
  406. data.append(group)
  407. }
  408. cursor.close()
  409. }
  410. return data
  411. }
  412. private func getOpenGroups(listGroups: [Group], completion: @escaping ([Group]) -> ()) {
  413. if let response = DigiX.writeSync(message: CoreMessage_TMessageBank.getOpenGroups(p_account: "1,2,3,5,6,7", offset: "0", search: "")) {
  414. var dataGroups: [Group] = []
  415. if (response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "00") {
  416. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  417. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
  418. for dataJson in json {
  419. let group = Group(
  420. id: dataJson[CoreMessage_TMessageKey.GROUP_ID] as? String ?? "",
  421. name: dataJson[CoreMessage_TMessageKey.GROUP_NAME] as? String ?? "",
  422. profile: dataJson[CoreMessage_TMessageKey.THUMB_ID] as? String ?? "",
  423. quote: dataJson[CoreMessage_TMessageKey.QUOTE] as? String ?? "",
  424. by: dataJson[CoreMessage_TMessageKey.BLOCK] as? String ?? "",
  425. date: "",
  426. parent: "",
  427. chatId: "",
  428. groupType: "NOTJOINED",
  429. isOpen: dataJson[CoreMessage_TMessageKey.IS_OPEN] as? String ?? "",
  430. official: "0",
  431. isEducation: "")
  432. dataGroups.append(group)
  433. }
  434. }
  435. } else {
  436. DispatchQueue.main.async {
  437. self.groups.removeAll()
  438. self.groups.append(contentsOf: listGroups)
  439. self.tableView.reloadData()
  440. }
  441. }
  442. completion(dataGroups)
  443. }
  444. }
  445. private func getGroups(id: String = "", parent: String = "", completion: @escaping ([Group]) -> ()) {
  446. DispatchQueue.global().async {
  447. Database.shared.database?.inTransaction({ fmdb, rollback in
  448. completion(self.getGroupRecursive(fmdb: fmdb, id: id, parent: parent))
  449. })
  450. }
  451. }
  452. private func pullBuddy() {
  453. if let me = UserDefaults.standard.string(forKey: "me") {
  454. DispatchQueue.global().async {
  455. let _ = DigiX.write(message: CoreMessage_TMessageBank.getBatchBuddiesInfos(p_f_pin: me, last_update: 0))
  456. }
  457. }
  458. }
  459. private func joinOpenGroup(groupId: String, flagMember: String = "0", completion: @escaping (Bool) -> ()) {
  460. DispatchQueue.global().async {
  461. var result: Bool = false
  462. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  463. if let response = DigiX.writeAndWait(message: CoreMessage_TMessageBank.getAddGroupMember(p_group_id: groupId, p_member_pin: idMe!, p_position: "0")), response.isOk() {
  464. result = true
  465. }
  466. completion(result)
  467. }
  468. }
  469. }
  470. // MARK: - Table view data source
  471. extension ContactChatViewController {
  472. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  473. if noData {
  474. return
  475. }
  476. tableView.deselectRow(at: indexPath, animated: true)
  477. func selectOnContact() {
  478. let data: User
  479. if isFilltering {
  480. data = fillteredData[indexPath.row] as! User
  481. } else {
  482. data = contacts[indexPath.row]
  483. }
  484. if let chooser = isChooser {
  485. var exblock = User.getDataCanNil(pin: data.pin)?.ex_block
  486. exblock = exblock == nil ? "0" : exblock!.isEmpty ? "0" : exblock!
  487. if exblock != "0" {
  488. if exblock == "1" {
  489. self.view.makeToast("You blocked this user".localized())
  490. } else {
  491. self.view.makeToast("You have been blocked by this user".localized())
  492. }
  493. return
  494. }
  495. chooser("3", data.pin)
  496. dismiss(animated: true, completion: nil)
  497. return
  498. }
  499. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  500. editorPersonalVC.hidesBottomBarWhenPushed = true
  501. editorPersonalVC.unique_l_pin = data.pin
  502. navigationController?.show(editorPersonalVC, sender: nil)
  503. }
  504. if segment.numberOfSegments == 3 {
  505. switch segment.selectedSegmentIndex {
  506. case 0:
  507. let data: Chat
  508. if isFilltering {
  509. data = fillteredData[indexPath.row] as! Chat
  510. } else {
  511. data = chats[indexPath.row]
  512. }
  513. if let chooser = isChooser {
  514. var exblock = User.getDataCanNil(pin: data.pin)?.ex_block
  515. exblock = exblock == nil ? "0" : exblock!.isEmpty ? "0" : exblock!
  516. if exblock != "0" {
  517. if exblock == "1" {
  518. self.view.makeToast("You blocked this user".localized())
  519. } else {
  520. self.view.makeToast("You have been blocked by this user".localized())
  521. }
  522. return
  523. }
  524. if data.pin == "-999"{
  525. return
  526. }
  527. chooser(data.messageScope, data.pin)
  528. dismiss(animated: true, completion: nil)
  529. return
  530. }
  531. if data.messageScope == "3" {
  532. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  533. editorPersonalVC.hidesBottomBarWhenPushed = true
  534. editorPersonalVC.unique_l_pin = data.pin
  535. navigationController?.show(editorPersonalVC, sender: nil)
  536. } else {
  537. groupMap.removeAll()
  538. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  539. editorGroupVC.hidesBottomBarWhenPushed = true
  540. editorGroupVC.unique_l_pin = data.pin
  541. navigationController?.show(editorGroupVC, sender: nil)
  542. }
  543. case 1:
  544. selectOnContact()
  545. case 2:
  546. expandCollapseGroup(tableView: tableView, indexPath: indexPath)
  547. default:
  548. let data = contacts[indexPath.row]
  549. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  550. editorPersonalVC.hidesBottomBarWhenPushed = true
  551. editorPersonalVC.unique_l_pin = data.pin
  552. navigationController?.show(editorPersonalVC, sender: nil)
  553. }
  554. } else {
  555. switch segment.selectedSegmentIndex {
  556. case 0:
  557. selectOnContact()
  558. case 1:
  559. expandCollapseGroup(tableView: tableView, indexPath: indexPath)
  560. default:
  561. let data = contacts[indexPath.row]
  562. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  563. editorPersonalVC.hidesBottomBarWhenPushed = true
  564. editorPersonalVC.unique_l_pin = data.pin
  565. navigationController?.show(editorPersonalVC, sender: nil)
  566. }
  567. }
  568. }
  569. func expandCollapseGroup(tableView: UITableView, indexPath: IndexPath) {
  570. let group: Group
  571. if isFilltering {
  572. if indexPath.row == 0 {
  573. group = fillteredData[indexPath.section] as! Group
  574. } else {
  575. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  576. }
  577. } else {
  578. if indexPath.row == 0 {
  579. group = groups[indexPath.section]
  580. } else {
  581. group = groups[indexPath.section].childs[indexPath.row - 1]
  582. }
  583. }
  584. group.isSelected = !group.isSelected
  585. if !group.isSelected{
  586. var sects = 0
  587. var sect = indexPath.section
  588. var id = group.id
  589. if let _ = groupMap[id] {
  590. var loooop = true
  591. repeat {
  592. let c = sect + 1
  593. if isFilltering {
  594. if let o = self.fillteredData[c] as? Group {
  595. if o.parent == id {
  596. sects = sects + 1
  597. sect = c
  598. id = o.id
  599. (self.fillteredData[c] as! Group).isSelected = false
  600. self.groupMap.removeValue(forKey: (self.fillteredData[c] as! Group).id)
  601. }
  602. else {
  603. loooop = false
  604. }
  605. }
  606. }
  607. else {
  608. if c < self.groups.count && self.groups[c].parent == id {
  609. sects = sects + 1
  610. sect = c
  611. id = self.groups[c].id
  612. self.groups[c].isSelected = false
  613. self.groupMap.removeValue(forKey: self.groups[c].id)
  614. }
  615. else {
  616. loooop = false
  617. }
  618. }
  619. } while(loooop)
  620. }
  621. for i in stride(from: sects, to: 0, by: -1){
  622. if isFilltering {
  623. self.fillteredData.remove(at: indexPath.section + i)
  624. }
  625. else {
  626. self.groups.remove(at: indexPath.section + i)
  627. }
  628. }
  629. groupMap.removeValue(forKey: group.id)
  630. }
  631. if group.groupType == "NOTJOINED" {
  632. let alert = LibAlertController(title: "Do you want to join this group?".localized(), message: "Groups : \(group.name)\nMembers: \(group.by)".localized(), preferredStyle: .alert)
  633. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  634. alert.addAction(UIAlertAction(title: "Join".localized(), style: .default, handler: {(_) in
  635. self.joinOpenGroup(groupId: group.id, completion: { result in
  636. if result {
  637. DispatchQueue.main.async {
  638. self.groupMap.removeAll()
  639. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  640. editorGroupVC.hidesBottomBarWhenPushed = true
  641. editorGroupVC.unique_l_pin = group.id
  642. self.navigationController?.show(editorGroupVC, sender: nil)
  643. }
  644. }
  645. })
  646. }))
  647. self.present(alert, animated: true, completion: nil)
  648. return
  649. }
  650. if group.childs.count == 0 {
  651. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  652. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  653. if let cursorMember = Database.shared.getRecords(fmdb: fmdb, query: "select f_pin from GROUPZ_MEMBER where group_id = '\(group.id)' and f_pin = '\(idMe!)'"), cursorMember.next() {
  654. let groupId = group.chatId.isEmpty ? group.id : group.chatId
  655. if let chooser = isChooser {
  656. chooser("4", groupId)
  657. dismiss(animated: true, completion: nil)
  658. return
  659. }
  660. self.groupMap.removeAll()
  661. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  662. editorGroupVC.hidesBottomBarWhenPushed = true
  663. editorGroupVC.unique_l_pin = groupId
  664. navigationController?.show(editorGroupVC, sender: nil)
  665. cursorMember.close()
  666. } else {
  667. self.view.makeToast("You are not a member of this group".localized(), duration: 0.5)
  668. }
  669. })
  670. } else {
  671. if indexPath.row == 0 {
  672. tableView.reloadData()
  673. } else {
  674. getGroups(id: group.id) { g in
  675. DispatchQueue.main.async {
  676. //print(("index path section: \(indexPath.section)")
  677. //print(("index path row: \(indexPath.row)")
  678. // //print(("index path item: \(indexPath.item)")
  679. if self.isFilltering {
  680. // self.fillteredData.remove(at: indexPath.section)
  681. if self.fillteredData[indexPath.section] is Group {
  682. self.groupMap[(self.fillteredData[indexPath.section] as! Group).id] = 1
  683. self.fillteredData.insert(contentsOf: g, at: indexPath.section + 1)
  684. }
  685. } else {
  686. // self.groups.remove(at: indexPath.section)
  687. self.groupMap[self.groups[indexPath.section].id] = 1
  688. self.groups.insert(contentsOf: g, at: indexPath.section + 1)
  689. }
  690. //print(("groupMap: \(self.groupMap)")
  691. tableView.reloadData()
  692. self.expandCollapseGroup(tableView: tableView, indexPath: IndexPath(row: 0, section: indexPath.section + 1))
  693. }
  694. }
  695. }
  696. }
  697. }
  698. }
  699. extension ContactChatViewController {
  700. override func numberOfSections(in tableView: UITableView) -> Int {
  701. if isFilltering {
  702. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)) {
  703. return fillteredData.count
  704. }
  705. return 1
  706. } else {
  707. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)) {
  708. return groups.count
  709. }
  710. return 1
  711. }
  712. }
  713. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  714. var value = 0
  715. if isFilltering {
  716. func filterGroup(groups: [Group]) {
  717. let group = groups[section]
  718. if group.isSelected {
  719. if let _ = groupMap[group.id] {
  720. value = 1
  721. }
  722. else {
  723. value = group.childs.count + 1
  724. }
  725. } else {
  726. value = 1
  727. }
  728. }
  729. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)), let groups = fillteredData as? [Group] {
  730. filterGroup(groups: groups)
  731. } else {
  732. value = fillteredData.count
  733. }
  734. return value
  735. }
  736. if segment.numberOfSegments == 3 {
  737. switch segment.selectedSegmentIndex {
  738. case 0:
  739. value = chats.count
  740. case 1:
  741. value = contacts.count
  742. case 2:
  743. let group = groups[section]
  744. if group.isSelected {
  745. if let _ = groupMap[group.id] {
  746. value = 1
  747. }
  748. else {
  749. value = group.childs.count + 1
  750. }
  751. } else {
  752. value = 1
  753. }
  754. default:
  755. value = chats.count
  756. }
  757. } else {
  758. switch segment.selectedSegmentIndex {
  759. case 0:
  760. value = contacts.count
  761. case 1:
  762. let group = groups[section]
  763. if group.isSelected {
  764. if let _ = groupMap[group.id] {
  765. value = 1
  766. }
  767. else {
  768. value = group.childs.count + 1
  769. }
  770. } else {
  771. value = 1
  772. }
  773. default:
  774. value = chats.count
  775. }
  776. }
  777. if value == 0 {
  778. noData = true
  779. value = 1
  780. tableView.separatorStyle = .none
  781. } else {
  782. noData = false
  783. tableView.separatorStyle = .singleLine
  784. }
  785. return value
  786. }
  787. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  788. var cell: UITableViewCell!
  789. switch segment.selectedSegmentIndex {
  790. case 0:
  791. if segment.numberOfSegments < 3 {
  792. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  793. var content = cell.defaultContentConfiguration()
  794. let data: User
  795. if isFilltering {
  796. data = fillteredData[indexPath.row] as! User
  797. } else {
  798. if indexPath.row > contacts.count - 1 {
  799. return cell
  800. }
  801. data = contacts[indexPath.row]
  802. }
  803. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  804. getImage(name: data.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  805. content.image = image
  806. })
  807. if User.isOfficial(official_account: data.official ?? "") || User.isOfficialRegular(official_account: data.official ?? "") {
  808. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.officialColor)
  809. } else if User.isVerified(official_account: data.official ?? "") {
  810. content.attributedText = self.set(image: UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.verifiedColor)
  811. }
  812. else if User.isInternal(userType: data.userType ?? "") {
  813. content.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.internalColor)
  814. } else if User.isCallCenter(userType: data.userType ?? "") {
  815. // let dataCategory = CategoryCC.getDataFromServiceId(service_id: data.ex_offmp!)
  816. // if dataCategory != nil {
  817. // content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName) (\(dataCategory!.service_name))", size: 15, y: -4, colorText: UIColor.ccColor)
  818. // } else {
  819. content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.ccColor)
  820. // }
  821. } else {
  822. content.text = data.fullName
  823. }
  824. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  825. cell.contentConfiguration = content
  826. } else {
  827. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierChat", for: indexPath)
  828. let content = cell.contentView
  829. if content.subviews.count > 0 {
  830. content.subviews.forEach { $0.removeFromSuperview() }
  831. }
  832. if noData {
  833. let labelNochat = UILabel()
  834. labelNochat.text = "There are no conversations".localized()
  835. labelNochat.font = .systemFont(ofSize: 13)
  836. labelNochat.textColor = .black
  837. content.addSubview(labelNochat)
  838. labelNochat.anchor(centerX: content.centerXAnchor, centerY: content.centerYAnchor)
  839. cell.backgroundColor = .clear
  840. cell.selectionStyle = .none
  841. return cell
  842. }
  843. let data: Chat
  844. if isFilltering {
  845. data = fillteredData[indexPath.row] as! Chat
  846. } else {
  847. if chats.count == 0 {
  848. let labelNochat = UILabel()
  849. labelNochat.text = "There are no conversations".localized()
  850. labelNochat.font = .systemFont(ofSize: 13)
  851. labelNochat.textColor = .black
  852. content.addSubview(labelNochat)
  853. labelNochat.anchor(centerX: content.centerXAnchor, centerY: content.centerYAnchor)
  854. cell.backgroundColor = .clear
  855. cell.selectionStyle = .none
  856. return cell
  857. }
  858. data = chats[indexPath.row]
  859. }
  860. let imageView = UIImageView()
  861. content.addSubview(imageView)
  862. imageView.translatesAutoresizingMaskIntoConstraints = false
  863. NSLayoutConstraint.activate([
  864. imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 10.0),
  865. imageView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  866. imageView.bottomAnchor.constraint(equalTo: content.bottomAnchor, constant: -10.0),
  867. imageView.widthAnchor.constraint(equalToConstant: 55.0),
  868. imageView.heightAnchor.constraint(equalToConstant: 55.0)
  869. ])
  870. if data.profile.isEmpty && data.pin != "-999" {
  871. if data.messageScope == "3" {
  872. imageView.image = UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil)
  873. } else {
  874. imageView.image = UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil)
  875. }
  876. } else {
  877. if Utils.getIconDock() != nil {
  878. let dataImage = try? Data(contentsOf: URL(string: Utils.getUrlDock()!)!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
  879. if dataImage != nil {
  880. getImage(name: data.profile, placeholderImage: UIImage(data: dataImage!), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  881. imageView.image = image
  882. })
  883. }
  884. } else {
  885. getImage(name: data.profile, placeholderImage: UIImage(named: data.pin == "-999" ? "pb_button" : data.messageScope == "3" ? "Profile---Purple" : "Conversation---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  886. imageView.image = image
  887. })
  888. }
  889. }
  890. let titleView = UILabel()
  891. content.addSubview(titleView)
  892. titleView.translatesAutoresizingMaskIntoConstraints = false
  893. NSLayoutConstraint.activate([
  894. titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  895. titleView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  896. titleView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  897. ])
  898. titleView.text = data.name
  899. titleView.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  900. let messageView = UILabel()
  901. content.addSubview(messageView)
  902. messageView.translatesAutoresizingMaskIntoConstraints = false
  903. NSLayoutConstraint.activate([
  904. messageView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  905. messageView.topAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 5.0),
  906. messageView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  907. ])
  908. messageView.textColor = .gray
  909. if data.messageText.contains("■") {
  910. data.messageText = data.messageText.components(separatedBy: "■")[0]
  911. data.messageText = data.messageText.trimmingCharacters(in: .whitespacesAndNewlines)
  912. }
  913. let text = Utils.previewMessageText(chat: data)
  914. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  915. if let attributeText = text as? NSMutableAttributedString {
  916. let stringMessage = NSMutableAttributedString(string: "")
  917. if data.fpin == idMe {
  918. if data.lock == "1" {
  919. if data.messageScope == "4" {
  920. stringMessage.append(NSAttributedString(string: "You".localized() + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .medium)]))
  921. }
  922. stringMessage.append(("🚫 _"+"You were deleted this message".localized()+"_").richText())
  923. } else {
  924. let imageStatus = NSTextAttachment()
  925. let status = getRealStatus(messageId: data.messageId)
  926. if (status == "1" || status == "2" ) {
  927. imageStatus.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
  928. } else if (status == "3") {
  929. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.lightGray)
  930. } else if (status == "8") {
  931. imageStatus.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  932. } else {
  933. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!.withTintColor(UIColor.systemBlue)
  934. }
  935. imageStatus.bounds = CGRect(x: 0, y: -2, width: 15, height: 15)
  936. let imageStatusString = NSAttributedString(attachment: imageStatus)
  937. stringMessage.append(imageStatusString)
  938. stringMessage.append(NSAttributedString(string: " "))
  939. if data.messageScope == "4" {
  940. stringMessage.append(NSAttributedString(string: "You".localized() + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .medium)]))
  941. }
  942. stringMessage.append(attributeText)
  943. }
  944. } else {
  945. if data.messageScope == "4" {
  946. stringMessage.append(NSAttributedString(string: User.getData(pin: data.fpin)!.firstName + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .medium)]))
  947. }
  948. if data.lock == "1" {
  949. stringMessage.append(("🚫 _"+"This message was deleted".localized()+"_").richText())
  950. } else {
  951. stringMessage.append(attributeText)
  952. }
  953. }
  954. messageView.attributedText = stringMessage
  955. }
  956. messageView.numberOfLines = 2
  957. let timeView = UILabel()
  958. content.addSubview(timeView)
  959. timeView.translatesAutoresizingMaskIntoConstraints = false
  960. NSLayoutConstraint.activate([
  961. timeView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  962. timeView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20.0),
  963. ])
  964. timeView.textColor = .gray
  965. timeView.font = UIFont.systemFont(ofSize: 14)
  966. let date = Date(milliseconds: Int64(data.serverDate)!)
  967. let calendar = Calendar.current
  968. if (calendar.isDateInToday(date)) {
  969. let formatter = DateFormatter()
  970. formatter.dateFormat = "HH:mm"
  971. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  972. timeView.text = formatter.string(from: date as Date)
  973. } else {
  974. let startOfNow = calendar.startOfDay(for: Date())
  975. let startOfTimeStamp = calendar.startOfDay(for: date)
  976. let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
  977. let day = -(components.day!)
  978. if day == 1 {
  979. timeView.text = "Yesterday".localized()
  980. } else {
  981. if day < 7 {
  982. let formatter = DateFormatter()
  983. formatter.dateFormat = "EEEE"
  984. timeView.text = formatter.string(from: date).localized()
  985. } else {
  986. let formatter = DateFormatter()
  987. formatter.dateFormat = "MM/dd/yy"
  988. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  989. let stringFormat = formatter.string(from: date as Date)
  990. timeView.text = stringFormat
  991. }
  992. }
  993. }
  994. if data.counter != "0" {
  995. timeView.textColor = .systemRed
  996. let viewCounter = UIView()
  997. content.addSubview(viewCounter)
  998. viewCounter.translatesAutoresizingMaskIntoConstraints = false
  999. NSLayoutConstraint.activate([
  1000. viewCounter.topAnchor.constraint(equalTo: timeView.bottomAnchor, constant: 5.0),
  1001. viewCounter.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20),
  1002. viewCounter.widthAnchor.constraint(greaterThanOrEqualToConstant: 20),
  1003. viewCounter.heightAnchor.constraint(equalToConstant: 20)
  1004. ])
  1005. viewCounter.backgroundColor = .systemRed
  1006. viewCounter.layer.cornerRadius = 10
  1007. viewCounter.clipsToBounds = true
  1008. viewCounter.layer.borderWidth = 0.5
  1009. viewCounter.layer.borderColor = UIColor.secondaryColor.cgColor
  1010. let labelCounter = UILabel()
  1011. viewCounter.addSubview(labelCounter)
  1012. labelCounter.translatesAutoresizingMaskIntoConstraints = false
  1013. NSLayoutConstraint.activate([
  1014. labelCounter.centerYAnchor.constraint(equalTo: viewCounter.centerYAnchor),
  1015. labelCounter.leadingAnchor.constraint(equalTo: viewCounter.leadingAnchor, constant: 2),
  1016. labelCounter.trailingAnchor.constraint(equalTo: viewCounter.trailingAnchor, constant: -2),
  1017. ])
  1018. labelCounter.font = UIFont.systemFont(ofSize: 11)
  1019. if Int(data.counter)! > 99 {
  1020. labelCounter.text = "99+"
  1021. } else {
  1022. labelCounter.text = data.counter
  1023. }
  1024. labelCounter.textColor = .secondaryColor
  1025. labelCounter.textAlignment = .center
  1026. }
  1027. }
  1028. case 1:
  1029. if segment.numberOfSegments < 3 {
  1030. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
  1031. var content = cell.defaultContentConfiguration()
  1032. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  1033. let group: Group
  1034. if isFilltering {
  1035. if indexPath.row == 0 {
  1036. group = fillteredData[indexPath.section] as! Group
  1037. } else {
  1038. if (fillteredData[indexPath.section] as! Group).childs.count > 0 {
  1039. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  1040. } else {
  1041. return cell
  1042. }
  1043. }
  1044. } else {
  1045. if indexPath.row == 0 {
  1046. if indexPath.section > (groups.count - 1) {
  1047. return cell
  1048. }
  1049. group = groups[indexPath.section]
  1050. } else {
  1051. group = groups[indexPath.section].childs[indexPath.row - 1]
  1052. }
  1053. }
  1054. if group.official == "1" && group.parent == "" {
  1055. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(group.name)", size: 15, y: -4)
  1056. }
  1057. else if group.isOpen == "1" && group.parent == "" {
  1058. if self.traitCollection.userInterfaceStyle == .dark {
  1059. content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  1060. } else {
  1061. content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4)
  1062. }
  1063. } else if group.parent == "" {
  1064. if self.traitCollection.userInterfaceStyle == .dark {
  1065. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  1066. } else {
  1067. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4)
  1068. }
  1069. } else {
  1070. content.text = group.name
  1071. }
  1072. if group.childs.count > 0 {
  1073. let iconName = (group.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
  1074. let imageView = UIImageView(image: UIImage(systemName: iconName))
  1075. imageView.tintColor = .black
  1076. cell.accessoryView = imageView
  1077. }
  1078. else {
  1079. cell.accessoryView = nil
  1080. cell.accessoryType = .none
  1081. }
  1082. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  1083. getImage(name: group.profile, placeholderImage: UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  1084. content.image = image
  1085. }
  1086. cell.contentConfiguration = content
  1087. if !group.level.isEmpty {
  1088. if group.level != "-1" && Int(group.level)! < 7 {
  1089. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * Int(group.level)!), bottom: 0.0, right: 0)
  1090. } else if Int(group.level)! > 6 {
  1091. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * (Int(group.level)! - 6)), bottom: 0.0, right: 0)
  1092. }
  1093. }
  1094. } else {
  1095. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  1096. var content = cell.defaultContentConfiguration()
  1097. let data: User
  1098. if isFilltering {
  1099. data = fillteredData[indexPath.row] as! User
  1100. } else {
  1101. if indexPath.row > contacts.count - 1 {
  1102. return cell
  1103. }
  1104. data = contacts[indexPath.row]
  1105. }
  1106. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  1107. getImage(name: data.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  1108. content.image = image
  1109. })
  1110. if User.isOfficial(official_account: data.official ?? "") || User.isOfficialRegular(official_account: data.official ?? "") {
  1111. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.officialColor)
  1112. } else if User.isVerified(official_account: data.official ?? "") {
  1113. content.attributedText = self.set(image: UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.verifiedColor)
  1114. }
  1115. else if User.isInternal(userType: data.userType ?? "") {
  1116. content.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.internalColor)
  1117. } else if User.isCallCenter(userType: data.userType ?? "") {
  1118. // let dataCategory = CategoryCC.getDataFromServiceId(service_id: data.ex_offmp!)
  1119. // if dataCategory != nil {
  1120. // content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName) (\(dataCategory!.service_name))", size: 15, y: -4, colorText: UIColor.ccColor)
  1121. // } else {
  1122. content.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.ccColor)
  1123. // }
  1124. } else {
  1125. content.text = data.fullName
  1126. }
  1127. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  1128. cell.contentConfiguration = content
  1129. }
  1130. case 2:
  1131. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
  1132. var content = cell.defaultContentConfiguration()
  1133. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  1134. let group: Group
  1135. if isFilltering {
  1136. if indexPath.row == 0 {
  1137. group = fillteredData[indexPath.section] as! Group
  1138. } else {
  1139. if (fillteredData[indexPath.section] as! Group).childs.count > 0 {
  1140. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  1141. } else {
  1142. return cell
  1143. }
  1144. }
  1145. } else {
  1146. if indexPath.row == 0 {
  1147. if indexPath.section > (groups.count - 1) {
  1148. return cell
  1149. }
  1150. group = groups[indexPath.section]
  1151. } else {
  1152. group = groups[indexPath.section].childs[indexPath.row - 1]
  1153. }
  1154. }
  1155. if group.official == "1" && group.parent == "" {
  1156. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: DigiX.self), with: nil)!, with: " \(group.name)", size: 15, y: -4)
  1157. }
  1158. else if group.isOpen == "1" && group.parent == "" {
  1159. if self.traitCollection.userInterfaceStyle == .dark {
  1160. content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  1161. } else {
  1162. content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4)
  1163. }
  1164. } else if group.parent == "" {
  1165. if self.traitCollection.userInterfaceStyle == .dark {
  1166. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  1167. } else {
  1168. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4)
  1169. }
  1170. } else {
  1171. content.text = group.name
  1172. }
  1173. if group.childs.count > 0 {
  1174. let iconName = (group.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
  1175. let imageView = UIImageView(image: UIImage(systemName: iconName))
  1176. imageView.tintColor = .black
  1177. cell.accessoryView = imageView
  1178. }
  1179. else {
  1180. cell.accessoryView = nil
  1181. cell.accessoryType = .none
  1182. }
  1183. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  1184. getImage(name: group.profile, placeholderImage: UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: DigiX.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  1185. content.image = image
  1186. }
  1187. cell.contentConfiguration = content
  1188. if !group.level.isEmpty {
  1189. if group.level != "-1" && Int(group.level)! < 7 {
  1190. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * Int(group.level)!), bottom: 0.0, right: 0)
  1191. } else if Int(group.level)! > 6 {
  1192. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * (Int(group.level)! - 6)), bottom: 0.0, right: 0)
  1193. }
  1194. }
  1195. default:
  1196. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  1197. var content = cell.defaultContentConfiguration()
  1198. content.text = ""
  1199. cell.contentConfiguration = content
  1200. }
  1201. cell.backgroundColor = .clear
  1202. cell.separatorInset = UIEdgeInsets(top: 0, left: 60.0, bottom: 0, right: 0)
  1203. return cell
  1204. }
  1205. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  1206. return 75.0
  1207. }
  1208. private func getRealStatus(messageId: String) -> String {
  1209. var status = "1"
  1210. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  1211. if let cursorStatus = Database.shared.getRecords(fmdb: fmdb, query: "SELECT status, f_pin FROM MESSAGE_STATUS WHERE message_id='\(messageId)'") {
  1212. var listStatus: [Int] = []
  1213. while cursorStatus.next() {
  1214. listStatus.append(Int(cursorStatus.string(forColumnIndex: 0)!)!)
  1215. }
  1216. cursorStatus.close()
  1217. status = "\(listStatus.min() ?? 2)"
  1218. }
  1219. })
  1220. return status
  1221. }
  1222. }
  1223. extension ContactChatViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  1224. func updateSearchResults(for searchController: UISearchController) {
  1225. filterContentForSearchText(searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines))
  1226. }
  1227. func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat, colorText: UIColor = UIColor.black) -> NSAttributedString {
  1228. let attachment = NSTextAttachment()
  1229. attachment.image = image
  1230. attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
  1231. let attachmentStr = NSAttributedString(attachment: attachment)
  1232. let mutableAttributedString = NSMutableAttributedString()
  1233. mutableAttributedString.append(attachmentStr)
  1234. let attributedStringColor = [NSAttributedString.Key.foregroundColor : colorText]
  1235. let textString = NSAttributedString(string: text, attributes: attributedStringColor)
  1236. mutableAttributedString.append(textString)
  1237. return mutableAttributedString
  1238. }
  1239. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  1240. searchBar.showsCancelButton = true
  1241. let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
  1242. cBtn.setTitle("Cancel".localized(), for: .normal)
  1243. }
  1244. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  1245. searchBar.showsCancelButton = false
  1246. }
  1247. }