Просмотр исходного кода

update counter total matches search

alqindiirsyam 4 месяцев назад
Родитель
Сommit
fb2640dc29

+ 1 - 1
AppBuilder/AppBuilder/SecondTabViewController.swift

@@ -122,7 +122,7 @@ class SecondTabViewController: UIViewController, UIScrollViewDelegate, UIGesture
                         group_id = filterGroupKey.key
                     }
                     let deepCopyChats = self.chats.map{ $0.copy() }
-                    fillteredData = deepCopyChats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.groupId == group_id }
+                    fillteredData = deepCopyChats.filter { $0.name.lowercased().contains(searchText.lowercased()) || $0.messageText.lowercased().contains(searchText.lowercased()) || $0.groupId == group_id }
                     if searchText.count > 1 {
 //                        fillteredMessages = Chat.getMessageFromSearch(text: searchText)
 //                        print("SEKUTT \(fillteredMessages.count)")

+ 19 - 0
NexilisLite/NexilisLite/Source/IncomingThread.swift

@@ -1317,10 +1317,29 @@ class IncomingThread {
         } else {
             Nexilis.saveMessage(message: message, withStatus: false)
         }
+        if APIS.checkAppStateisBackground() {
+            APIS.addNotificationNexilis(message)
+            ackAPN(id: message.mStatus)
+        }
         //print("save message incoming")
         ack(message: message)
     }
     
