SecondTabViewController.swift 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. //
  2. // SecondTabViewController.swift
  3. // AppBuilder
  4. //
  5. // Created by Kevin Maulana on 30/03/22.
  6. //
  7. import UIKit
  8. import FMDB
  9. import NexilisLite
  10. import Speech
  11. class SecondTabViewController: UIViewController, UIScrollViewDelegate {
  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 cancelSearchButton = UIBarButtonItem()
  22. var menuItem = UIBarButtonItem()
  23. var voiceItem = UIBarButtonItem()
  24. var childrenMenu = [UIAction]()
  25. lazy var searchController: UISearchController = {
  26. var searchController = UISearchController(searchResultsController: nil)
  27. searchController.delegate = self
  28. searchController.searchResultsUpdater = self
  29. // searchController.searchBar.showsBookmarkButton = true
  30. searchController.searchBar.autocapitalizationType = .none
  31. searchController.searchBar.delegate = self
  32. searchController.searchBar.placeholder = "Search chats & messages".localized()
  33. searchController.searchBar.barTintColor = .secondaryColor
  34. searchController.searchBar.searchTextField.backgroundColor = .secondaryColor
  35. searchController.obscuresBackgroundDuringPresentation = false
  36. return searchController
  37. }()
  38. lazy var segment: UISegmentedControl = {
  39. print("Chat".localized())
  40. var segment = UISegmentedControl(items: ["Chat".localized(), "Group".localized()])
  41. segment.sizeToFit()
  42. segment.selectedSegmentIndex = 0
  43. segment.addTarget(self, action: #selector(segmentChanged(sender:)), for: .valueChanged)
  44. return segment
  45. }()
  46. var fillteredData: [Any] = []
  47. var isSearchBarEmpty: Bool {
  48. return searchController.searchBar.text?.isEmpty ?? true
  49. }
  50. var isFilltering: Bool {
  51. return searchController.isActive && !isSearchBarEmpty
  52. }
  53. @IBOutlet var tableView: UITableView!
  54. let speechRecognizer = SFSpeechRecognizer()
  55. var recognitionRequest : SFSpeechAudioBufferRecognitionRequest?
  56. var recognitionTask : SFSpeechRecognitionTask?
  57. let audioEngine = AVAudioEngine()
  58. func filterContentForSearchText(_ searchText: String) {
  59. switch segment.selectedSegmentIndex {
  60. case 1:
  61. fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
  62. default:
  63. fillteredData = self.chats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) }
  64. }
  65. tableView.reloadData()
  66. }
  67. override func viewDidLoad() {
  68. super.viewDidLoad()
  69. SFSpeechRecognizer.requestAuthorization({ authStatus in
  70. var isButtonEnabled = false
  71. OperationQueue.main.addOperation {
  72. switch authStatus {
  73. case .authorized:
  74. isButtonEnabled = true
  75. print("User allowed access to speech recognition")
  76. case .denied:
  77. isButtonEnabled = false
  78. print("User denied access to speech recognition")
  79. case .restricted:
  80. isButtonEnabled = false
  81. print("Speech recognition restricted on this device")
  82. case .notDetermined:
  83. isButtonEnabled = false
  84. print("Speech recognition not yet authorized")
  85. @unknown default:
  86. print("Speech recognition not yet authorized")
  87. }
  88. }
  89. })
  90. let me = UserDefaults.standard.string(forKey: "me")!
  91. Database.shared.database?.inTransaction({ fmdb, rollback in
  92. 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() {
  93. isAdmin = cursor.string(forColumnIndex: 3) == "23" || cursor.string(forColumnIndex: 3) == "24"
  94. cursor.close()
  95. }
  96. })
  97. // navigationController?.title = "Start Conversation"
  98. // navigationController?.navigationBar.prefersLargeTitles = true
  99. cancelSearchButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancel(sender:)))
  100. var childrenMenu : [UIAction] = [
  101. // UIAction(title: "Create Group", image: UIImage(systemName: "person.and.person"), handler: {[weak self](_) in
  102. // let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "createGroupNav") as! UINavigationController
  103. // let vc = controller.topViewController as! GroupCreateViewController
  104. // vc.isDismiss = { id in
  105. // let controller = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "groupDetailView") as! GroupDetailViewController
  106. // controller.data = id
  107. // self?.navigationController?.show(controller, sender: nil)
  108. // }
  109. // self?.navigationController?.present(controller, animated: true, completion: nil)
  110. // }),
  111. // UIAction(title: "Add Friends", image: UIImage(systemName: "person.badge.plus"), handler: {[weak self](_) in
  112. // let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "addFriendNav") as! UINavigationController
  113. // if let vc = controller.viewControllers.first as? AddFriendTableViewController {
  114. // vc.isDismiss = {
  115. // self?.getContacts {
  116. // DispatchQueue.main.async {
  117. // self?.tableView.reloadData()
  118. // }
  119. // }
  120. // }
  121. // }
  122. // self?.navigationController?.present(controller, animated: true, completion: nil)
  123. // }),
  124. // UIAction(title: "Configure Email", image: UIImage(systemName: "mail"), handler: {[weak self](_) in
  125. //
  126. // }),
  127. // UIAction(title: "Favorite Messages", image: UIImage(systemName: "star"), handler: {[weak self](_) in
  128. // let editorStaredVC = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "staredVC") as! EditorStarMessages
  129. // self?.navigationController?.show(editorStaredVC, sender: nil)
  130. // }),
  131. ]
  132. //debug only
  133. // isAdmin = true
  134. if(isAdmin){
  135. childrenMenu.append(UIAction(title: "Broadcast Message", image: UIImage(systemName: "envelope.open"), handler: {[weak self](_) in
  136. let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "broadcastNav")
  137. self?.navigationController?.present(controller, animated: true, completion: nil)
  138. }))
  139. }
  140. // let menu = UIMenu(title: "", children: childrenMenu)
  141. menuItem = UIBarButtonItem(image: UIImage(systemName: "square.and.pencil"), style: .plain, target: self, action: #selector(startConversation))
  142. voiceItem = UIBarButtonItem(image: UIImage(systemName: "mic.fill"), style: .plain, target: self, action: #selector(recordAudio))
  143. // tabBarController?.navigationItem.leftBarButtonItem = cancelSearchButton
  144. tabBarController?.navigationItem.leftBarButtonItem = voiceItem
  145. tabBarController?.navigationItem.rightBarButtonItem = menuItem
  146. tabBarController?.navigationItem.searchController = searchController
  147. // navigationItem.searchController = searchController
  148. // navigationItem.hidesSearchBarWhenScrolling = true
  149. definesPresentationContext = true
  150. NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: "onReceiveChat"), object: nil)
  151. NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onMember"), object: nil)
  152. tableView.tableHeaderView = segment
  153. pullBuddy()
  154. let cpaasMode = PrefsUtil.getCpaasMode()
  155. let isBurger = cpaasMode == PrefsUtil.CPAAS_MODE_BURGER
  156. navigationController?.setNavigationBarHidden(!isBurger, animated: false)
  157. }
  158. @objc func startConversation(){
  159. let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactChatNav") as! UINavigationController
  160. navigationController.modalPresentationStyle = .fullScreen
  161. // navigationController.navigationBar.tintColor = .white
  162. navigationController.navigationBar.barTintColor = .mainColor
  163. navigationController.navigationBar.isTranslucent = false
  164. // let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
  165. // navigationController.navigationBar.titleTextAttributes = textAttributes
  166. navigationController.view.backgroundColor = .mainColor
  167. self.navigationController?.present(navigationController, animated: true, completion: nil)
  168. }
  169. @objc func recordAudio(){
  170. if audioEngine.isRunning {
  171. self.audioEngine.stop()
  172. self.recognitionRequest?.endAudio()
  173. voiceItem.image = UIImage(systemName: "mic.fill")
  174. searchController.searchBar.isUserInteractionEnabled = true
  175. searchController.automaticallyShowsCancelButton = true
  176. searchController.hidesNavigationBarDuringPresentation = true
  177. } else {
  178. print("start recording")
  179. self.startRecording()
  180. voiceItem.image = UIImage(systemName: "mic")
  181. searchController.searchBar.isUserInteractionEnabled = false
  182. }
  183. }
  184. func startRecording() {
  185. // Clear all previous session data and cancel task
  186. if recognitionTask != nil {
  187. recognitionTask?.cancel()
  188. recognitionTask = nil
  189. }
  190. // Create instance of audio session to record voice
  191. let audioSession = AVAudioSession.sharedInstance()
  192. do {
  193. try audioSession.setCategory(AVAudioSession.Category.record, mode: AVAudioSession.Mode.measurement, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
  194. try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
  195. } catch {
  196. print("audioSession properties weren't set because of an error.")
  197. }
  198. self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
  199. let inputNode = audioEngine.inputNode
  200. guard let recognitionRequest = recognitionRequest else {
  201. fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
  202. }
  203. recognitionRequest.shouldReportPartialResults = true
  204. self.recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
  205. var isFinal = false
  206. if result != nil {
  207. self.searchController.searchBar.searchTextField.text = result?.bestTranscription.formattedString
  208. self.updateSearchResults(for: self.searchController)
  209. isFinal = (result?.isFinal)!
  210. }
  211. if error != nil || isFinal {
  212. self.audioEngine.stop()
  213. inputNode.removeTap(onBus: 0)
  214. self.recognitionRequest = nil
  215. self.recognitionTask = nil
  216. // self.btnStart.isEnabled = true
  217. }
  218. })
  219. let recordingFormat = inputNode.outputFormat(forBus: 0)
  220. inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
  221. self.recognitionRequest?.append(buffer)
  222. }
  223. self.audioEngine.prepare()
  224. do {
  225. try self.audioEngine.start()
  226. } catch {
  227. print("audioEngine couldn't start because of an error.")
  228. }
  229. //
  230. // self.lblText.text = "Say something, I'm listening!"
  231. }
  232. override func viewWillAppear(_ animated: Bool) {
  233. // tabBarController?.navigationItem.leftBarButtonItem = cancelSearchButton
  234. self.navigationController?.navigationBar.topItem?.title = Bundle.main.displayName
  235. let cpaasMode = PrefsUtil.getCpaasMode()
  236. let isBurger = cpaasMode == PrefsUtil.CPAAS_MODE_BURGER
  237. navigationController?.navigationBar.backgroundColor = .clear
  238. navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
  239. navigationController?.navigationBar.shadowImage = UIImage()
  240. navigationController?.navigationBar.isTranslucent = true
  241. navigationController?.setNavigationBarHidden(false, animated: false)
  242. tabBarController?.navigationItem.leftBarButtonItem = voiceItem
  243. tabBarController?.navigationItem.searchController = searchController
  244. tabBarController?.navigationItem.rightBarButtonItem = menuItem
  245. let randomInt = Int.random(in: 1..<2)
  246. backgroundImage.image = UIImage(named: "pb_lbackground_\(randomInt)")
  247. getData()
  248. }
  249. override func viewWillDisappear(_ animated: Bool) {
  250. tabBarController?.navigationItem.leftBarButtonItem = nil
  251. tabBarController?.navigationItem.searchController = nil
  252. tabBarController?.navigationItem.rightBarButtonItem = nil
  253. }
  254. @objc func onReload(notification: NSNotification) {
  255. let data:[AnyHashable : Any] = notification.userInfo!
  256. if data["member"] as! String == UserDefaults.standard.string(forKey: "me")! {
  257. DispatchQueue.main.async {
  258. self.getData()
  259. }
  260. }
  261. }
  262. @objc func onReceiveMessage(notification: NSNotification) {
  263. let data:[AnyHashable : Any] = notification.userInfo!
  264. guard let dataMessage = data["message"] as? TMessage else {
  265. return
  266. }
  267. let isUser = User.getData(pin: dataMessage.getBody(key: CoreMessage_TMessageKey.L_PIN)) != nil
  268. let chatId = dataMessage.getBody(key: CoreMessage_TMessageKey.CHAT_ID, default_value: "").isEmpty ? dataMessage.getBody(key: CoreMessage_TMessageKey.L_PIN) : dataMessage.getBody(key: CoreMessage_TMessageKey.CHAT_ID, default_value: "")
  269. let pin = isUser ? dataMessage.getBody(key: CoreMessage_TMessageKey.F_PIN) : chatId
  270. let messageId = dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)
  271. if let index = chats.firstIndex(of: Chat(pin: pin)) {
  272. guard let chat = Chat.getData(messageId: messageId).first else {
  273. return
  274. }
  275. DispatchQueue.main.async {
  276. if self.segment.selectedSegmentIndex == 0 {
  277. self.tableView.beginUpdates()
  278. self.chats.remove(at: index)
  279. self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .none)
  280. }
  281. self.chats.insert(chat, at: 0)
  282. if self.segment.selectedSegmentIndex == 0 {
  283. self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
  284. self.tableView.endUpdates()
  285. }
  286. }
  287. } else {
  288. guard let chat = Chat.getData(messageId: messageId).first else {
  289. return
  290. }
  291. DispatchQueue.main.async {
  292. if self.segment.selectedSegmentIndex == 0 {
  293. self.tableView.beginUpdates()
  294. }
  295. self.chats.insert(chat, at: 0)
  296. if self.segment.selectedSegmentIndex == 0 {
  297. self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
  298. self.tableView.endUpdates()
  299. }
  300. }
  301. }
  302. }
  303. @objc func add(sender: Any) {
  304. }
  305. @objc func cancel(sender: Any) {
  306. navigationController?.dismiss(animated: true, completion: nil)
  307. }
  308. @objc func segmentChanged(sender: Any) {
  309. filterContentForSearchText(searchController.searchBar.text!)
  310. }
  311. // MARK: - Data source
  312. func getData() {
  313. getChats {
  314. self.getContacts {
  315. self.getGroups { g1 in
  316. self.getOpenGroups(listGroups: g1, completion: { g in
  317. self.groups.removeAll()
  318. self.groups.append(contentsOf: g1)
  319. for og in g {
  320. if self.groups.first(where: { $0.id == og.id }) == nil {
  321. self.groups.append(og)
  322. }
  323. }
  324. DispatchQueue.main.async {
  325. self.tableView.reloadData()
  326. }
  327. })
  328. }
  329. }
  330. }
  331. }
  332. func getChats(completion: @escaping ()->()) {
  333. DispatchQueue.global().async {
  334. self.chats.removeAll()
  335. self.chats.append(contentsOf: Chat.getData())
  336. completion()
  337. }
  338. }
  339. private func getContacts(completion: @escaping ()->()) {
  340. DispatchQueue.global().async {
  341. self.contacts.removeAll()
  342. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  343. do {
  344. 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") {
  345. while cursorData.next() {
  346. let user = User(pin: cursorData.string(forColumnIndex: 0) ?? "",
  347. firstName: cursorData.string(forColumnIndex: 1) ?? "",
  348. lastName: cursorData.string(forColumnIndex: 2) ?? "",
  349. thumb: cursorData.string(forColumnIndex: 3) ?? "",
  350. userType: cursorData.string(forColumnIndex: 5) ?? "")
  351. if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
  352. continue
  353. }
  354. user.official = cursorData.string(forColumnIndex: 4) ?? ""
  355. self.contacts.append(user)
  356. }
  357. cursorData.close()
  358. }
  359. } catch {
  360. rollback.pointee = true
  361. print(error)
  362. }
  363. completion()
  364. })
  365. }
  366. }
  367. private func getGroupRecursive(fmdb: FMDatabase, id: String = "", parent: String = "") -> [Group] {
  368. var data: [Group] = []
  369. 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 from GROUPZ g where "
  370. if id.isEmpty {
  371. query += "g.parent = '\(parent)'"
  372. } else {
  373. query += "g.group_id = '\(id)'"
  374. }
  375. if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query) {
  376. while cursor.next() {
  377. let group = Group(
  378. id: cursor.string(forColumnIndex: 0) ?? "",
  379. name: cursor.string(forColumnIndex: 1) ?? "",
  380. profile: cursor.string(forColumnIndex: 2) ?? "",
  381. quote: cursor.string(forColumnIndex: 3) ?? "",
  382. by: cursor.string(forColumnIndex: 4) ?? "",
  383. date: cursor.string(forColumnIndex: 5) ?? "",
  384. parent: cursor.string(forColumnIndex: 6) ?? "",
  385. chatId: "",
  386. groupType: cursor.string(forColumnIndex: 7) ?? "",
  387. isOpen: cursor.string(forColumnIndex: 8) ?? "",
  388. official: cursor.string(forColumnIndex: 9) ?? "",
  389. isEducation: cursor.string(forColumnIndex: 10) ?? "")
  390. if group.chatId.isEmpty {
  391. 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)
  392. group.childs.append(lounge)
  393. }
  394. if let topicCursor = Database.shared.getRecords(fmdb: fmdb, query: "select chat_id, title, thumb from DISCUSSION_FORUM where group_id = '\(group.id)'") {
  395. while topicCursor.next() {
  396. let topic = Group(id: group.id,
  397. name: topicCursor.string(forColumnIndex: 1) ?? "",
  398. profile: topicCursor.string(forColumnIndex: 2) ?? "",
  399. quote: group.quote,
  400. by: group.by,
  401. date: group.date,
  402. parent: group.id,
  403. chatId: topicCursor.string(forColumnIndex: 0) ?? "",
  404. groupType: group.groupType,
  405. isOpen: group.isOpen,
  406. official: group.official,
  407. isEducation: group.isEducation)
  408. group.childs.append(topic)
  409. }
  410. topicCursor.close()
  411. }
  412. if !group.id.isEmpty {
  413. if group.official == "1" {
  414. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  415. if let cursorUser = Database.shared.getRecords(fmdb: fmdb, query: "SELECT user_type FROM BUDDY where f_pin='\(idMe!)'"), cursorUser.next() {
  416. if cursorUser.string(forColumnIndex: 0) == "23" || cursorUser.string(forColumnIndex: 0) == "24" {
  417. group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  418. }
  419. cursorUser.close()
  420. }
  421. } else if group.official != "1"{
  422. group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
  423. }
  424. }
  425. data.append(group)
  426. }
  427. cursor.close()
  428. }
  429. return data
  430. }
  431. private func getOpenGroups(listGroups: [Group], completion: @escaping ([Group]) -> ()) {
  432. if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getOpenGroups(p_account: "1,2,3,5,6,7", offset: "0", search: "")) {
  433. var dataGroups: [Group] = []
  434. if (response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "00") {
  435. let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
  436. if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
  437. for dataJson in json {
  438. let group = Group(
  439. id: dataJson[CoreMessage_TMessageKey.GROUP_ID] as? String ?? "",
  440. name: dataJson[CoreMessage_TMessageKey.GROUP_NAME] as? String ?? "",
  441. profile: dataJson[CoreMessage_TMessageKey.THUMB_ID] as? String ?? "",
  442. quote: dataJson[CoreMessage_TMessageKey.QUOTE] as? String ?? "",
  443. by: dataJson[CoreMessage_TMessageKey.BLOCK] as? String ?? "",
  444. date: "",
  445. parent: "",
  446. chatId: "",
  447. groupType: "NOTJOINED",
  448. isOpen: dataJson[CoreMessage_TMessageKey.IS_OPEN] as? String ?? "",
  449. official: "0",
  450. isEducation: "")
  451. dataGroups.append(group)
  452. }
  453. }
  454. } else {
  455. DispatchQueue.main.async {
  456. self.groups.removeAll()
  457. self.groups.append(contentsOf: listGroups)
  458. self.tableView.reloadData()
  459. }
  460. }
  461. completion(dataGroups)
  462. }
  463. }
  464. private func getGroups(id: String = "", parent: String = "", completion: @escaping ([Group]) -> ()) {
  465. DispatchQueue.global().async {
  466. Database.shared.database?.inTransaction({ fmdb, rollback in
  467. completion(self.getGroupRecursive(fmdb: fmdb, id: id, parent: parent))
  468. })
  469. }
  470. }
  471. private func pullBuddy() {
  472. if let me = UserDefaults.standard.string(forKey: "me") {
  473. DispatchQueue.global().async {
  474. let _ = Nexilis.write(message: CoreMessage_TMessageBank.getBatchBuddiesInfos(p_f_pin: me, last_update: 0))
  475. }
  476. }
  477. }
  478. private func joinOpenGroup(groupId: String, flagMember: String = "0", completion: @escaping (Bool) -> ()) {
  479. DispatchQueue.global().async {
  480. var result: Bool = false
  481. let idMe = UserDefaults.standard.string(forKey: "me") as String?
  482. if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddGroupMember(p_group_id: groupId, p_member_pin: idMe!, p_position: "0")), response.isOk() {
  483. result = true
  484. }
  485. completion(result)
  486. }
  487. }
  488. @IBOutlet weak var backgroundImage: UIImageView!
  489. /*
  490. // MARK: - Navigation
  491. // In a storyboard-based application, you will often want to do a little preparation before navigation
  492. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  493. // Get the new view controller using segue.destination.
  494. // Pass the selected object to the new view controller.
  495. }
  496. */
  497. }
  498. // MARK: - Table view data source
  499. extension SecondTabViewController: UITableViewDelegate, UITableViewDataSource {
  500. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  501. switch segment.selectedSegmentIndex {
  502. case 0:
  503. let data: Chat
  504. if isFilltering {
  505. data = fillteredData[indexPath.row] as! Chat
  506. } else {
  507. data = chats[indexPath.row]
  508. }
  509. if let chooser = isChooser {
  510. if data.pin == "-999"{
  511. return
  512. }
  513. chooser(data.messageScope, data.pin)
  514. dismiss(animated: true, completion: nil)
  515. return
  516. }
  517. let user = User.getData(pin: data.pin)
  518. if user != nil || data.pin == "-999" {
  519. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  520. editorPersonalVC.hidesBottomBarWhenPushed = true
  521. editorPersonalVC.unique_l_pin = data.pin
  522. navigationController?.show(editorPersonalVC, sender: nil)
  523. } else {
  524. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  525. editorGroupVC.hidesBottomBarWhenPushed = true
  526. editorGroupVC.unique_l_pin = data.pin
  527. navigationController?.show(editorGroupVC, sender: nil)
  528. }
  529. case 1:
  530. let group: Group
  531. if isFilltering {
  532. if indexPath.row == 0 {
  533. group = fillteredData[indexPath.section] as! Group
  534. } else {
  535. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  536. }
  537. } else {
  538. if indexPath.row == 0 {
  539. group = groups[indexPath.section]
  540. } else {
  541. group = groups[indexPath.section].childs[indexPath.row - 1]
  542. }
  543. }
  544. group.isSelected = !group.isSelected
  545. if group.groupType == "NOTJOINED" {
  546. let alert = UIAlertController(title: "Do you want to join this group?".localized(), message: "Groups : \(group.name)\nMembers: \(group.by)".localized(), preferredStyle: .alert)
  547. alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
  548. alert.addAction(UIAlertAction(title: "Join".localized(), style: .default, handler: {(_) in
  549. self.joinOpenGroup(groupId: group.id, completion: { result in
  550. if result {
  551. DispatchQueue.main.async {
  552. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  553. editorGroupVC.hidesBottomBarWhenPushed = true
  554. editorGroupVC.unique_l_pin = group.id
  555. self.navigationController?.show(editorGroupVC, sender: nil)
  556. }
  557. }
  558. })
  559. }))
  560. self.present(alert, animated: true, completion: nil)
  561. return
  562. }
  563. if group.childs.count == 0 {
  564. let groupId = group.chatId.isEmpty ? group.id : group.chatId
  565. if let chooser = isChooser {
  566. chooser("4", groupId)
  567. dismiss(animated: true, completion: nil)
  568. return
  569. }
  570. let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
  571. editorGroupVC.hidesBottomBarWhenPushed = true
  572. editorGroupVC.unique_l_pin = groupId
  573. navigationController?.show(editorGroupVC, sender: nil)
  574. } else {
  575. if indexPath.row == 0 {
  576. tableView.reloadData()
  577. } else {
  578. getGroups(id: group.id) { g in
  579. DispatchQueue.main.async {
  580. if self.isFilltering {
  581. self.fillteredData.remove(at: indexPath.section)
  582. self.fillteredData.insert(contentsOf: g, at: indexPath.section)
  583. } else {
  584. self.groups.remove(at: indexPath.section)
  585. self.groups.insert(contentsOf: g, at: indexPath.section)
  586. }
  587. tableView.reloadData()
  588. }
  589. }
  590. }
  591. }
  592. default:
  593. let data = contacts[indexPath.row]
  594. let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
  595. editorPersonalVC.hidesBottomBarWhenPushed = true
  596. editorPersonalVC.unique_l_pin = data.pin
  597. navigationController?.show(editorPersonalVC, sender: nil)
  598. }
  599. }
  600. func numberOfSections(in tableView: UITableView) -> Int {
  601. if isFilltering {
  602. if segment.selectedSegmentIndex == 1 {
  603. return fillteredData.count
  604. }
  605. return 1
  606. } else {
  607. if segment.selectedSegmentIndex == 1 {
  608. return groups.count
  609. }
  610. return 1
  611. }
  612. }
  613. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  614. var value = 0
  615. if isFilltering {
  616. if segment.selectedSegmentIndex == 1, let groups = fillteredData as? [Group] {
  617. let group = groups[section]
  618. if group.isSelected {
  619. value = group.childs.count + 1
  620. } else {
  621. value = 1
  622. }
  623. return value
  624. }
  625. return fillteredData.count
  626. }
  627. switch segment.selectedSegmentIndex {
  628. case 0:
  629. value = chats.count
  630. case 1:
  631. let group = groups[section]
  632. if group.isSelected {
  633. value = group.childs.count + 1
  634. } else {
  635. value = 1
  636. }
  637. default:
  638. value = chats.count
  639. }
  640. return value
  641. }
  642. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  643. var cell: UITableViewCell!
  644. switch segment.selectedSegmentIndex {
  645. case 0:
  646. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierChat", for: indexPath)
  647. cell.separatorInset.left = 60.0
  648. let content = cell.contentView
  649. if content.subviews.count > 0 {
  650. content.subviews.forEach { $0.removeFromSuperview() }
  651. }
  652. let data: Chat
  653. if isFilltering {
  654. data = fillteredData[indexPath.row] as! Chat
  655. } else {
  656. data = chats[indexPath.row]
  657. }
  658. let imageView = UIImageView()
  659. content.addSubview(imageView)
  660. imageView.translatesAutoresizingMaskIntoConstraints = false
  661. NSLayoutConstraint.activate([
  662. imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 10.0),
  663. imageView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  664. imageView.bottomAnchor.constraint(equalTo: content.bottomAnchor, constant: -20.0),
  665. imageView.widthAnchor.constraint(equalToConstant: 40.0),
  666. imageView.heightAnchor.constraint(equalToConstant: 40.0)
  667. ])
  668. if data.profile.isEmpty && data.pin != "-999" {
  669. let user = User.getData(pin: data.pin)
  670. if user != nil {
  671. imageView.image = UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  672. } else {
  673. imageView.image = UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
  674. }
  675. } else {
  676. getImage(name: data.profile, placeholderImage: UIImage(named: data.pin == "-999" ? "pb_ball" : "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, completion: { result, isDownloaded, image in
  677. imageView.image = image
  678. if !result {
  679. imageView.tintColor = .mainColor
  680. }
  681. if isDownloaded {
  682. tableView.reloadRows(at: [indexPath], with: .none)
  683. }
  684. })
  685. }
  686. let titleView = UILabel()
  687. content.addSubview(titleView)
  688. titleView.translatesAutoresizingMaskIntoConstraints = false
  689. NSLayoutConstraint.activate([
  690. titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  691. titleView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
  692. titleView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  693. ])
  694. titleView.text = data.name
  695. titleView.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  696. let messageView = UILabel()
  697. content.addSubview(messageView)
  698. messageView.translatesAutoresizingMaskIntoConstraints = false
  699. NSLayoutConstraint.activate([
  700. messageView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
  701. messageView.topAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 5.0),
  702. messageView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
  703. ])
  704. messageView.textColor = .gray
  705. let text = Utils.previewMessageText(chat: data)
  706. if let attributeText = text as? NSAttributedString {
  707. messageView.attributedText = attributeText
  708. } else if let stringText = text as? String {
  709. messageView.text = stringText
  710. }
  711. messageView.font = UIFont.systemFont(ofSize: 12)
  712. messageView.numberOfLines = 2
  713. if data.counter != "0" {
  714. let viewCounter = UIView()
  715. content.addSubview(viewCounter)
  716. viewCounter.translatesAutoresizingMaskIntoConstraints = false
  717. NSLayoutConstraint.activate([
  718. viewCounter.centerYAnchor.constraint(equalTo: content.centerYAnchor),
  719. viewCounter.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20),
  720. viewCounter.widthAnchor.constraint(greaterThanOrEqualToConstant: 20),
  721. viewCounter.heightAnchor.constraint(equalToConstant: 20)
  722. ])
  723. viewCounter.backgroundColor = .systemRed
  724. viewCounter.layer.cornerRadius = 10
  725. viewCounter.clipsToBounds = true
  726. viewCounter.layer.borderWidth = 0.5
  727. viewCounter.layer.borderColor = UIColor.secondaryColor.cgColor
  728. let labelCounter = UILabel()
  729. viewCounter.addSubview(labelCounter)
  730. labelCounter.translatesAutoresizingMaskIntoConstraints = false
  731. NSLayoutConstraint.activate([
  732. labelCounter.centerYAnchor.constraint(equalTo: viewCounter.centerYAnchor),
  733. labelCounter.leadingAnchor.constraint(equalTo: viewCounter.leadingAnchor, constant: 2),
  734. labelCounter.trailingAnchor.constraint(equalTo: viewCounter.trailingAnchor, constant: -2),
  735. ])
  736. labelCounter.font = UIFont.systemFont(ofSize: 11)
  737. if Int(data.counter)! > 99 {
  738. labelCounter.text = "99+"
  739. } else {
  740. labelCounter.text = data.counter
  741. }
  742. labelCounter.textColor = .secondaryColor
  743. labelCounter.textAlignment = .center
  744. }
  745. case 1:
  746. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
  747. var content = cell.defaultContentConfiguration()
  748. content.textProperties.font = UIFont.systemFont(ofSize: 14)
  749. let group: Group
  750. if isFilltering {
  751. if indexPath.row == 0 {
  752. group = fillteredData[indexPath.section] as! Group
  753. } else {
  754. group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
  755. }
  756. } else {
  757. if indexPath.row == 0 {
  758. group = groups[indexPath.section]
  759. } else {
  760. group = groups[indexPath.section].childs[indexPath.row - 1]
  761. }
  762. }
  763. if group.official == "1" && group.parent == "" {
  764. 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)
  765. }
  766. else if group.isOpen == "1" && group.parent == "" {
  767. if self.traitCollection.userInterfaceStyle == .dark {
  768. content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  769. } else {
  770. content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4)
  771. }
  772. } else if group.parent == "" {
  773. if self.traitCollection.userInterfaceStyle == .dark {
  774. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
  775. } else {
  776. content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4)
  777. }
  778. } else {
  779. content.text = group.name
  780. }
  781. content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
  782. getImage(name: group.profile, placeholderImage: UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true) { result, isDownloaded, image in
  783. content.image = image
  784. tableView.reloadRows(at: [indexPath], with: .none)
  785. }
  786. cell.contentConfiguration = content
  787. default:
  788. cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
  789. var content = cell.defaultContentConfiguration()
  790. content.text = ""
  791. cell.contentConfiguration = content
  792. }
  793. return cell
  794. }
  795. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  796. return 70.0
  797. }
  798. }
  799. extension SecondTabViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
  800. func updateSearchResults(for searchController: UISearchController) {
  801. filterContentForSearchText(searchController.searchBar.text!)
  802. }
  803. func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
  804. recordAudio()
  805. }
  806. func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat) -> NSAttributedString {
  807. let attachment = NSTextAttachment()
  808. attachment.image = image
  809. attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
  810. let attachmentStr = NSAttributedString(attachment: attachment)
  811. let mutableAttributedString = NSMutableAttributedString()
  812. mutableAttributedString.append(attachmentStr)
  813. let textString = NSAttributedString(string: text)
  814. mutableAttributedString.append(textString)
  815. return mutableAttributedString
  816. }
  817. }