123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967 |
- //
- // SecondTabViewController.swift
- // AppBuilder
- //
- // Created by Kevin Maulana on 30/03/22.
- //
- import UIKit
- import FMDB
- import NexilisLite
- import Speech
- class SecondTabViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
-
- var isChooser: ((String, String) -> ())?
-
- var isAdmin: Bool = false
-
- var chats: [Chat] = []
-
- var contacts: [User] = []
-
- var groups: [Group] = []
-
- var cancelSearchButton = UIBarButtonItem()
- var menuItem = UIBarButtonItem()
- var voiceItem = UIBarButtonItem()
-
- var childrenMenu = [UIAction]()
-
- var groupMap: [String:Int] = [:]
- lazy var searchController: UISearchController = {
- var searchController = UISearchController(searchResultsController: nil)
- searchController.delegate = self
- searchController.searchResultsUpdater = self
- searchController.searchBar.autocapitalizationType = .none
- searchController.searchBar.delegate = self
- searchController.searchBar.barTintColor = .secondaryColor
- searchController.searchBar.searchTextField.backgroundColor = .secondaryColor
- searchController.obscuresBackgroundDuringPresentation = false
-
- return searchController
- }()
-
- lazy var segment: UISegmentedControl = {
- var segment = UISegmentedControl(items: ["Chats".localized(), "Groups".localized()])
- segment.sizeToFit()
- segment.selectedSegmentIndex = 0
- segment.addTarget(self, action: #selector(segmentChanged(sender:)), for: .valueChanged)
- return segment
- }()
-
- var fillteredData: [Any] = []
-
- var isSearchBarEmpty: Bool {
- return searchController.searchBar.text?.isEmpty ?? true
- }
-
- var isFilltering: Bool {
- return searchController.isActive && !isSearchBarEmpty
- }
- @IBOutlet var tableView: UITableView!
-
- let speechRecognizer = SFSpeechRecognizer()
- var recognitionRequest : SFSpeechAudioBufferRecognitionRequest?
- var recognitionTask : SFSpeechRecognitionTask?
- let audioEngine = AVAudioEngine()
-
- func filterContentForSearchText(_ searchText: String) {
- switch segment.selectedSegmentIndex {
- case 1:
- fillteredData = self.groups.filter { $0.name.lowercased().contains(searchText.lowercased()) }
- default:
- fillteredData = self.chats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) }
- }
- tableView.reloadData()
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- SFSpeechRecognizer.requestAuthorization({ authStatus in
- var isButtonEnabled = false
- OperationQueue.main.addOperation {
- switch authStatus {
- case .authorized:
- isButtonEnabled = true
- print("User allowed access to speech recognition")
- case .denied:
- isButtonEnabled = false
- print("User denied access to speech recognition")
- case .restricted:
- isButtonEnabled = false
- print("Speech recognition restricted on this device")
- case .notDetermined:
- isButtonEnabled = false
- print("Speech recognition not yet authorized")
- @unknown default:
- print("Speech recognition not yet authorized")
- }
- }
- })
- let me = UserDefaults.standard.string(forKey: "me")!
- Database.shared.database?.inTransaction({ fmdb, rollback in
- 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() {
- isAdmin = cursor.string(forColumnIndex: 3) == "23" || cursor.string(forColumnIndex: 3) == "24"
- cursor.close()
- }
- })
-
- cancelSearchButton = UIBarButtonItem(title: "Cancel".localized(), style: .plain, target: self, action: #selector(cancel(sender:)))
-
- var childrenMenu : [UIAction] = []
-
- if(isAdmin){
- childrenMenu.append(UIAction(title: "Broadcast Message", image: UIImage(systemName: "envelope.open"), handler: {[weak self](_) in
- let controller = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "broadcastNav")
- self?.navigationController?.present(controller, animated: true, completion: nil)
- }))
- }
-
- menuItem = UIBarButtonItem(image: UIImage(systemName: "square.and.pencil"), style: .plain, target: self, action: #selector(startConversation))
- voiceItem = UIBarButtonItem(image: UIImage(systemName: "mic.fill"), style: .plain, target: self, action: #selector(recordAudio))
-
- tabBarController?.navigationItem.leftBarButtonItem = voiceItem
- tabBarController?.navigationItem.rightBarButtonItem = menuItem
- tabBarController?.navigationItem.searchController = searchController
-
- definesPresentationContext = true
-
-
- NotificationCenter.default.addObserver(self, selector: #selector(onReceiveMessage(notification:)), name: NSNotification.Name(rawValue: "onReceiveChat"), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(onReload(notification:)), name: NSNotification.Name(rawValue: "onMember"), object: nil)
-
-
-
- tableView.tableHeaderView = segment
-
- pullBuddy()
- let cpaasMode = PrefsUtil.getCpaasMode()
- let isBurger = cpaasMode == PrefsUtil.CPAAS_MODE_BURGER
- navigationController?.setNavigationBarHidden(!isBurger, animated: false)
-
- let tapGesture = UITapGestureRecognizer(target: self, action: #selector(collapseDocked))
- tapGesture.cancelsTouchesInView = false
- tapGesture.delegate = self
- self.view.addGestureRecognizer(tapGesture)
- }
-
- @objc func collapseDocked() {
- if ViewController.isExpandButton {
- ViewController.expandButton()
- }
- }
-
- @objc func startConversation(){
- let navigationController = AppStoryBoard.Palio.instance.instantiateViewController(withIdentifier: "contactChatNav") as! UINavigationController
- navigationController.modalPresentationStyle = .fullScreen
- navigationController.navigationBar.tintColor = .white
- navigationController.navigationBar.barTintColor = .mainColor
- navigationController.navigationBar.isTranslucent = false
- let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
- navigationController.navigationBar.titleTextAttributes = textAttributes
- navigationController.view.backgroundColor = .mainColor
- self.navigationController?.present(navigationController, animated: true, completion: nil)
- }
-
- @objc func recordAudio(){
- if audioEngine.isRunning {
- self.audioEngine.stop()
- self.recognitionRequest?.endAudio()
- voiceItem.image = UIImage(systemName: "mic.fill")
- searchController.searchBar.isUserInteractionEnabled = true
- searchController.automaticallyShowsCancelButton = true
- searchController.hidesNavigationBarDuringPresentation = true
- } else {
- print("start recording")
- self.startRecording()
- voiceItem.image = UIImage(systemName: "mic")
- searchController.searchBar.isUserInteractionEnabled = false
- }
- }
-
- func startRecording() {
- // Clear all previous session data and cancel task
- if recognitionTask != nil {
- recognitionTask?.cancel()
- recognitionTask = nil
- }
- // Create instance of audio session to record voice
- let audioSession = AVAudioSession.sharedInstance()
- do {
- try audioSession.setCategory(AVAudioSession.Category.record, mode: AVAudioSession.Mode.measurement, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
- try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
- } catch {
- print("audioSession properties weren't set because of an error.")
- }
- self.recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
- let inputNode = audioEngine.inputNode
- guard let recognitionRequest = recognitionRequest else {
- fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
- }
- recognitionRequest.shouldReportPartialResults = true
- self.recognitionTask = speechRecognizer?.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
- var isFinal = false
- if result != nil {
- self.searchController.searchBar.searchTextField.text = result?.bestTranscription.formattedString
- self.updateSearchResults(for: self.searchController)
- isFinal = (result?.isFinal)!
- }
- if error != nil || isFinal {
- self.audioEngine.stop()
- inputNode.removeTap(onBus: 0)
- self.recognitionRequest = nil
- self.recognitionTask = nil
- // self.btnStart.isEnabled = true
- }
- })
- let recordingFormat = inputNode.outputFormat(forBus: 0)
- inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
- self.recognitionRequest?.append(buffer)
- }
- self.audioEngine.prepare()
- do {
- try self.audioEngine.start()
- } catch {
- print("audioEngine couldn't start because of an error.")
- }
- //
- // self.lblText.text = "Say something, I'm listening!"
- }
-
- override func viewWillAppear(_ animated: Bool) {
- // tabBarController?.navigationItem.leftBarButtonItem = cancelSearchButton
- self.navigationController?.navigationBar.topItem?.title = Bundle.main.displayName
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
- let cpaasMode = PrefsUtil.getCpaasMode()
- let isBurger = cpaasMode == PrefsUtil.CPAAS_MODE_BURGER
- navigationController?.navigationBar.backgroundColor = .clear
- navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
- navigationController?.navigationBar.shadowImage = UIImage()
- navigationController?.navigationBar.isTranslucent = true
- navigationController?.setNavigationBarHidden(false, animated: false)
- navigationController?.navigationBar.tintColor = .black
- tabBarController?.navigationItem.leftBarButtonItem = voiceItem
- tabBarController?.navigationItem.searchController = searchController
- tabBarController?.navigationItem.rightBarButtonItem = menuItem
- let randomInt = Int.random(in: 1..<10)
- backgroundImage.image = UIImage(named: "pb_lbackground_\(randomInt)")
- getData()
- searchController.searchBar.placeholder = "Search chats & messages".localized()
- searchController.searchBar.setValue("Cancel".localized(), forKey: "cancelButtonText")
- if segment.numberOfSegments == 2 {
- segment.setTitle("Chats".localized(), forSegmentAt: 0)
- segment.setTitle("Groups".localized(), forSegmentAt: 1)
- }
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- tabBarController?.navigationItem.leftBarButtonItem = nil
- tabBarController?.navigationItem.searchController = nil
- tabBarController?.navigationItem.rightBarButtonItem = nil
- if ViewController.isExpandButton {
- ViewController.expandButton()
- }
- }
-
- @objc func onReload(notification: NSNotification) {
- let data:[AnyHashable : Any] = notification.userInfo!
- if data["member"] as! String == UserDefaults.standard.string(forKey: "me")! {
- DispatchQueue.main.async {
- self.getData()
- }
- }
- }
-
- @objc func onReceiveMessage(notification: NSNotification) {
- let data:[AnyHashable : Any] = notification.userInfo!
- guard let dataMessage = data["message"] as? TMessage else {
- return
- }
- let isUser = User.getData(pin: dataMessage.getBody(key: CoreMessage_TMessageKey.L_PIN)) != nil
- 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: "")
- let pin = isUser ? dataMessage.getBody(key: CoreMessage_TMessageKey.F_PIN) : chatId
- let messageId = dataMessage.getBody(key: CoreMessage_TMessageKey.MESSAGE_ID)
- if let index = chats.firstIndex(of: Chat(pin: pin)) {
- guard let chat = Chat.getData(messageId: messageId).first else {
- return
- }
- DispatchQueue.main.async {
- if self.segment.selectedSegmentIndex == 0 {
- self.tableView.beginUpdates()
- self.chats.remove(at: index)
- self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .none)
- }
- self.chats.insert(chat, at: 0)
- if self.segment.selectedSegmentIndex == 0 {
- self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
- self.tableView.endUpdates()
- }
- }
- } else {
- guard let chat = Chat.getData(messageId: messageId).first else {
- return
- }
- DispatchQueue.main.async {
- if self.segment.selectedSegmentIndex == 0 {
- self.tableView.beginUpdates()
- }
- self.chats.insert(chat, at: 0)
- if self.segment.selectedSegmentIndex == 0 {
- self.tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .none)
- self.tableView.endUpdates()
- }
- }
- }
- }
-
- @objc func add(sender: Any) {
-
- }
-
- @objc func cancel(sender: Any) {
- navigationController?.dismiss(animated: true, completion: nil)
- }
-
- @objc func segmentChanged(sender: Any) {
- filterContentForSearchText(searchController.searchBar.text!)
- }
-
- // MARK: - Data source
-
- func getData() {
- getChats {
- self.getContacts {
- self.getGroups { g1 in
- DispatchQueue.global().async {
- self.getOpenGroups(listGroups: g1, completion: { g in
- DispatchQueue.main.async {
- self.groups.removeAll()
- self.groups.append(contentsOf: g1)
- for og in g {
- if self.groups.first(where: { $0.id == og.id }) == nil {
- self.groups.append(og)
- }
- }
- DispatchQueue.main.async {
- self.tableView.reloadData()
- }
- }
- })
- }
- }
- }
- }
- }
-
- func getChats(completion: @escaping ()->()) {
- DispatchQueue.global().async {
- self.chats.removeAll()
- self.chats.append(contentsOf: Chat.getData())
- completion()
- }
- }
-
- private func getContacts(completion: @escaping ()->()) {
- DispatchQueue.global().async {
- self.contacts.removeAll()
- Database.shared.database?.inTransaction({ (fmdb, rollback) in
- do {
- 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") {
- while cursorData.next() {
- let user = User(pin: cursorData.string(forColumnIndex: 0) ?? "",
- firstName: cursorData.string(forColumnIndex: 1) ?? "",
- lastName: cursorData.string(forColumnIndex: 2) ?? "",
- thumb: cursorData.string(forColumnIndex: 3) ?? "",
- userType: cursorData.string(forColumnIndex: 5) ?? "")
- if (user.firstName + " " + user.lastName).trimmingCharacters(in: .whitespaces) == "USR\(user.pin)" {
- continue
- }
- user.official = cursorData.string(forColumnIndex: 4) ?? ""
- self.contacts.append(user)
- }
- cursorData.close()
- }
- } catch {
- rollback.pointee = true
- print(error)
- }
- completion()
- })
- }
- }
-
- private func getGroupRecursive(fmdb: FMDatabase, id: String = "", parent: String = "") -> [Group] {
- var data: [Group] = []
- 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 "
- if id.isEmpty {
- query += "g.parent = '\(parent)'"
- } else {
- query += "g.group_id = '\(id)'"
- }
- if let cursor = Database.shared.getRecords(fmdb: fmdb, query: query) {
- while cursor.next() {
- let group = Group(
- id: cursor.string(forColumnIndex: 0) ?? "",
- name: cursor.string(forColumnIndex: 1) ?? "",
- profile: cursor.string(forColumnIndex: 2) ?? "",
- quote: cursor.string(forColumnIndex: 3) ?? "",
- by: cursor.string(forColumnIndex: 4) ?? "",
- date: cursor.string(forColumnIndex: 5) ?? "",
- parent: cursor.string(forColumnIndex: 6) ?? "",
- chatId: "",
- groupType: cursor.string(forColumnIndex: 7) ?? "",
- isOpen: cursor.string(forColumnIndex: 8) ?? "",
- official: cursor.string(forColumnIndex: 9) ?? "",
- isEducation: cursor.string(forColumnIndex: 10) ?? "")
-
- if group.chatId.isEmpty {
- 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)
- group.childs.append(lounge)
- }
-
- if let topicCursor = Database.shared.getRecords(fmdb: fmdb, query: "select chat_id, title, thumb from DISCUSSION_FORUM where group_id = '\(group.id)'") {
- while topicCursor.next() {
- let topic = Group(id: group.id,
- name: topicCursor.string(forColumnIndex: 1) ?? "",
- profile: topicCursor.string(forColumnIndex: 2) ?? "",
- quote: group.quote,
- by: group.by,
- date: group.date,
- parent: group.id,
- chatId: topicCursor.string(forColumnIndex: 0) ?? "",
- groupType: group.groupType,
- isOpen: group.isOpen,
- official: group.official,
- isEducation: group.isEducation)
- group.childs.append(topic)
- }
- topicCursor.close()
- }
-
- if !group.id.isEmpty {
- if group.official == "1" {
- let idMe = UserDefaults.standard.string(forKey: "me") as String?
- if let cursorUser = Database.shared.getRecords(fmdb: fmdb, query: "SELECT user_type FROM BUDDY where f_pin='\(idMe!)'"), cursorUser.next() {
- group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
- cursorUser.close()
- }
- } else if group.official != "1"{
- group.childs.append(contentsOf: getGroupRecursive(fmdb: fmdb, parent: group.id))
- }
- }
- data.append(group)
- }
- cursor.close()
- }
- return data
- }
-
- private func getOpenGroups(listGroups: [Group], completion: @escaping ([Group]) -> ()) {
- if let response = Nexilis.writeSync(message: CoreMessage_TMessageBank.getOpenGroups(p_account: "1,2,3,5,6,7", offset: "0", search: "")) {
- var dataGroups: [Group] = []
- if (response.getBody(key: CoreMessage_TMessageKey.ERRCOD, default_value: "99") == "00") {
- let data = response.getBody(key: CoreMessage_TMessageKey.DATA)
- if let json = try! JSONSerialization.jsonObject(with: data.data(using: String.Encoding.utf8)!, options: []) as? [[String: Any?]] {
- for dataJson in json {
- let group = Group(
- id: dataJson[CoreMessage_TMessageKey.GROUP_ID] as? String ?? "",
- name: dataJson[CoreMessage_TMessageKey.GROUP_NAME] as? String ?? "",
- profile: dataJson[CoreMessage_TMessageKey.THUMB_ID] as? String ?? "",
- quote: dataJson[CoreMessage_TMessageKey.QUOTE] as? String ?? "",
- by: dataJson[CoreMessage_TMessageKey.BLOCK] as? String ?? "",
- date: "",
- parent: "",
- chatId: "",
- groupType: "NOTJOINED",
- isOpen: dataJson[CoreMessage_TMessageKey.IS_OPEN] as? String ?? "",
- official: "0",
- isEducation: "")
- dataGroups.append(group)
- }
- }
- } else {
- DispatchQueue.main.async {
- self.groups.removeAll()
- self.groups.append(contentsOf: listGroups)
- self.tableView.reloadData()
- }
- }
- completion(dataGroups)
- }
- }
-
- private func getGroups(id: String = "", parent: String = "", completion: @escaping ([Group]) -> ()) {
- DispatchQueue.global().async {
- Database.shared.database?.inTransaction({ fmdb, rollback in
- completion(self.getGroupRecursive(fmdb: fmdb, id: id, parent: parent))
- })
- }
- }
-
- private func pullBuddy() {
- if let me = UserDefaults.standard.string(forKey: "me") {
- DispatchQueue.global().async {
- let _ = Nexilis.write(message: CoreMessage_TMessageBank.getBatchBuddiesInfos(p_f_pin: me, last_update: 0))
- }
- }
- }
-
- private func joinOpenGroup(groupId: String, flagMember: String = "0", completion: @escaping (Bool) -> ()) {
- DispatchQueue.global().async {
- var result: Bool = false
- let idMe = UserDefaults.standard.string(forKey: "me") as String?
- if let response = Nexilis.writeAndWait(message: CoreMessage_TMessageBank.getAddGroupMember(p_group_id: groupId, p_member_pin: idMe!, p_position: "0")), response.isOk() {
- result = true
- }
- completion(result)
- }
- }
- @IBOutlet weak var backgroundImage: UIImageView!
-
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- }
- */
- }
- // MARK: - Table view data source
- extension SecondTabViewController: UITableViewDelegate, UITableViewDataSource {
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- switch segment.selectedSegmentIndex {
- case 0:
- let data: Chat
- if isFilltering {
- data = fillteredData[indexPath.row] as! Chat
- } else {
- data = chats[indexPath.row]
- }
- if let chooser = isChooser {
- if data.pin == "-999"{
- return
- }
- chooser(data.messageScope, data.pin)
- dismiss(animated: true, completion: nil)
- return
- }
- let user = User.getData(pin: data.pin)
- if user != nil || data.pin == "-999" {
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = data.pin
- navigationController?.show(editorPersonalVC, sender: nil)
- } else {
- let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
- editorGroupVC.hidesBottomBarWhenPushed = true
- editorGroupVC.unique_l_pin = data.pin
- navigationController?.show(editorGroupVC, sender: nil)
- }
- case 1:
- let group: Group
- if isFilltering {
- if indexPath.row == 0 {
- group = fillteredData[indexPath.section] as! Group
- } else {
- group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
- }
- } else {
- if indexPath.row == 0 {
- group = groups[indexPath.section]
- } else {
- group = groups[indexPath.section].childs[indexPath.row - 1]
- }
- }
- group.isSelected = !group.isSelected
- if !group.isSelected{
- var sects = 0
- var sect = indexPath.section
- var id = group.id
- if let e = groupMap[id] {
- var loooop = true
- repeat {
- let c = sect + 1
- if isFilltering {
- if let o = self.fillteredData[c] as? Group {
- if o.parent == id {
- sects = sects + 1
- sect = c
- id = o.id
- (self.fillteredData[c] as! Group).isSelected = false
- self.groupMap.removeValue(forKey: (self.fillteredData[c] as! Group).id)
- }
- else {
- loooop = false
- }
- }
- }
- else {
- if self.groups[c].parent == id {
- sects = sects + 1
- sect = c
- id = self.groups[c].id
- self.groups[c].isSelected = false
- self.groupMap.removeValue(forKey: self.groups[c].id)
- }
- else {
- loooop = false
- }
- }
- } while(loooop)
- }
- for i in stride(from: sects, to: 0, by: -1){
- if isFilltering {
- self.fillteredData.remove(at: indexPath.section + i)
- }
- else {
- self.groups.remove(at: indexPath.section + i)
- }
- }
- groupMap.removeValue(forKey: group.id)
- }
- if group.groupType == "NOTJOINED" {
- let alert = UIAlertController(title: "Do you want to join this group?".localized(), message: "Groups : \(group.name)\nMembers: \(group.by)".localized(), preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "Cancel".localized(), style: .cancel, handler: nil))
- alert.addAction(UIAlertAction(title: "Join".localized(), style: .default, handler: {(_) in
- self.joinOpenGroup(groupId: group.id, completion: { result in
- if result {
- DispatchQueue.main.async {
- self.groupMap.removeAll()
- let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
- editorGroupVC.hidesBottomBarWhenPushed = true
- editorGroupVC.unique_l_pin = group.id
- self.navigationController?.show(editorGroupVC, sender: nil)
- }
- }
- })
- }))
- self.present(alert, animated: true, completion: nil)
- return
- }
- if group.childs.count == 0 {
- let groupId = group.chatId.isEmpty ? group.id : group.chatId
- if let chooser = isChooser {
- chooser("4", groupId)
- dismiss(animated: true, completion: nil)
- return
- }
- self.groupMap.removeAll()
- let editorGroupVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorGroupVC") as! EditorGroup
- editorGroupVC.hidesBottomBarWhenPushed = true
- editorGroupVC.unique_l_pin = groupId
- navigationController?.show(editorGroupVC, sender: nil)
- } else {
- if indexPath.row == 0 {
- tableView.reloadData()
- } else {
- getGroups(id: group.id) { g in
- DispatchQueue.main.async {
- print("index path section: \(indexPath.section)")
- print("index path row: \(indexPath.row)")
- // print("index path item: \(indexPath.item)")
- if self.isFilltering {
- // self.fillteredData.remove(at: indexPath.section)
- if self.fillteredData[indexPath.section] is Group {
- self.groupMap[(self.fillteredData[indexPath.section] as! Group).id] = 1
- self.fillteredData.insert(contentsOf: g, at: indexPath.section + 1)
- }
- } else {
- // self.groups.remove(at: indexPath.section)
- self.groupMap[self.groups[indexPath.section].id] = 1
- self.groups.insert(contentsOf: g, at: indexPath.section + 1)
- }
- print("groupMap: \(self.groupMap)")
- tableView.reloadData()
- }
- }
- }
- }
- default:
- let data = contacts[indexPath.row]
- let editorPersonalVC = AppStoryBoard.Palio.instance.instantiateViewController(identifier: "editorPersonalVC") as! EditorPersonal
- editorPersonalVC.hidesBottomBarWhenPushed = true
- editorPersonalVC.unique_l_pin = data.pin
- navigationController?.show(editorPersonalVC, sender: nil)
- }
- }
-
- func numberOfSections(in tableView: UITableView) -> Int {
- if isFilltering {
- if segment.selectedSegmentIndex == 1 {
- return fillteredData.count
- }
- return 1
- } else {
- if segment.selectedSegmentIndex == 1 {
- return groups.count
- }
- return 1
- }
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- var value = 0
- if isFilltering {
- if segment.selectedSegmentIndex == 2, let groups = fillteredData as? [Group] {
- let group = groups[section]
- if group.isSelected {
- if let g = groupMap[group.id] {
- value = 1
- }
- else {
- value = group.childs.count + 1
- }
- } else {
- value = 1
- }
- }
- return fillteredData.count
- }
- switch segment.selectedSegmentIndex {
- case 0:
- value = chats.count
- case 1:
- let group = groups[section]
- if group.isSelected {
- if let g = groupMap[group.id] {
- value = 1
- }
- else {
- value = group.childs.count + 1
- }
- } else {
- value = 1
- }
- default:
- value = chats.count
- }
- return value
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- var cell: UITableViewCell!
- switch segment.selectedSegmentIndex {
- case 0:
- cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierChat", for: indexPath)
- cell.separatorInset.left = 60.0
- let content = cell.contentView
- if content.subviews.count > 0 {
- content.subviews.forEach { $0.removeFromSuperview() }
- }
- let data: Chat
- if isFilltering {
- data = fillteredData[indexPath.row] as! Chat
- } else {
- data = chats[indexPath.row]
- }
- let imageView = UIImageView()
- content.addSubview(imageView)
- imageView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- imageView.leadingAnchor.constraint(equalTo: content.leadingAnchor, constant: 10.0),
- imageView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
- imageView.bottomAnchor.constraint(equalTo: content.bottomAnchor, constant: -20.0),
- imageView.widthAnchor.constraint(equalToConstant: 40.0),
- imageView.heightAnchor.constraint(equalToConstant: 40.0)
- ])
- if data.profile.isEmpty && data.pin != "-999" {
- let user = User.getData(pin: data.pin)
- if user != nil {
- imageView.image = UIImage(named: "Profile---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- } else {
- imageView.image = UIImage(named: "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil)
- }
- } else {
- getImage(name: data.profile, placeholderImage: UIImage(named: data.pin == "-999" ? "pb_button" : "Conversation---Purple", in: Bundle.resourceBundle(for: Nexilis.self), with: nil), isCircle: true, tableView: tableView, indexPath: indexPath, completion: { result, isDownloaded, image in
- imageView.image = image
- })
- }
- let titleView = UILabel()
- content.addSubview(titleView)
- titleView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- titleView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
- titleView.topAnchor.constraint(equalTo: content.topAnchor, constant: 10.0),
- titleView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
- ])
- titleView.text = data.name
- titleView.font = UIFont.systemFont(ofSize: 14, weight: .medium)
-
- let messageView = UILabel()
- content.addSubview(messageView)
- messageView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- messageView.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: 10.0),
- messageView.topAnchor.constraint(equalTo: titleView.bottomAnchor, constant: 5.0),
- messageView.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -40.0),
- ])
- messageView.textColor = .gray
- let text = Utils.previewMessageText(chat: data)
- if let attributeText = text as? NSAttributedString {
- messageView.attributedText = attributeText
- } else if let stringText = text as? String {
- messageView.text = stringText
- }
- messageView.font = UIFont.systemFont(ofSize: 12)
- messageView.numberOfLines = 2
-
- if data.counter != "0" {
- let viewCounter = UIView()
- content.addSubview(viewCounter)
- viewCounter.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- viewCounter.centerYAnchor.constraint(equalTo: content.centerYAnchor),
- viewCounter.trailingAnchor.constraint(equalTo: content.trailingAnchor, constant: -20),
- viewCounter.widthAnchor.constraint(greaterThanOrEqualToConstant: 20),
- viewCounter.heightAnchor.constraint(equalToConstant: 20)
- ])
- viewCounter.backgroundColor = .systemRed
- viewCounter.layer.cornerRadius = 10
- viewCounter.clipsToBounds = true
- viewCounter.layer.borderWidth = 0.5
- viewCounter.layer.borderColor = UIColor.secondaryColor.cgColor
- let labelCounter = UILabel()
- viewCounter.addSubview(labelCounter)
- labelCounter.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- labelCounter.centerYAnchor.constraint(equalTo: viewCounter.centerYAnchor),
- labelCounter.leadingAnchor.constraint(equalTo: viewCounter.leadingAnchor, constant: 2),
- labelCounter.trailingAnchor.constraint(equalTo: viewCounter.trailingAnchor, constant: -2),
- ])
- labelCounter.font = UIFont.systemFont(ofSize: 11)
- if Int(data.counter)! > 99 {
- labelCounter.text = "99+"
- } else {
- labelCounter.text = data.counter
- }
- labelCounter.textColor = .secondaryColor
- labelCounter.textAlignment = .center
- }
- case 1:
- cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierGroup", for: indexPath)
- var content = cell.defaultContentConfiguration()
- content.textProperties.font = UIFont.systemFont(ofSize: 14)
- let group: Group
- if isFilltering {
- if indexPath.row == 0 {
- group = fillteredData[indexPath.section] as! Group
- } else {
- group = (fillteredData[indexPath.section] as! Group).childs[indexPath.row - 1]
- }
- } else {
- if indexPath.row == 0 {
- group = groups[indexPath.section]
- } else {
- group = groups[indexPath.section].childs[indexPath.row - 1]
- }
- }
- if group.official == "1" && group.parent == "" {
- 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)
- }
- else if group.isOpen == "1" && group.parent == "" {
- if self.traitCollection.userInterfaceStyle == .dark {
- content.attributedText = self.set(image: UIImage(systemName: "globe")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
- } else {
- content.attributedText = self.set(image: UIImage(systemName: "globe")!, with: " \(group.name)", size: 15, y: -4)
- }
- } else if group.parent == "" {
- if self.traitCollection.userInterfaceStyle == .dark {
- content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!.withTintColor(.white), with: " \(group.name)", size: 15, y: -4)
- } else {
- content.attributedText = self.set(image: UIImage(systemName: "lock.fill")!, with: " \(group.name)", size: 15, y: -4)
- }
- } else {
- content.text = group.name
- }
- if group.childs.count > 0 {
- let iconName = (group.isSelected) ? "chevron.up.circle" : "chevron.down.circle"
- let imageView = UIImageView(image: UIImage(systemName: iconName))
- imageView.tintColor = .black
- cell.accessoryView = imageView
- }
- else {
- cell.accessoryView = nil
- cell.accessoryType = .none
- }
- content.imageProperties.maximumSize = CGSize(width: 40, height: 40)
- 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
- content.image = image
- }
- cell.contentConfiguration = content
- default:
- cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifierContact", for: indexPath)
- var content = cell.defaultContentConfiguration()
- content.text = ""
- cell.contentConfiguration = content
- }
- cell.backgroundColor = .clear
- return cell
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 70.0
- }
-
- }
- extension SecondTabViewController: UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating {
-
- func updateSearchResults(for searchController: UISearchController) {
- filterContentForSearchText(searchController.searchBar.text!)
- }
-
- func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
- recordAudio()
- }
-
- func set(image: UIImage, with text: String, size: CGFloat, y: CGFloat) -> NSAttributedString {
- let attachment = NSTextAttachment()
- attachment.image = image
- attachment.bounds = CGRect(x: 0, y: y, width: size, height: size)
- let attachmentStr = NSAttributedString(attachment: attachment)
-
- let mutableAttributedString = NSMutableAttributedString()
- mutableAttributedString.append(attachmentStr)
-
- let textString = NSAttributedString(string: text)
- mutableAttributedString.append(textString)
-
- return mutableAttributedString
- }
-
- }
|