+    private func ackAPN(id: String) {
+        DispatchQueue.global().async {
+            Nexilis.sendStateToServer(s: "send ack from apn")
+            DispatchQueue.global().async {
+                let parameter: [String : Any] = [
+                    "pin": User.getMyPin() ?? "",
+                    "message_id": id
+                ]
+                Utils.postDataWithCookiesAndUserAgent(from: URL(string: Utils.getDomainOpr() + "ack_message")!, parameter: parameter, isFormData: true) { data, response, error in
+                }
+            }
+        }
+    }
+
+    
     private func receiveMessageStatus(message: TMessage) -> Void {
         guard let _: String = SecureUserDefaults.shared.value(forKey: "status") else {
             //print("App not ready!!! skip receive message \(message_id)")

+ 18 - 0
NexilisLite/NexilisLite/Source/Model/Chat.swift

@@ -127,6 +127,24 @@ public class Chat: Model {
         return ""
     }
     
+    public static func getCountSearchMessage(key: String, pin: String, chatId:String = "", isPersonal: Bool) -> Int {
+        var query = ""
+        var count = 0
+        if isPersonal {
+            query = "select message_id FROM MESSAGE where message_text LIKE '%\(key)%' and (l_pin = '\(pin)' or f_pin = '\(pin)')"
+        } else {
+            query = "select message_id FROM MESSAGE where message_text LIKE '%\(key)%' and l_pin = '\(pin)' and chat_id = '\(chatId)'"
+        }
+        Database.shared.database?.inTransaction({ (fmdb, rollback) in
+            if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
+                while cursorData.next() {
+                    count+=1
+                }
+            }
+        })
+        return count
+    }
+    
     public static func getMessageFromSearch(text: String = "") -> [Chat] {
         var messages: [Chat] = []
         Database.shared.database?.inTransaction({ (fmdb, rollback) in

+ 18 - 16
NexilisLite/NexilisLite/Source/View/Chat/EditorGroup.swift

@@ -117,6 +117,7 @@ public class EditorGroup: UIViewController, CLLocationManagerDelegate {
     var audioPlayers: [IndexPath: AVAudioPlayer] = [:]
     var timers: [IndexPath: Timer] = [:]
     var playingIndexPath: IndexPath?
+    var timerSearch: Timer?
     
     func offset() -> CGFloat{
         guard let fontSize = Int(SecureUserDefaults.shared.value(forKey: "font_size") ?? "0") else { return 0 }
@@ -1679,14 +1680,10 @@ public class EditorGroup: UIViewController, CLLocationManagerDelegate {
                 UIView.animate(withDuration: TimeInterval(duration), animations: {
                     self.view.layoutIfNeeded()
                 })
-                if isSearching {
-                    self.tableChatView.scrollToBottom()
+                if (self.currentIndexpath != nil) {
+                    self.tableChatView.scrollToRow(at: IndexPath(row: self.currentIndexpath!.row, section: self.currentIndexpath!.section), at: .none, animated: false)
                 } else {
-                    if (self.currentIndexpath != nil) {
-                        self.tableChatView.scrollToRow(at: IndexPath(row: self.currentIndexpath!.row, section: self.currentIndexpath!.section), at: .none, animated: false)
-                    } else {
-                        self.tableChatView.scrollToBottom()
-                    }
+                    self.tableChatView.scrollToBottom()
                 }
             }
         }  else if isEditingMessage {
@@ -1997,7 +1994,7 @@ public class EditorGroup: UIViewController, CLLocationManagerDelegate {
     }
     
     private func addButtonScrollToBottom() {
-        if tableChatView.alpha != 1 {
+        if tableChatView.alpha != 1 || isSearching {
             return
         }
         self.view.addSubview(buttonScrollToBottom)
@@ -2023,6 +2020,9 @@ public class EditorGroup: UIViewController, CLLocationManagerDelegate {
     }
     
     private func addCounterAtButttonScrollToBottom() {
+        if tableChatView.alpha != 1 || isSearching {
+            return
+        }
         self.view.addSubview(indicatorCounterBSTB)
         indicatorCounterBSTB.translatesAutoresizingMaskIntoConstraints = false
         indicatorCounterBSTB.backgroundColor = .systemRed
@@ -4899,9 +4899,6 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource, AVAudioPlayer
         
         if isSearching && textSearch.count > 1 {
             messageText.attributedText = textChat.richText(isSearching: true, textSearch: textSearch, group_id: self.dataGroup["group_id"]  as? String ?? "")
-            if textChat.lowercased().contains(textSearch) {
-                countMatchesSearch += 1
-            }
         }
         
         let stringDate = (dataMessages[indexPath.row]["server_date"]  as? String ?? "")
@@ -6684,11 +6681,16 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource, AVAudioPlayer
 extension EditorGroup: UISearchBarDelegate {
     
     public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
-        textSearch = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
-        countMatchesSearch = 0
-        titleSearchMatches.isHidden = true
-        tableChatView.reloadData()
-        scrollToFirstSearchMessage()
+        timerSearch?.invalidate()
+        if searchText.count > 1 {
+            timerSearch = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: {[self] _ in
+                textSearch = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
+                titleSearchMatches.isHidden = true
+                countMatchesSearch = Chat.getCountSearchMessage(key: textSearch, pin: (self.dataGroup["group_id"] as? String) ?? "", chatId: (self.dataTopic["chat_id"] as? String) ?? "", isPersonal: false)
+                tableChatView.reloadData()
+                scrollToFirstSearchMessage()
+            })
+        }
     }
 }
     

+ 15 - 9
NexilisLite/NexilisLite/Source/View/Chat/EditorPersonal.swift

@@ -128,6 +128,7 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
     var audioPlayers: [IndexPath: AVAudioPlayer] = [:]
     var timers: [IndexPath: Timer] = [:]
     var playingIndexPath: IndexPath?
+    var timerSearch: Timer?
     
     func offset() -> CGFloat{
         guard let fontSize = Int(SecureUserDefaults.shared.value(forKey: "font_size") ?? "0") else { return 0 }
@@ -3347,7 +3348,7 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
     }
     
     private func addButtonScrollToBottom() {
-        if tableChatView.alpha != 1 {
+        if tableChatView.alpha != 1 || isSearching {
             return
         }
         self.view.addSubview(buttonScrollToBottom)
@@ -3373,6 +3374,9 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
     }
     
     private func addCounterAtButttonScrollToBottom() {
+        if tableChatView.alpha != 1 || isSearching {
+            return
+        }
         self.view.addSubview(indicatorCounterBSTB)
         indicatorCounterBSTB.translatesAutoresizingMaskIntoConstraints = false
         indicatorCounterBSTB.backgroundColor = .systemRed
@@ -6133,9 +6137,6 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource, AVAudioPla
         
         if isSearching && textSearch.count > 1 {
             messageText.attributedText = stringLS.isEmpty ? textChat.richText(isSearching: true, textSearch: textSearch) : stringLS.richText(isSearching: true, textSearch: textSearch)
-            if textChat.lowercased().contains(textSearch) {
-                countMatchesSearch += 1
-            }
         }
         
         let stringDate = (dataMessages[indexPath.row]["server_date"] as? String) ?? ""
@@ -8011,11 +8012,16 @@ extension UIImage {
 extension EditorPersonal: UISearchBarDelegate {
     
     public func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
-        textSearch = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
-        countMatchesSearch = 0
-        titleSearchMatches.isHidden = true
-        tableChatView.reloadData()
-        scrollToFirstSearchMessage()
+        timerSearch?.invalidate()
+        if searchText.count > 1 {
+            timerSearch = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: {[self] _ in
+                textSearch = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
+                titleSearchMatches.isHidden = true
+                countMatchesSearch = Chat.getCountSearchMessage(key: textSearch, pin: unique_l_pin, isPersonal: true)
+                tableChatView.reloadData()
+                scrollToFirstSearchMessage()
+            })
+        }
     }
 }