ContactChatViewController.swift 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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 chatGroupMaps: [String: [Chat]] = [:]
  20. var contacts: [User] = []
  21. var groups: [Group] = []
  22. var groupMap: [String:Int] = [:]
  23. var searchController: UISearchController!
  24. var segment: UISegmentedControl!
  25. var fillteredData: [Any] = []
  26. var isSearchBarEmpty: Bool {
  27. return searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
  28. }
  29. var isFilltering: Bool {
  30. return !isSearchBarEmpty
  31. }
  32. var noData = false
  33. var loadingData = true
  34. var isGettingData = false
  35. var timerReloadData: Timer?
  36. var noUCList = false
  37. func filterContentForSearchText(_ searchText: String) {
  38. func filterContact() {
  39. Utils.inTabChats = false
  40. fillteredData = self.contacts.filter { $0.fullName.lowercased().contains(searchText.lowercased()) }
  41. }
  42. func filterGroup() {
  43. Utils.inTabChats = false
  44. fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
  45. }
  46. if !searchText.isEmpty {
  47. if segment.numberOfSegments == 3 {
  48. switch segment.selectedSegmentIndex {
  49. case 1:
  50. fillteredData = self.contacts.filter { $0.fullName.lowercased().contains(searchText.lowercased()) }
  51. case 2:
  52. fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
  53. default:
  54. var group_id: String?
  55. if let filterGroupKey = self.chatGroupMaps.first(where: { $0.value.contains { $0.name.lowercased().contains(searchText.lowercased()) || $0.groupName.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) } } ) {
  56. group_id = filterGroupKey.key
  57. }
  58. let deepCopyChats = self.chats.map{ $0.copy() }
  59. fillteredData = deepCopyChats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) || $0.groupId == group_id }
  60. }
  61. } else {
  62. switch segment.selectedSegmentIndex {
  63. case 1:
  64. filterGroup()
  65. default:
  66. filterContact()
  67. }
  68. }
  69. }
  70. tableView.reloadData()
  71. }
  72. override func viewDidLoad() {
  73. super.viewDidLoad()
  74. let me = User.getMyPin()!
  75. Database.shared.database?.inTransaction({ fmdb, rollback in
  76. do {
  77. 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() {
  78. isAdmin = cursor.string(forColumnIndex: 3) == "23" || cursor.string(forColumnIndex: 3) == "24"
  79. cursor.close()
  80. }
  81. } catch {
  82. rollback.pointee = true
  83. print("Access database error: \(error.localizedDescription)")
  84. }
  85. })
  86. // navigationController?.navigationBar.prefersLargeTitles = true
  87. navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
  88. let childrenMenu : [UIAction] = [
  89. UIAction(title: "Create Group".localized(), image: UIImage(systemName: "person.and.person"), handler: {[weak self](_) in
  90. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "createGroupNav") as! UINavigationController
  91. Utils.addBackground(view: controller.view)
  92. let vc = controller.topViewController as! GroupCreateViewController
  93. vc.isDismiss = { id in
  94. self?.groupMap.removeAll()
  95. let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "groupDetailView") as! GroupDetailViewController
  96. controller.data = id
  97. self?.navigationController?.show(controller, sender: nil)
  98. }
  99. self?.navigationController?.present(controller, animated: true, completion: nil)
  100. }),
  101. UIAction(title: "Add Friends".localized(), image: UIImage(systemName: "person.badge.plus"), handler: {[weak self](_) in
  102. self?.addFriend(sender: UIBarButtonItem())
  103. }),
  104. // UIAction(title: "Configure Email", image: UIImage(systemName: "mail"), handler: {[weak self](_) in
  105. //
  106. // }),
  107. UIAction(title: "Favorite Messages".localized(), image: UIImage(systemName: "star"), handler: {[weak self](_) in
  108. let editorStaredVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "staredVC") as! EditorStarMessages
  109. self?.navigationController?.show(editorStaredVC, sender: nil)
  110. }),
  111. ]
  112. //debug only
  113. // isAdmin = true
  114. // if(isAdmin){
  115. // childrenMenu.append(UIAction(title: "Broadcast Message".localized(), image: UIImage(systemName: "envelope.open"), handler: {[weak self](_) in
  116. // let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "broadcastNav")
  117. // controller.modalPresentationStyle = .fullScreen
  118. // self?.navigationController?.present(controller, animated: true, completion: nil)
  119. // }))
  120. // childrenMenu.append(UIAction(title: "Live Streaming".localized(), image: UIImage(systemName: "video.bubble.left"), handler: {[weak self](_) in
  121. // let navigationController = CustomNavigationController(rootViewController: QmeraCreateStreamingViewController())
  122. // navigationController.modalPresentationStyle = .fullScreen
  123. // navigationController.navigationBar.tintColor = .white
  124. // navigationController.navigationBar.barTintColor = .mainColor
  125. // navigationController.navigationBar.isTranslucent = false
  126. // navigationController.navigationBar.overrideUserInterfaceStyle = .dark
  127. // navigationController.navigationBar.barStyle = .black
  128. // let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.white]
  129. // UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, for: .normal)
  130. // let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  131. // navigationController.navigationBar.titleTextAttributes = textAttributes
  132. // navigationController.view.backgroundColor = .mainColor
  133. // self?.navigationController?.present(navigationController, animated: true, completion: nil)
  134. // }))
  135. // }
  136. // if noUCList {
  137. // 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:)))
  138. // navigationItem.rightBarButtonItem = buttonAddFriend
  139. // } else {
  140. // let menu = UIMenu(title: "", children: childrenMenu)
  141. // navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, image: UIImage(systemName: "ellipsis"), primaryAction: .none, menu: menu)
  142. // }
  143. let menu = UIMenu(title: "", children: childrenMenu)
  144. navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, image: UIImage(systemName: "ellipsis"), primaryAction: .none, menu: menu)
  145. searchController = UISearchController(searchResultsController: nil)
  146. searchController.delegate = self
  147. searchController.searchResultsUpdater = self
  148. searchController.searchBar.autocapitalizationType = .none
  149. searchController.searchBar.delegate = self
  150. searchController.obscuresBackgroundDuringPresentation = false
  151. // searchController.searchBar.setMagnifyingGlassColorTo(color: .white)
  152. // searchController.searchBar.updateHeight(height: 36, radius: 18)
  153. searchController.searchBar.setImage(UIImage(), for: .search, state: .normal)
  154. searchController.searchBar.setPositionAdjustment(UIOffset(horizontal: 10, vertical: 0), for: .search)
  155. searchController.searchBar.setCustomBackgroundImage(image: UIImage(named: self.traitCollection.userInterfaceStyle == .dark ? "nx_search_bar_dark" : "nx_search_bar", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!)
  156. searchController.searchBar.tintColor = .mainColor
  157. searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString(string: "Search".localized(), attributes: [NSAttributedString.Key.foregroundColor: UIColor.gray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)])
  158. navigationItem.searchController = searchController
  159. navigationItem.hidesSearchBarWhenScrolling = false
  160. definesPresentationContext = true
  161. var dataSegment = ["Chats".localized(), "Contacts".localized(), "Forums".localized()]
  162. if noUCList{
  163. dataSegment = ["Contacts".localized(), "Forums".localized()]
  164. }
  165. segment = UISegmentedControl(items: dataSegment)
  166. segment.sizeToFit()
  167. segment.selectedSegmentIndex = 0
  168. segment.addTarget(self, action: #selector(segmentChanged(sender:)), for: .valueChanged)
  169. segment.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12.0 + String.offset() * 0.5)], for: .normal)
  170. Utils.inTabChats = true
  171. NotificationCenter.default.addObserver(self, selector: #selector(onStatusChat(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerStatusChat), object: nil)
  172. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: Nexilis.listenerReceiveChat), object: nil)
  173. NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onMember"), object: nil)
  174. NotificationCenter.default.addObserver(self, selector: #selector(onReloadTab(notification:)), name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil)
  175. NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onUpdatePersonInfo"), object: nil)
  176. NotificationCenter.default.addObserver(self, selector: #selector(onReloadTab(notification:)), name: NSNotification.Name(rawValue: "onTopic"), object: nil)
  177. NotificationCenter.default.addObserver(self, selector: #selector(onDisconnected(notification:)), name: NSNotification.Name(rawValue: "disconnected_nexilis"), object: nil)
  178. tableView.tableHeaderView = segment
  179. tableView.tableFooterView = UIView()
  180. pullBuddy()
  181. // getData()
  182. }
  183. override func viewWillAppear(_ animated: Bool) {
  184. let attributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16.0), NSAttributedString.Key.foregroundColor: UIColor.white]
  185. let navBarAppearance = UINavigationBarAppearance()
  186. navBarAppearance.configureWithOpaqueBackground()
  187. navBarAppearance.backgroundColor = self.traitCollection.userInterfaceStyle == .dark ? .blackDarkMode : UIColor.mainColor
  188. navBarAppearance.titleTextAttributes = attributes
  189. navigationController?.navigationBar.standardAppearance = navBarAppearance
  190. navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
  191. // removeAllData()
  192. // getData()
  193. }
  194. override func viewWillDisappear(_ animated: Bool) {
  195. Utils.inTabChats = false
  196. }
  197. override func viewDidAppear(_ animated: Bool) {
  198. if isChooser != nil {
  199. self.navigationController?.navigationBar.topItem?.title = "Forward Messages".localized()
  200. self.navigationController?.navigationBar.setNeedsLayout()
  201. } else if noUCList{
  202. self.navigationController?.navigationBar.topItem?.title = "Start Chat".localized()
  203. self.navigationController?.navigationBar.setNeedsLayout()
  204. } else {
  205. self.navigationController?.navigationBar.topItem?.title = "Start Conversation".localized()
  206. self.navigationController?.navigationBar.setNeedsLayout()
  207. }
  208. getData()
  209. APIS.setDataForShareExtension()
  210. }
  211. @objc func addFriend(sender: UIBarButtonItem) {
  212. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "addFriendNav") as! UINavigationController
  213. Utils.addBackground(view: controller.view)
  214. if let vc = controller.viewControllers.first as? AddFriendTableViewController {
  215. vc.isDismiss = {
  216. self.getContacts {
  217. DispatchQueue.main.async {
  218. self.tableView.reloadData()
  219. }
  220. }
  221. }
  222. }
  223. navigationController?.present(controller, animated: true, completion: nil)
  224. }
  225. // func removeAllData() {
  226. // groups.removeAll()
  227. // groupMap.removeAll()
  228. // chats.removeAll()
  229. // tableView.reloadData()
  230. // }
  231. private func reloadAllData() {
  232. DispatchQueue.main.async { [weak self] in
  233. if self?.timerReloadData == nil {
  234. self?.getData()
  235. } else {
  236. self?.timerReloadData?.invalidate()
  237. self?.timerReloadData = nil
  238. self?.timerReloadData = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { [weak self] _ in
  239. self?.getData()
  240. self?.timerReloadData = nil
  241. }
  242. }
  243. }
  244. }
  245. @objc func onDisconnected(notification: NSNotification) {
  246. DispatchQueue.main.async {
  247. self.loadingData = true
  248. self.chatGroupMaps.removeAll()
  249. self.chats.removeAll()
  250. self.contacts.removeAll()
  251. self.groups.removeAll()
  252. self.tableView.reloadData()
  253. self.reloadAllData()
  254. }
  255. }
  256. @objc func onReloadTab(notification: NSNotification) {
  257. reloadAllData()
  258. }
  259. @objc func onReload(notification: NSNotification) {
  260. let data:[AnyHashable : Any] = notification.userInfo!
  261. if data["member"] as? String == User.getMyPin()! {
  262. reloadAllData()
  263. } else if data["state"] as? Int == 99 {
  264. reloadAllData()
  265. }
  266. }
  267. @objc func onReceiveMessage(notification: NSNotification) {
  268. reloadAllData()
  269. }
  270. @objc func onStatusChat(notification: NSNotification) {
  271. reloadAllData()
  272. }
  273. @objc func add(sender: Any) {
  274. }
  275. @objc func cancel(sender: Any) {
  276. navigationController?.dismiss(animated: true, completion: nil)
  277. }
  278. @objc func segmentChanged(sender: Any) {
  279. switch segment.selectedSegmentIndex {
  280. case 0:
  281. if segment.numberOfSegments == 3 {
  282. Utils.inTabChats = true
  283. }
  284. case 1:
  285. Utils.inTabChats = false
  286. if segment.numberOfSegments < 3 {
  287. DispatchQueue.global().async {
  288. self.getOpenGroups(listGroups: self.groups, completion: { g in
  289. DispatchQueue.main.async {
  290. for og in g {
  291. if self.groups.first(where: { $0.id == og.id }) == nil {
  292. self.groups.append(og)
  293. }
  294. }
  295. self.groups.sort { (a, b) -> Bool in
  296. if Int(a.official) == 1 {
  297. return true
  298. } else if Int(b.official) == 1 {
  299. return false
  300. } else {
  301. return Int(a.official) ?? 0 > Int(b.official) ?? 0
  302. }
  303. }
  304. DispatchQueue.main.async {
  305. self.tableView.reloadData()
  306. }
  307. }
  308. })
  309. }
  310. }
  311. case 2:
  312. Utils.inTabChats = false
  313. DispatchQueue.global().async {
  314. self.getOpenGroups(listGroups: self.groups, completion: { g in
  315. DispatchQueue.main.async {
  316. for og in g {
  317. if self.groups.first(where: { $0.id == og.id }) == nil {
  318. self.groups.append(og)
  319. }
  320. }
  321. self.groups.sort { (a, b) -> Bool in
  322. if Int(a.official) == 1 {
  323. return true
  324. } else if Int(b.official) == 1 {
  325. return false
  326. } else {
  327. return Int(a.official) ?? 0 > Int(b.official) ?? 0
  328. }
  329. }
  330. DispatchQueue.main.async {
  331. self.tableView.reloadData()
  332. }
  333. }
  334. })
  335. }
  336. default:
  337. Utils.inTabChats = false
  338. }
  339. filterContentForSearchText(searchController.searchBar.text!)
  340. }
  341. // MARK: - Data source
  342. func getData() {
  343. if self.isGettingData {
  344. return
  345. }
  346. self.isGettingData = true
  347. getChats {
  348. self.getContacts {
  349. self.getGroups { g1 in
  350. self.groupMap.removeAll()
  351. self.groups = g1
  352. self.groups.sort { (a, b) -> Bool in
  353. if Int(a.official) == 1 {
  354. return true
  355. } else if Int(b.official) == 1 {
  356. return false
  357. } else {
  358. return Int(a.official) ?? 0 > Int(b.official) ?? 0
  359. }
  360. }
  361. DispatchQueue.main.async {
  362. self.tableView.reloadData()
  363. self.isGettingData = false
  364. }
  365. }
  366. }
  367. }
  368. }
  369. func getChats(completion: @escaping ()->()) {
  370. DispatchQueue.global().async {
  371. self.chatGroupMaps.removeAll()
  372. let previousChat = self.chats
  373. let allChats = Chat.getData()
  374. var tempChats: [Chat] = []
  375. for singleChat in allChats {
  376. if !singleChat.groupId.isEmpty {
  377. let chatParentInPreviousChats = previousChat.first(where: { $0.isParent && $0.groupId == singleChat.groupId })
  378. if self.chatGroupMaps[singleChat.groupId] != nil {
  379. self.chatGroupMaps[singleChat.groupId]!.insert(singleChat, at: 0)
  380. if let parentChat = tempChats.first(where: { $0.groupId == singleChat.groupId && $0.isParent }) {
  381. let counterParent = parentChat.counter
  382. parentChat.counter = "\(Int(counterParent)! + Int(singleChat.counter)!)"
  383. }
  384. if let parentExist = chatParentInPreviousChats, parentExist.isSelected {
  385. if let indexParent = tempChats.firstIndex(where: { $0.isParent && $0.groupId == singleChat.groupId }){
  386. tempChats.insert(singleChat, at: indexParent + self.chatGroupMaps[singleChat.groupId]!.count)
  387. }
  388. }
  389. } else {
  390. self.chatGroupMaps[singleChat.groupId] = [singleChat]
  391. let parentChat = Chat(profile: singleChat.profile, groupName: singleChat.groupName, counter: singleChat.counter, groupId: singleChat.groupId)
  392. parentChat.isParent = true
  393. if let parentExist = chatParentInPreviousChats, parentExist.isSelected {
  394. parentChat.isSelected = true
  395. tempChats.append(parentChat)
  396. tempChats.append(singleChat)
  397. } else {
  398. tempChats.append(parentChat)
  399. }
  400. }
  401. } else {
  402. tempChats.append(singleChat)
  403. }
  404. }
  405. if self.isChooser != nil {
  406. tempChats.removeAll(where: { $0.pin == "-997" })
  407. }
  408. self.chats = tempChats
  409. completion()
  410. }
  411. }
  412. private func getContacts(completion: @escaping ()->()) {
  413. self.contacts.removeAll()
  414. DispatchQueue.global().async {
  415. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  416. do {
  417. if self.isChooser == nil {
  418. let gptUser = User(pin: "-997",
  419. firstName: "GPT SmartBot",
  420. lastName: "",
  421. thumb: "",
  422. userType: "0",
  423. official: "1")
  424. self.contacts.append(gptUser)
  425. }
  426. 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 <> '\(User.getMyPin()!)' order by 5 desc, 2 collate nocase asc") {
  427. while cursorData.next() {
  428. let user = User(pin: cursorData.string(forColumnIndex: 0) ?? "",
  429. firstName: cursorData.string(forColumnIndex: 1) ?? "",
  430. lastName: cursorData.string(forColumnIndex: 2) ?? "",
  431. thumb: cursorData.string(forColumnIndex: 3) ?? "",
  432. userType: cursorData.string(forColumnIndex: 5) ?? "")
  433. if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
  434. continue
  435. }
  436. user.official = cursorData.string(forColumnIndex: 4) ?? ""
  437. if !self.contacts.contains(where: {$0.pin == user.pin}) {
  438. self.contacts.append(user)
  439. }
  440. }
  441. cursorData.close()
  442. }
  443. completion()
  444. } catch {
  445. rollback.pointee = true
  446. print("Access database error: \(error.localizedDescription)")
  447. }
  448. })
  449. }
  450. }
  451. private func getGroupRecursive(fmdb: FMDatabase, id: String = "", parent: String = "") -> [Group] {
  452. var data: [Group] = []
  453. 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, g.chat_modifier from GROUPZ g where "
  454. if id.isEmpty {
  455. query += "g.parent = '\(parent)'"
  456. } else {
  457. query += "g.group_id = '\(id)'"
  458. }
  459. query += "order by 12 asc, 13 asc, 2 asc"
  460. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query) {
  461. while cursor.next() {
  462. let group = Group(
  463. id: cursor.string(forColumnIndex: 0) ?? "",
  464. name: cursor.string(forColumnIndex: 1) ?? "",
  465. profile: cursor.string(forColumnIndex: 2) ?? "",
  466. quote: cursor.string(forColumnIndex: 3) ?? "",
  467. by: cursor.string(forColumnIndex: 4) ?? "",
  468. date: cursor.string(forColumnIndex: 5) ?? "",
  469. parent: cursor.string(forColumnIndex: 6) ?? "",
  470. chatId: "",
  471. groupType: cursor.string(forColumnIndex: 7) ?? "",
  472. isOpen: cursor.string(forColumnIndex: 8) ?? "",
  473. official: cursor.string(forColumnIndex: 9) ?? "",
  474. isEducation: cursor.string(forColumnIndex: 10) ?? "",
  475. level: cursor.string(forColumnIndex: 11) ?? "")
  476. if group.chatId.isEmpty {
  477. 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")
  478. group.childs.append(lounge)
  479. }
  480. if let topicCursor = Database.shared.getRecords(fmdb: fmdb, query: "select chat_id, title, thumb from DISCUSSION_FORUM where group_id = '\(group.id)'") {
  481. while topicCursor.next() {
  482. let topic = Group(id: group.id,
  483. name: topicCursor.string(forColumnIndex: 1) ?? "",
  484. profile: topicCursor.string(forColumnIndex: 2) ?? "",
  485. quote: group.quote,
  486. by: group.by,
  487. date: group.date,
  488. parent: group.id,
  489. chatId: topicCursor.string(forColumnIndex: 0) ?? "",
  490. groupType: group.groupType,
  491. isOpen: group.isOpen,
  492. official: group.official,
  493. isEducation: group.isEducation,
  494. level: group.level != "-1" ? group.level : "2")
  495. group.childs.append(topic)
  496. }
  497. topicCursor.close()
  498. }
  499. if !group.id.isEmpty {
  500. // if group.official == "1" {
  501. // let idMe = User.getMyPin() as String?
  502. // if let cursorUser = Database.shared.getRecords(fmdb: fmdb, query: "SELECT user_type FROM BUDDY where f_pin='\(idMe!)'"), cursorUser.next() {
  503. //// if cursorUser.string(forColumnIndex: 0) == "23" || cursorUser.string(forColumnIndex: 0) == "24" {
  504. //// group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  505. //// }
  506. // group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  507. // cursorUser.close()
  508. // }
  509. // } else if group.official != "1"{
  510. // group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  511. // }
  512. group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  513. // group.childs = group.childs.sorted(by: { $0.name < $1.name })
  514. // let dataLounge = group.childs.filter({$0.name == "Lounge".localized()})
  515. // group.childs = group.childs.filter({ $0.name != "Lounge".localized() })
  516. // group.childs.insert(contentsOf: dataLounge, at: 0)
  517. }
  518. data.append(group)
  519. }
  520. cursor.close()
  521. }
  522. return data
  523. }
  524. private func getOpenGroups(listGroups: [Group], completion: @escaping ([Group]) -> ()) {
  525. while Nexilis.isProcessWriteSync {
  526. Thread.sleep(forTimeInterval: 0.5)
  527. }
  528. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getOpenGroups(p_account: "1,2,3,5,6,7", offset: "0", search: "")) {
  529. var dataGroups: [Group] = []
  530. if (response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "00") {
  531. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  532. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
  533. for dataJson in json {
  534. let group = Group(
  535. id: dataJson[CoreMessage_TMessageKey.GROUP_ID] as? String ?? "",
  536. name: dataJson[CoreMessage_TMessageKey.GROUP_NAME] as? String ?? "",
  537. profile: dataJson[CoreMessage_TMessageKey.THUMB_ID] as? String ?? "",
  538. quote: dataJson[CoreMessage_TMessageKey.QUOTE] as? String ?? "",
  539. by: dataJson[CoreMessage_TMessageKey.BLOCK] as? String ?? "",
  540. date: "",
  541. parent: "",
  542. chatId: "",
  543. groupType: "NOTJOINED",
  544. isOpen: dataJson[CoreMessage_TMessageKey.IS_OPEN] as? String ?? "",
  545. official: "0",
  546. isEducation: "")
  547. dataGroups.append(group)
  548. }
  549. }
  550. } else {
  551. DispatchQueue.main.async {
  552. self.groups.removeAll()
  553. self.groups.append(contentsOf: listGroups)
  554. self.tableView.reloadData()
  555. }
  556. }
  557. completion(dataGroups)
  558. }
  559. }
  560. private func getGroups(id: String = "", parent: String = "", completion: @escaping ([Group]) -> ()) {
  561. DispatchQueue.global().async {
  562. Database.shared.database?.inTransaction({ fmdb, rollback in
  563. do {
  564. completion(self.getGroupRecursive(fmdb: fmdb, id: id, parent: parent))
  565. } catch {
  566. rollback.pointee = true
  567. print("Access database error: \(error.localizedDescription)")
  568. }
  569. })
  570. }
  571. }
  572. private func pullBuddy() {
  573. if let me = User.getMyPin() {
  574. DispatchQueue.global().async {
  575. let _ = Nexilis.write(message: CoreMessage_TMessageBank.getBatchBuddiesInfos(p_f_pin: me, last_update: 0))
  576. }
  577. }
  578. }
  579. private func joinOpenGroup(groupId: String, flagMember: String = "0", completion: @escaping (Bool) -> ()) {
  580. DispatchQueue.global().async {
  581. var result: Bool = false
  582. let idMe = User.getMyPin() as String?
  583. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddGroupMember(p_group_id: groupId, p_member_pin: idMe!, p_position: "0")), response.isOk() {
  584. result = true
  585. }
  586. completion(result)
  587. }
  588. }
  589. }
  590. // MARK: - Table view data source
  591. extension ContactChatViewController {
  592. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  593. if noData {
  594. return
  595. }
  596. tableView.deselectRow(at: indexPath, animated: true)
  597. func selectOnContact() {
  598. let data: User
  599. if isFilltering {
  600. data = fillteredData[indexPath.row] as! User
  601. } else {
  602. data = contacts[indexPath.row]
  603. }
  604. if data.pin == "-997" {
  605. let smartChatVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "chatGptVC") as! ChatGPTBotView
  606. smartChatVC.hidesBottomBarWhenPushed = true
  607. smartChatVC.fromNotification = false
  608. navigationController?.show(smartChatVC, sender: nil)
  609. return
  610. }
  611. if let chooser = isChooser {
  612. var exblock = User.getDataCanNil(pin: data.pin)?.ex_block
  613. exblock = exblock == nil ? "0" : exblock!.isEmpty ? "0" : exblock!
  614. if exblock != "0" {
  615. if exblock == "1" {
  616. self.view.makeToast("You blocked this user".localized(), duration: 3)
  617. } else {
  618. self.view.makeToast("You have been blocked by this user".localized(), duration: 3)
  619. }
  620. return
  621. }
  622. chooser("3", data.pin)
  623. dismiss(animated: true, completion: nil)
  624. return
  625. }
  626. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  627. editorPersonalVC.hidesBottomBarWhenPushed = true
  628. editorPersonalVC.unique_l_pin = data.pin
  629. navigationController?.show(editorPersonalVC, sender: nil)
  630. }
  631. if segment.numberOfSegments == 3 {
  632. switch segment.selectedSegmentIndex {
  633. case 0:
  634. let data: Chat
  635. if isFilltering {
  636. data = fillteredData[indexPath.row] as! Chat
  637. } else {
  638. data = chats[indexPath.row]
  639. }
  640. if data.isParent {
  641. expandCollapseChats(tableView: tableView, indexPath: indexPath)
  642. return
  643. }
  644. if let chooser = isChooser {
  645. var exblock = User.getDataCanNil(pin: data.pin)?.ex_block
  646. exblock = exblock == nil ? "0" : exblock!.isEmpty ? "0" : exblock!
  647. if exblock != "0" {
  648. if exblock == "1" {
  649. self.view.makeToast("You blocked this user".localized(), duration: 3)
  650. } else {
  651. self.view.makeToast("You have been blocked by this user".localized(), duration: 3)
  652. }
  653. return
  654. }
  655. if data.pin == "-999"{
  656. return
  657. }
  658. chooser(data.messageScope, data.pin)
  659. dismiss(animated: true, completion: nil)
  660. return
  661. }
  662. if data.pin == "-997" {
  663. let smartChatVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "chatGptVC") as! ChatGPTBotView
  664. smartChatVC.hidesBottomBarWhenPushed = true
  665. smartChatVC.fromNotification = false
  666. navigationController?.show(smartChatVC, sender: nil)
  667. } else if data.messageScope == MessageScope.WHISPER || data.messageScope == MessageScope.CALL || data.messageScope == MessageScope.MISSED_CALL {
  668. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  669. editorPersonalVC.hidesBottomBarWhenPushed = true
  670. editorPersonalVC.unique_l_pin = data.pin
  671. navigationController?.show(editorPersonalVC, sender: nil)
  672. } else {
  673. groupMap.removeAll()
  674. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  675. editorGroupVC.hidesBottomBarWhenPushed = true
  676. editorGroupVC.unique_l_pin = data.pin
  677. navigationController?.show(editorGroupVC, sender: nil)
  678. }
  679. case 1:
  680. selectOnContact()
  681. case 2:
  682. expandCollapseGroup(tableView: tableView, indexPath: indexPath)
  683. default:
  684. let data = contacts[indexPath.row]
  685. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  686. editorPersonalVC.hidesBottomBarWhenPushed = true
  687. editorPersonalVC.unique_l_pin = data.pin
  688. navigationController?.show(editorPersonalVC, sender: nil)
  689. }
  690. } else {
  691. switch segment.selectedSegmentIndex {
  692. case 0:
  693. selectOnContact()
  694. case 1:
  695. expandCollapseGroup(tableView: tableView, indexPath: indexPath)
  696. default:
  697. let data = contacts[indexPath.row]
  698. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  699. editorPersonalVC.hidesBottomBarWhenPushed = true
  700. editorPersonalVC.unique_l_pin = data.pin
  701. navigationController?.show(editorPersonalVC, sender: nil)
  702. }
  703. }
  704. }
  705. func expandCollapseChats(tableView: UITableView, indexPath: IndexPath) {
  706. let data: Chat
  707. if isFilltering {
  708. data = fillteredData[indexPath.row] as! Chat
  709. } else {
  710. data = chats[indexPath.row]
  711. }
  712. data.isSelected = !data.isSelected
  713. if data.isSelected {
  714. if let dataSubChats = self.chatGroupMaps[data.groupId] {
  715. for dataSubChat in dataSubChats {
  716. if var indexParent = chats.firstIndex(where: { $0.isParent && $0.groupId == data.groupId }) {
  717. if isFilltering {
  718. fillteredData.insert(dataSubChat, at: indexParent + 1)
  719. indexParent+=1
  720. } else {
  721. chats.insert(dataSubChat, at: indexParent + 1)
  722. indexParent+=1
  723. }
  724. }
  725. }
  726. }
  727. } else {
  728. if isFilltering {
  729. if var changedFillteredData = fillteredData as? [Chat] {
  730. changedFillteredData.removeAll(where: { $0.isParent == false && $0.groupId == data.groupId })
  731. self.fillteredData = changedFillteredData
  732. }
  733. } else {
  734. chats.removeAll(where: { $0.isParent == false && $0.groupId == data.groupId })
  735. }
  736. }
  737. tableView.reloadData()
  738. }
  739. func expandCollapseGroup(tableView: UITableView, indexPath: IndexPath) {
  740. let group: Group
  741. if isFilltering {
  742. if indexPath.row == 0 {
  743. group = fillteredData[indexPath.section] as! Group
  744. } else {
  745. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  746. }
  747. } else {
  748. if indexPath.row == 0 {
  749. group = groups[indexPath.section]
  750. } else {
  751. group = groups[indexPath.section].childs[indexPath.row - 1]
  752. }
  753. }
  754. if (checkOverrideAction(groupHolder: group)) {
  755. return;
  756. }
  757. group.isSelected = !group.isSelected
  758. if !group.isSelected{
  759. var sects = 0
  760. var sect = indexPath.section
  761. var id = group.id
  762. if let _ = groupMap[id] {
  763. var loooop = true
  764. repeat {
  765. let c = sect + 1
  766. if isFilltering {
  767. if let o = self.fillteredData[c] as? Group {
  768. if o.parent == id {
  769. sects = sects + 1
  770. sect = c
  771. id = o.id
  772. (self.fillteredData[c] as! Group).isSelected = false
  773. self.groupMap.removeValue(forKey: (self.fillteredData[c] as! Group).id)
  774. }
  775. else {
  776. loooop = false
  777. }
  778. }
  779. }
  780. else {
  781. if c < self.groups.count && self.groups[c].parent == id {
  782. sects = sects + 1
  783. sect = c
  784. id = self.groups[c].id
  785. self.groups[c].isSelected = false
  786. self.groupMap.removeValue(forKey: self.groups[c].id)
  787. }
  788. else {
  789. loooop = false
  790. }
  791. }
  792. } while(loooop)
  793. }
  794. for i in stride(from: sects, to: 0, by: -1){
  795. if isFilltering {
  796. self.fillteredData.remove(at: indexPath.section + i)
  797. }
  798. else {
  799. self.groups.remove(at: indexPath.section + i)
  800. }
  801. }
  802. groupMap.removeValue(forKey: group.id)
  803. }
  804. if group.groupType == "NOTJOINED" {
  805. let alert = LibAlertController(title: "Do you want to join this group?".localized(), message: "Groups : \(group.name)\nMembers: \(group.by)".localized(), preferredStyle: .alert)
  806. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  807. alert.addAction(UIAlertAction(title: "Join".localized(), style: .default, handler: {(_) in
  808. self.joinOpenGroup(groupId: group.id, completion: { result in
  809. if result {
  810. DispatchQueue.main.async {
  811. self.groupMap.removeAll()
  812. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  813. editorGroupVC.hidesBottomBarWhenPushed = true
  814. editorGroupVC.unique_l_pin = group.id
  815. self.navigationController?.show(editorGroupVC, sender: nil)
  816. }
  817. }
  818. })
  819. }))
  820. self.present(alert, animated: true, completion: nil)
  821. return
  822. }
  823. if group.childs.count == 0 {
  824. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  825. do {
  826. let idMe = User.getMyPin() as String?
  827. 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() {
  828. let groupId = group.chatId.isEmpty ? group.id : group.chatId
  829. if let chooser = isChooser {
  830. chooser("4", groupId)
  831. dismiss(animated: true, completion: nil)
  832. return
  833. }
  834. self.groupMap.removeAll()
  835. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  836. editorGroupVC.hidesBottomBarWhenPushed = true
  837. editorGroupVC.unique_l_pin = groupId
  838. navigationController?.show(editorGroupVC, sender: nil)
  839. cursorMember.close()
  840. } else {
  841. self.view.makeToast("You are not a member of this group".localized(), duration: 3)
  842. }
  843. } catch {
  844. rollback.pointee = true
  845. print("Access database error: \(error.localizedDescription)")
  846. }
  847. })
  848. } else {
  849. if indexPath.row == 0 {
  850. tableView.reloadData()
  851. } else {
  852. getGroups(id: group.id) { g in
  853. DispatchQueue.main.async {
  854. if self.isFilltering {
  855. if self.fillteredData[indexPath.section] is Group {
  856. self.groupMap[(self.fillteredData[indexPath.section] as! Group).id] = 1
  857. self.fillteredData.insert(contentsOf: g, at: indexPath.section + 1)
  858. }
  859. } else {
  860. self.groupMap[self.groups[indexPath.section].id] = 1
  861. self.groups.insert(contentsOf: g, at: indexPath.section + 1)
  862. }
  863. tableView.reloadData()
  864. self.expandCollapseGroup(tableView: tableView, indexPath: IndexPath(row: 0, section: indexPath.section + 1))
  865. }
  866. }
  867. }
  868. }
  869. }
  870. func checkOverrideAction(groupHolder: Group) -> Bool {
  871. if groupHolder.isLounge {
  872. return false
  873. }
  874. let groupId = groupHolder.chatId.isEmpty ? groupHolder.id : groupHolder.chatId
  875. switch (groupId){
  876. case "18d1c6cffb70215af7b49" //bpkh konsultasi
  877. , "18d1c6e37a20215af7b49"
  878. , "18d1c6f852d0215af7b49"
  879. , "18d1c6ff83a0215af7b49"
  880. , "18d1c705e970215af7b49"
  881. , "18d30db3bde0230d00c15" //ina konsultasi bot
  882. , "18d30e64ce30230d00c15"
  883. , "18d30e9b6d80230d00c15"
  884. , "18d30ee00610230d00c15"
  885. , "18d30f02f850230d00c15":
  886. APIS.openSmartChatbot();
  887. return true;
  888. case "18d30daa4540230d00c15" //ina cc
  889. , "18d30e59a950230d00c15"
  890. , "18d30e9292b0230d00c15"
  891. , "18d30ed8e250230d00c15"
  892. , "18d30efa66c0230d00c15"
  893. , "18d35b220540215af7b49" //bpkhcc
  894. , "18d35b2f5ee0215af7b49"
  895. , "18d35b356530215af7b49"
  896. , "18d35b411510215af7b49"
  897. , "18d35b46ae90215af7b49":
  898. APIS.openContactCenter();
  899. return true;
  900. case "18d1c6d9f330215af7b49": //bpkh haji
  901. APIS.openUrl(url: Utils.decrypt(str: "6]tov!l_opgn=hgz?ykmgv?yoro3kt?uo>yoro3kt??@yvzzn"))
  902. return true;
  903. case "18d1c6eefd40215af7b49": //bpkh bpjs
  904. APIS.openUrl(url: Utils.decrypt(str: "1>ojq`g@tkqc.cbu:tfhbq:tjmjyfo:pj/tjmjyfo::;tquui"))
  905. return true;
  906. case "18d30e711c20230d00c15": //ina bpjs
  907. APIS.openUrl(url: Utils.decrypt(str: "6]tov!l_ypvh=hgz?ykmgv?yoro3kt?sui>ykrgyomojrkyz??@yvzzn"))
  908. return true;
  909. case "18d30e47ae60230d00c15": //ina KTP, KK, SKL
  910. APIS.openUrl(url: Utils.decrypt(str: "1>ojq`g@qul.cbu:tfhbq:tjmjyfo:npd/tfmbtjhjemftu::;tquui"))
  911. return true;
  912. case "18d30eb2e910230d00c15": //SIM, SKKB, SKBN
  913. APIS.openUrl(url: Utils.decrypt(str: "4[rmt}j]qmw;fex=wiket=wmpm1ir=qsg<wipewmkmhpiwx==>wtxxl"))
  914. return true;
  915. case "18da1c0200f0215af7b49": //BPKH index BMI
  916. APIS.openUrl(url: Utils.decrypt(str: "4[rmt}j]mqf}1ihrm=mqf=wmpm1ir=sm<wmpm1ir==>wtxxl"))
  917. return true;
  918. default:
  919. break;
  920. }
  921. return false
  922. }
  923. }
  924. extension ContactChatViewController {
  925. override func numberOfSections(in tableView: UITableView) -> Int {
  926. if isFilltering {
  927. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)) {
  928. return fillteredData.count
  929. }
  930. return 1
  931. } else {
  932. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)) {
  933. return groups.count
  934. }
  935. return 1
  936. }
  937. }
  938. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  939. var value = 0
  940. if isFilltering {
  941. func filterGroup(groups: [Group]) {
  942. let group = groups[section]
  943. if group.isSelected {
  944. if let _ = groupMap[group.id] {
  945. value = 1
  946. }
  947. else {
  948. value = group.childs.count + 1
  949. }
  950. } else {
  951. value = 1
  952. }
  953. }
  954. if ((segment.numberOfSegments == 3 && segment.selectedSegmentIndex == 2) || (segment.numberOfSegments < 3 && segment.selectedSegmentIndex == 1)), let groups = fillteredData as? [Group] {
  955. filterGroup(groups: groups)
  956. } else {
  957. value = fillteredData.count
  958. }
  959. return value
  960. }
  961. if segment.numberOfSegments == 3 {
  962. switch segment.selectedSegmentIndex {
  963. case 0:
  964. value = chats.count
  965. case 1:
  966. value = contacts.count
  967. case 2:
  968. let group = groups[section]
  969. if group.isSelected {
  970. if let _ = groupMap[group.id] {
  971. value = 1
  972. }
  973. else {
  974. value = group.childs.count + 1
  975. }
  976. } else {
  977. value = 1
  978. }
  979. default:
  980. value = chats.count
  981. }
  982. } else {
  983. switch segment.selectedSegmentIndex {
  984. case 0:
  985. value = contacts.count
  986. case 1:
  987. let group = groups[section]
  988. if group.isSelected {
  989. if let _ = groupMap[group.id] {
  990. value = 1
  991. }
  992. else {
  993. value = group.childs.count + 1
  994. }
  995. } else {
  996. value = 1
  997. }
  998. default:
  999. value = chats.count
  1000. }
  1001. }
  1002. if value == 0 {
  1003. noData = true
  1004. value = 1
  1005. tableView.separatorStyle = .none
  1006. } else {
  1007. noData = false
  1008. tableView.separatorStyle = .singleLine
  1009. }
  1010. return value
  1011. }
  1012. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  1013. var cell: UITableViewCell!
  1014. switch segment.selectedSegmentIndex {
  1015. case 0:
  1016. if segment.numberOfSegments < 3 {
  1017. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  1018. let content = cell.contentView
  1019. if content.subviews.count > 0 {
  1020. content.subviews.forEach { $0.removeFromSuperview() }
  1021. }
  1022. let data: User
  1023. if isFilltering {
  1024. data = fillteredData[indexPath.row] as! User
  1025. } else {
  1026. if indexPath.row > contacts.count - 1 {
  1027. return cell
  1028. }
  1029. data = contacts[indexPath.row]
  1030. }
  1031. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40.0, height: 40.0))
  1032. content.addSubview(imageView)
  1033. imageView.translatesAutoresizingMaskIntoConstraints = false
  1034. NSLayoutConstraint.activate([
  1035. imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 20.0),
  1036. imageView.centerYAnchor.constraint(equalTo: content.centerYAnchor),
  1037. imageView.widthAnchor.constraint(equalToConstant: 40.0),
  1038. imageView.heightAnchor.constraint(equalToConstant: 40.0)
  1039. ])
  1040. if data.pin == "-997" {
  1041. imageView.circle()
  1042. if let urlGif = Bundle.resourceBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {//resourcesMediaBundle
  1043. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1044. if error == nil {
  1045. imageView.animationImages = image?.images
  1046. imageView.animationDuration = image?.duration ?? 0.0
  1047. imageView.animationRepeatCount = 0
  1048. imageView.startAnimating()
  1049. }
  1050. }
  1051. } else if let urlGif = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {
  1052. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1053. if error == nil {
  1054. imageView.animationImages = image?.images
  1055. imageView.animationDuration = image?.duration ?? 0.0
  1056. imageView.animationRepeatCount = 0
  1057. imageView.startAnimating()
  1058. }
  1059. }
  1060. }
  1061. }
  1062. else {
  1063. getImage(name: data.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  1064. imageView.image = image
  1065. })
  1066. }
  1067. let titleView = UILabel()
  1068. content.addSubview(titleView)
  1069. titleView.translatesAutoresizingMaskIntoConstraints = false
  1070. NSLayoutConstraint.activate([
  1071. titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 15.0),
  1072. titleView.centerYAnchor.constraint(equalTo: content.centerYAnchor)
  1073. ])
  1074. titleView.font = UIFont.systemFont(ofSize: 14 + String.offset())
  1075. if (User.isOfficial(official_account: data.official ?? "") || User.isOfficialRegular(official_account: data.official ?? "")) && data.pin != "-997" {
  1076. titleView.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.officialColor)
  1077. } else if User.isVerified(official_account: data.official ?? "") {
  1078. titleView.attributedText = self.set(image: UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.verifiedColor)
  1079. }
  1080. else if User.isInternal(userType: data.userType ?? "") {
  1081. titleView.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.internalColor)
  1082. } else if User.isCallCenter(userType: data.userType ?? "") {
  1083. titleView.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.ccColor)
  1084. } else {
  1085. titleView.text = data.fullName
  1086. }
  1087. } else {
  1088. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierChat", for: indexPath)
  1089. let content = cell.contentView
  1090. if content.subviews.count > 0 {
  1091. content.subviews.forEach { $0.removeFromSuperview() }
  1092. }
  1093. if noData {
  1094. let labelNochat = UILabel()
  1095. labelNochat.text = "There are no conversations".localized()
  1096. labelNochat.font = .systemFont(ofSize: 13 + String.offset())
  1097. labelNochat.textColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  1098. content.addSubview(labelNochat)
  1099. labelNochat.anchor(centerX: content.centerXAnchor, centerY: content.centerYAnchor)
  1100. cell.backgroundColor = .clear
  1101. cell.selectionStyle = .none
  1102. return cell
  1103. }
  1104. let data: Chat
  1105. if isFilltering {
  1106. data = fillteredData[indexPath.row] as! Chat
  1107. } else {
  1108. if chats.count == 0 || (indexPath.row > (chats.count - 1)) {
  1109. let labelNochat = UILabel()
  1110. labelNochat.text = loadingData ? "Loading Data...".localized() : "There are no conversations".localized()
  1111. labelNochat.font = .systemFont(ofSize: 13 + String.offset())
  1112. labelNochat.textColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  1113. content.addSubview(labelNochat)
  1114. labelNochat.anchor(centerX: content.centerXAnchor, centerY: content.centerYAnchor)
  1115. cell.backgroundColor = .clear
  1116. cell.selectionStyle = .none
  1117. return cell
  1118. }
  1119. data = chats[indexPath.row]
  1120. }
  1121. let imageView = UIImageView()
  1122. content.addSubview(imageView)
  1123. imageView.translatesAutoresizingMaskIntoConstraints = false
  1124. NSLayoutConstraint.activate([
  1125. imageView.centerYAnchor.constraint(equalTo: content.centerYAnchor),
  1126. imageView.widthAnchor.constraint(equalToConstant: 55.0),
  1127. imageView.heightAnchor.constraint(equalToConstant: 55.0)
  1128. ])
  1129. var leadingAnchor = imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 10.0)
  1130. if data.pin == "-997" {
  1131. imageView.frame = CGRect(x: 0, y: 0, width: 55.0, height: 55.0)
  1132. imageView.circle()
  1133. if let urlGif = Bundle.resourceBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {
  1134. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1135. if error == nil {
  1136. imageView.animationImages = image?.images
  1137. imageView.animationDuration = image?.duration ?? 0.0
  1138. imageView.animationRepeatCount = 0
  1139. imageView.startAnimating()
  1140. }
  1141. }
  1142. } else if let urlGif = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {
  1143. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1144. if error == nil {
  1145. imageView.animationImages = image?.images
  1146. imageView.animationDuration = image?.duration ?? 0.0
  1147. imageView.animationRepeatCount = 0
  1148. imageView.startAnimating()
  1149. }
  1150. }
  1151. }
  1152. } else {
  1153. if !Utils.getIconDock().isEmpty && data.official == "1" {
  1154. let urlString = Utils.getUrlDock()!
  1155. if let cachedImage = ImageCache.shared.image(forKey: urlString) {
  1156. let imageData = cachedImage
  1157. imageView.image = imageData
  1158. } else {
  1159. DispatchQueue.global().async{
  1160. Utils.fetchDataWithCookiesAndUserAgent(from: URL(string: urlString)!) { data, response, error in
  1161. guard let data = data, error == nil else { return }
  1162. DispatchQueue.main.async() {
  1163. if UIImage(data: data) != nil {
  1164. let imageData = UIImage(data: data)!
  1165. imageView.image = imageData
  1166. ImageCache.shared.save(image: imageData, forKey: urlString)
  1167. }
  1168. }
  1169. }
  1170. }
  1171. }
  1172. } else {
  1173. if data.messageScope == MessageScope.WHISPER || data.messageScope == MessageScope.CALL || data.messageScope == MessageScope.MISSED_CALL || data.isParent || data.pin == "-999" {
  1174. getImage(name: data.profile, placeholderImage: UIImage(named: data.pin == "-999" ? "pb_button" : (data.messageScope == MessageScope.WHISPER || data.messageScope == MessageScope.CALL || data.messageScope == MessageScope.MISSED_CALL) ? "Profile---Purple" : "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  1175. imageView.image = image
  1176. })
  1177. } else {
  1178. leadingAnchor = imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 40.0)
  1179. let image = UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  1180. imageView.image = image
  1181. }
  1182. }
  1183. }
  1184. leadingAnchor.isActive = true
  1185. let titleView = UILabel()
  1186. content.addSubview(titleView)
  1187. titleView.translatesAutoresizingMaskIntoConstraints = false
  1188. NSLayoutConstraint.activate([
  1189. titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  1190. titleView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  1191. ])
  1192. titleView.font = UIFont.systemFont(ofSize: 14 + String.offset(), weight: .medium)
  1193. let timeView = UILabel()
  1194. let viewCounter = UIView()
  1195. if data.counter != "0" {
  1196. timeView.textColor = .systemRed
  1197. content.addSubview(viewCounter)
  1198. viewCounter.translatesAutoresizingMaskIntoConstraints = false
  1199. NSLayoutConstraint.activate([
  1200. viewCounter.widthAnchor.constraint(greaterThanOrEqualToConstant: 20),
  1201. viewCounter.heightAnchor.constraint(equalToConstant: 20)
  1202. ])
  1203. viewCounter.backgroundColor = .systemRed
  1204. viewCounter.layer.cornerRadius = 10
  1205. viewCounter.clipsToBounds = true
  1206. viewCounter.layer.borderWidth = 0.5
  1207. viewCounter.layer.borderColor = UIColor.secondaryColor.cgColor
  1208. let labelCounter = UILabel()
  1209. viewCounter.addSubview(labelCounter)
  1210. labelCounter.translatesAutoresizingMaskIntoConstraints = false
  1211. NSLayoutConstraint.activate([
  1212. labelCounter.centerYAnchor.constraint(equalTo: viewCounter.centerYAnchor),
  1213. labelCounter.leadingAnchor.constraint(equalTo: viewCounter.leadingAnchor, constant: 2),
  1214. labelCounter.trailingAnchor.constraint(equalTo: viewCounter.trailingAnchor, constant: -2),
  1215. ])
  1216. labelCounter.font = UIFont.systemFont(ofSize: 11 + String.offset())
  1217. if Int(data.counter)! > 99 {
  1218. labelCounter.text = "99+"
  1219. } else {
  1220. labelCounter.text = data.counter
  1221. }
  1222. labelCounter.textColor = .secondaryColor
  1223. labelCounter.textAlignment = .center
  1224. }
  1225. if !data.isParent {
  1226. titleView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0).isActive = true
  1227. titleView.text = data.name
  1228. content.addSubview(timeView)
  1229. timeView.translatesAutoresizingMaskIntoConstraints = false
  1230. NSLayoutConstraint.activate([
  1231. timeView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  1232. timeView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20.0),
  1233. ])
  1234. timeView.textColor = .gray
  1235. timeView.font = UIFont.systemFont(ofSize: 14 + String.offset())
  1236. let date = Date(milliseconds: Int64(data.serverDate) ?? 0)
  1237. let calendar = Calendar.current
  1238. if (calendar.isDateInToday(date)) {
  1239. let formatter = DateFormatter()
  1240. formatter.dateFormat = "HH:mm"
  1241. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  1242. timeView.text = formatter.string(from: date as Date)
  1243. } else {
  1244. let startOfNow = calendar.startOfDay(for: Date())
  1245. let startOfTimeStamp = calendar.startOfDay(for: date)
  1246. let components = calendar.dateComponents([.day], from: startOfNow, to: startOfTimeStamp)
  1247. let day = -(components.day!)
  1248. if day == 1 {
  1249. timeView.text = "Yesterday".localized()
  1250. } else {
  1251. if day < 7 {
  1252. let formatter = DateFormatter()
  1253. formatter.dateFormat = "EEEE"
  1254. let lang: String = SecureUserDefaults.shared.value(forKey: "i18n_language") ?? "en"
  1255. if lang == "id" {
  1256. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  1257. }
  1258. timeView.text = formatter.string(from: date)
  1259. } else {
  1260. let formatter = DateFormatter()
  1261. formatter.dateFormat = "M/dd/yy"
  1262. formatter.locale = NSLocale(localeIdentifier: "id") as Locale?
  1263. let stringFormat = formatter.string(from: date as Date)
  1264. timeView.text = stringFormat
  1265. }
  1266. }
  1267. }
  1268. let messageView = UILabel()
  1269. content.addSubview(messageView)
  1270. messageView.translatesAutoresizingMaskIntoConstraints = false
  1271. NSLayoutConstraint.activate([
  1272. messageView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  1273. messageView.topAnchor.constraint(equalTo: titleView.bottomAnchor),
  1274. messageView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  1275. ])
  1276. messageView.textColor = .gray
  1277. if data.messageText.contains("■") {
  1278. data.messageText = data.messageText.components(separatedBy: "■")[0]
  1279. data.messageText = data.messageText.trimmingCharacters(in: .whitespacesAndNewlines)
  1280. }
  1281. let text = Utils.previewMessageText(chat: data)
  1282. let idMe = User.getMyPin() as String?
  1283. if let attributeText = text as? NSMutableAttributedString {
  1284. let stringMessage = NSMutableAttributedString(string: "")
  1285. if data.fpin == idMe {
  1286. if data.lock == "1" {
  1287. if data.messageScope == "4" {
  1288. stringMessage.append(NSAttributedString(string: "You".localized() + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12 + String.offset(), weight: .medium)]))
  1289. }
  1290. stringMessage.append(("🚫 _"+"You were deleted this message".localized()+"_").richText())
  1291. } else if data.messageScope != MessageScope.CALL && data.messageScope != MessageScope.MISSED_CALL {
  1292. let imageStatus = NSTextAttachment()
  1293. let status = getRealStatus(messageId: data.messageId)
  1294. if status == "0" {
  1295. imageStatus.image = UIImage(systemName: "xmark.circle")!.withTintColor(UIColor.red, renderingMode: .alwaysOriginal)
  1296. } else if status == "1" {
  1297. imageStatus.image = UIImage(systemName: "clock.arrow.circlepath")!.withTintColor(UIColor.lightGray, renderingMode: .alwaysOriginal)
  1298. } else if status == "2" {
  1299. imageStatus.image = UIImage(named: "checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  1300. } else if (status == "3") {
  1301. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.lightGray)
  1302. } else if (status == "8") {
  1303. imageStatus.image = UIImage(named: "message_status_ack", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withRenderingMode(.alwaysOriginal)
  1304. } else {
  1305. imageStatus.image = UIImage(named: "double-checklist", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!.withTintColor(UIColor.systemBlue)
  1306. }
  1307. imageStatus.bounds = CGRect(x: 0, y: -5, width: 15, height: 15)
  1308. let imageStatusString = NSAttributedString(attachment: imageStatus)
  1309. stringMessage.append(imageStatusString)
  1310. stringMessage.append(NSAttributedString(string: " "))
  1311. if data.messageScope == "4" {
  1312. stringMessage.append(NSAttributedString(string: "You".localized() + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12 + String.offset(), weight: .medium)]))
  1313. }
  1314. stringMessage.append(attributeText)
  1315. }
  1316. } else {
  1317. if data.messageScope == "4" {
  1318. var fullname = User.getData(pin: data.fpin, lPin: data.pin)!.fullName
  1319. let components = fullname.split(separator: " ")
  1320. if components.count >= 2 {
  1321. fullname = components.prefix(2).joined(separator: " ")
  1322. }
  1323. stringMessage.append(NSAttributedString(string: fullname + ": ", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12 + String.offset(), weight: .medium)]))
  1324. }
  1325. if data.lock == "1" {
  1326. stringMessage.append(("🚫 _"+"This message was deleted".localized()+"_").richText())
  1327. } else {
  1328. stringMessage.append(attributeText)
  1329. }
  1330. }
  1331. messageView.attributedText = stringMessage
  1332. }
  1333. messageView.numberOfLines = 2
  1334. if data.counter != "0" {
  1335. viewCounter.topAnchor.constraint(equalTo: timeView.bottomAnchor, constant: 5.0).isActive = true
  1336. viewCounter.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20).isActive = true
  1337. }
  1338. } else {
  1339. titleView.centerYAnchor.constraint(equalTo: content.centerYAnchor).isActive = true
  1340. titleView.text = data.groupName
  1341. let iconName = (data.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
  1342. let imageView = UIImageView(image: UIImage(systemName: iconName))
  1343. imageView.tintColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  1344. content.addSubview(imageView)
  1345. imageView.translatesAutoresizingMaskIntoConstraints = false
  1346. NSLayoutConstraint.activate([
  1347. imageView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20),
  1348. imageView.centerYAnchor.constraint(equalTo: content.centerYAnchor),
  1349. imageView.widthAnchor.constraint(equalToConstant: 20),
  1350. imageView.heightAnchor.constraint(equalToConstant: 20)
  1351. ])
  1352. if data.counter != "0" {
  1353. viewCounter.trailingAnchor.constraint(equalTo: imageView.leadingAnchor, constant: -5).isActive = true
  1354. viewCounter.centerYAnchor.constraint(equalTo: content.centerYAnchor).isActive = true
  1355. }
  1356. }
  1357. }
  1358. case 1:
  1359. if segment.numberOfSegments < 3 {
  1360. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
  1361. var content = cell.defaultContentConfiguration()
  1362. content.textProperties.font = UIFont.systemFont(ofSize: 14 + String.offset())
  1363. let group: Group
  1364. if isFilltering {
  1365. if indexPath.row == 0 {
  1366. group = fillteredData[indexPath.section] as! Group
  1367. } else {
  1368. if (fillteredData[indexPath.section] as! Group).childs.count > 0 {
  1369. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  1370. } else {
  1371. return cell
  1372. }
  1373. }
  1374. } else {
  1375. if indexPath.row == 0 {
  1376. if indexPath.section > (groups.count - 1) {
  1377. return cell
  1378. }
  1379. group = groups[indexPath.section]
  1380. } else {
  1381. group = groups[indexPath.section].childs[indexPath.row - 1]
  1382. }
  1383. }
  1384. if group.official == "1" && group.parent == "" {
  1385. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1386. }
  1387. else if group.isOpen == "1" && group.parent == "" {
  1388. if self.traitCollection.userInterfaceStyle == .dark {
  1389. content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1390. } else {
  1391. content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4)
  1392. }
  1393. } else if group.parent == "" {
  1394. if self.traitCollection.userInterfaceStyle == .dark {
  1395. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1396. } else {
  1397. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1398. }
  1399. } else {
  1400. content.text = group.name
  1401. }
  1402. if group.childs.count > 0 {
  1403. let iconName = (group.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
  1404. let imageView = UIImageView(image: UIImage(systemName: iconName))
  1405. imageView.tintColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  1406. cell.accessoryView = imageView
  1407. }
  1408. else {
  1409. cell.accessoryView = nil
  1410. cell.accessoryType = .none
  1411. }
  1412. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  1413. getImage(name: group.profile, placeholderImage: UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  1414. content.image = image
  1415. }
  1416. cell.contentConfiguration = content
  1417. if !group.level.isEmpty {
  1418. if group.level != "-1" && Int(group.level)! < 7 {
  1419. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * Int(group.level)!), bottom: 0.0, right: 0)
  1420. } else if Int(group.level)! > 6 {
  1421. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * (Int(group.level)! - 6)), bottom: 0.0, right: 0)
  1422. }
  1423. }
  1424. } else {
  1425. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  1426. let content = cell.contentView
  1427. if content.subviews.count > 0 {
  1428. content.subviews.forEach { $0.removeFromSuperview() }
  1429. }
  1430. let data: User
  1431. if isFilltering {
  1432. data = fillteredData[indexPath.row] as! User
  1433. } else {
  1434. if indexPath.row > contacts.count - 1 {
  1435. return cell
  1436. }
  1437. data = contacts[indexPath.row]
  1438. }
  1439. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40.0, height: 40.0))
  1440. content.addSubview(imageView)
  1441. imageView.translatesAutoresizingMaskIntoConstraints = false
  1442. NSLayoutConstraint.activate([
  1443. imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 20.0),
  1444. imageView.centerYAnchor.constraint(equalTo: content.centerYAnchor),
  1445. imageView.widthAnchor.constraint(equalToConstant: 40.0),
  1446. imageView.heightAnchor.constraint(equalToConstant: 40.0)
  1447. ])
  1448. if data.pin == "-997" {
  1449. imageView.circle()
  1450. if let urlGif = Bundle.resourceBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {//resourcesMediaBundle
  1451. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1452. if error == nil {
  1453. imageView.animationImages = image?.images
  1454. imageView.animationDuration = image?.duration ?? 0.0
  1455. imageView.animationRepeatCount = 0
  1456. imageView.startAnimating()
  1457. }
  1458. }
  1459. } else if let urlGif = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: "pb_gpt_bot", withExtension: "gif") {
  1460. imageView.sd_setImage(with: urlGif) { (image, error, cacheType, imageURL) in
  1461. if error == nil {
  1462. imageView.animationImages = image?.images
  1463. imageView.animationDuration = image?.duration ?? 0.0
  1464. imageView.animationRepeatCount = 0
  1465. imageView.startAnimating()
  1466. }
  1467. }
  1468. }
  1469. }
  1470. else {
  1471. getImage(name: data.thumb, placeholderImage: UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
  1472. imageView.image = image
  1473. })
  1474. }
  1475. let titleView = UILabel()
  1476. content.addSubview(titleView)
  1477. titleView.translatesAutoresizingMaskIntoConstraints = false
  1478. NSLayoutConstraint.activate([
  1479. titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 15.0),
  1480. titleView.centerYAnchor.constraint(equalTo: content.centerYAnchor)
  1481. ])
  1482. titleView.font = UIFont.systemFont(ofSize: 14 + String.offset())
  1483. if (User.isOfficial(official_account: data.official ?? "") || User.isOfficialRegular(official_account: data.official ?? "")) && data.pin != "-997" {
  1484. titleView.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.officialColor)
  1485. } else if User.isVerified(official_account: data.official ?? "") {
  1486. titleView.attributedText = self.set(image: UIImage(named: "ic_verified", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.verifiedColor)
  1487. }
  1488. else if User.isInternal(userType: data.userType ?? "") {
  1489. titleView.attributedText = self.set(image: UIImage(named: "ic_internal", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.internalColor)
  1490. } else if User.isCallCenter(userType: data.userType ?? "") {
  1491. titleView.attributedText = self.set(image: UIImage(named: "pb_call_center", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(data.fullName)", size: 15, y: -4, colorText: UIColor.ccColor)
  1492. } else {
  1493. titleView.text = data.fullName
  1494. }
  1495. }
  1496. case 2:
  1497. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
  1498. var content = cell.defaultContentConfiguration()
  1499. content.textProperties.font = UIFont.systemFont(ofSize: 14 + String.offset())
  1500. let group: Group
  1501. if isFilltering {
  1502. if indexPath.row == 0 {
  1503. group = fillteredData[indexPath.section] as! Group
  1504. } else {
  1505. if (fillteredData[indexPath.section] as! Group).childs.count > 0 {
  1506. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  1507. } else {
  1508. return cell
  1509. }
  1510. }
  1511. } else {
  1512. if indexPath.row == 0 {
  1513. if indexPath.section > (groups.count - 1) {
  1514. return cell
  1515. }
  1516. group = groups[indexPath.section]
  1517. } else {
  1518. group = groups[indexPath.section].childs[indexPath.row - 1]
  1519. }
  1520. }
  1521. if group.official == "1" && group.parent == "" {
  1522. content.attributedText = self.set(image: UIImage(named: "ic_official_flag", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)!, with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1523. }
  1524. else if group.isOpen == "1" && group.parent == "" {
  1525. if self.traitCollection.userInterfaceStyle == .dark {
  1526. content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1527. } else {
  1528. content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1529. }
  1530. } else if group.parent == "" {
  1531. if self.traitCollection.userInterfaceStyle == .dark {
  1532. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1533. } else {
  1534. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4, colorText: self.traitCollection.userInterfaceStyle == .dark ? .white : .black)
  1535. }
  1536. } else {
  1537. content.text = group.name
  1538. }
  1539. if group.childs.count > 0 {
  1540. let iconName = (group.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
  1541. let imageView = UIImageView(image: UIImage(systemName: iconName))
  1542. imageView.tintColor = self.traitCollection.userInterfaceStyle == .dark ? .white : .black
  1543. cell.accessoryView = imageView
  1544. }
  1545. else {
  1546. cell.accessoryView = nil
  1547. cell.accessoryType = .none
  1548. }
  1549. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  1550. getImage(name: group.profile, placeholderImage: UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath) { result, isDownloaded, image in
  1551. content.image = image
  1552. }
  1553. cell.contentConfiguration = content
  1554. if !group.level.isEmpty {
  1555. if group.level != "-1" && Int(group.level)! < 7 {
  1556. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * Int(group.level)!), bottom: 0.0, right: 0)
  1557. } else if Int(group.level)! > 6 {
  1558. cell.contentView.layoutMargins = .init(top: 0.0, left: CGFloat(25 * (Int(group.level)! - 6)), bottom: 0.0, right: 0)
  1559. }
  1560. }
  1561. default:
  1562. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  1563. var content = cell.defaultContentConfiguration()
  1564. content.text = ""
  1565. cell.contentConfiguration = content
  1566. }
  1567. cell.backgroundColor = .clear
  1568. cell.separatorInset = UIEdgeInsets(top: 0, left: 60.0, bottom: 0, right: 0)
  1569. return cell
  1570. }
  1571. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  1572. let fontSize = Int(SecureUserDefaults.shared.value(forKey: "font_size") ?? "0")
  1573. if fontSize == 4 {
  1574. return 85.0
  1575. } else if fontSize == 6 {
  1576. return 95.0
  1577. }
  1578. return 75.0
  1579. }
  1580. private func getRealStatus(messageId: String) -> String {
  1581. var status = "1"
  1582. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  1583. do {
  1584. if let cursorStatus = Database.shared.getRecords(fmdb: fmdb, query: "SELECT status, f_pin FROM MESSAGE_STATUS WHERE message_id='\(messageId)'") {
  1585. var listStatus: [Int] = []
  1586. while cursorStatus.next() {
  1587. listStatus.append(Int(cursorStatus.string(forColumnIndex: 0)!)!)
  1588. }
  1589. cursorStatus.close()
  1590. status = "\(listStatus.min() ?? 2)"
  1591. }
  1592. } catch {
  1593. rollback.pointee = true
  1594. print("Access database error: \(error.localizedDescription)")
  1595. }
  1596. })
  1597. return status
  1598. }
  1599. }
  1600. extension ContactChatViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  1601. func updateSearchResults(for searchController: UISearchController) {
  1602. filterContentForSearchText(searchController.searchBar.text!.trimmingCharacters(in: .whitespacesAndNewlines))
  1603. }
  1604. func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat, colorText: UIColor = UIColor.black) -> NSAttributedString {
  1605. let attachment = NSTextAttachment()
  1606. attachment.image = image
  1607. attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
  1608. let attachmentStr = NSAttributedString(attachment: attachment)
  1609. let mutableAttributedString = NSMutableAttributedString()
  1610. mutableAttributedString.append(attachmentStr)
  1611. let attributedStringColor = [NSAttributedString.Key.foregroundColor : colorText]
  1612. let textString = NSAttributedString(string: text, attributes: attributedStringColor)
  1613. mutableAttributedString.append(textString)
  1614. return mutableAttributedString
  1615. }
  1616. func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  1617. searchBar.showsCancelButton = true
  1618. let cBtn = searchBar.value(forKey: "cancelButton") as! UIButton
  1619. cBtn.setTitle("Cancel".localized(), for: .normal)
  1620. }
  1621. func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  1622. searchBar.showsCancelButton = false
  1623. }
  1624. }