瀏覽代碼

ringtone message support change from BE

alqindiirsyam 5 月之前
父節點
當前提交
02dba5d772

+ 6 - 4
AppBuilder/AppBuilder/FirstTabViewController.swift

@@ -242,11 +242,13 @@ class FirstTabViewController: UIViewController, UIScrollViewDelegate, UIGestureR
     }
     
     override func viewWillDisappear(_ animated: Bool) {
-        if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
-            self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
+        if self.webView != nil {
+            if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
+                self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
+            }
+            self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
+            self.webView.evaluateJavaScript("hideAddToCart();")
         }
-        self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
-        self.webView.evaluateJavaScript("hideAddToCart();")
     }
     
     func scrollViewDidScroll(_ scrollView: UIScrollView) {

+ 1 - 1
AppBuilder/AppBuilder/SceneDelegate.swift

@@ -33,7 +33,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
 
                 // ⏳Wait for 5 seconds, then remove.
                 //
-                DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2))
+                DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1))
                 {
                     UIView.animate(withDuration: -1, // system default
                         animations:

+ 8 - 6
AppBuilder/AppBuilder/ThirdTabViewController.swift

@@ -246,13 +246,15 @@ class ThirdTabViewController: UIViewController, UIScrollViewDelegate, UIGestureR
     }
     
     override func viewWillDisappear(_ animated: Bool) {
-        if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
-            self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
+        if self.webView != nil {
+            if self.webView.scrollView.contentOffset.y < 0 { // Move tableView to top
+                self.webView.scrollView.setContentOffset(CGPoint.zero, animated: true)
+            }
+            self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
+            view.endEditing(true)
+            resignFirstResponder()
+            self.webView.evaluateJavaScript("hideAddToCart();")
         }
-        self.webView.evaluateJavaScript("{if(pauseAll){pauseAll();}}")
-        view.endEditing(true)
-        resignFirstResponder()
-        self.webView.evaluateJavaScript("hideAddToCart();")
     }
     
     func scrollViewDidScroll(_ scrollView: UIScrollView) {

+ 37 - 7
NexilisLite/NexilisLite/Source/APIS.swift

@@ -912,7 +912,7 @@ public class APIS: NSObject {
     //                                            print("Incoming call reported successfully")
     //                                        }
     //                                    }
-                                copySoundToLocalPath("pb_call_in")
+                                copySoundToLocalPath("pb_call_in", false)
                                 let center = UNUserNotificationCenter.current()
                                 let content = UNMutableNotificationContent()
                                 content.title = callFromName
@@ -1035,10 +1035,16 @@ public class APIS: NSObject {
             }
         }
         var nameSound = soundId.components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: "_")
+        var fromPref = false
         if nameSound.contains("_(Default)") {
-            nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+            if !Utils.getDefaultIncomingMsg().isEmpty {
+                nameSound = Utils.getDefaultIncomingMsg()
+                fromPref = true
+            } else {
+                nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+            }
         }
-        copySoundToLocalPath(nameSound)
+        copySoundToLocalPath(nameSound, fromPref)
         let center = UNUserNotificationCenter.current()
         let content = UNMutableNotificationContent()
         content.title = nameUser
@@ -1062,10 +1068,34 @@ public class APIS: NSObject {
         }
     }
     
-    private static func copySoundToLocalPath(_ nameSound: String) {
-        var sourceURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
-        if sourceURL == nil {
-            sourceURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+    private static func copySoundToLocalPath(_ nameSound: String, _ fromPref: Bool) {
+        var sourceURL: URL?
+        if fromPref {
+            let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
+            let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
+            let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
+            if let dirPath = paths.first {
+                let audioURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameSound)
+                if FileManager.default.fileExists(atPath: audioURL.path) {
+                    sourceURL = audioURL
+                } else if FileEncryption.shared.isSecureExists(filename: nameSound) {
+                    do {
+                        if let audioData = try FileEncryption.shared.readSecure(filename: nameSound) {
+                            let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
+                            let tempPath = cachesDirectory.appendingPathComponent(nameSound)
+                            try audioData.write(to: tempPath)
+                            sourceURL = tempPath
+                        }
+                    } catch {
+                        
+                    }
+                }
+            }
+        } else {
+            sourceURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+            if sourceURL == nil {
+                sourceURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+            }
         }
         let fileManager = FileManager.default
         let soundDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!.appendingPathComponent("Sounds", isDirectory: true)

+ 4 - 0
NexilisLite/NexilisLite/Source/Download.swift

@@ -45,6 +45,10 @@ public class Download {
         _ = Nexilis.write(message: CoreMessage_TMessageBank.getImageDownload(p_image_id: forKey))
     }
     
+    public func startHTTP(forKey: String, downloadUrl: String, completion: @escaping (String, Double)->()) {
+        _ = startHTTP(filename: forKey, isImage: false, baseURL: downloadUrl, completion: completion)
+    }
+    
     public func startHTTP(forKey: String, isImage: Bool = false, completion: @escaping (String, Double)->()) {
         _ = startHTTP(filename: forKey, isImage: isImage, baseURL: DOWNLOAD_URL, completion: completion)
     }

+ 132 - 14
NexilisLite/NexilisLite/Source/Nexilis.swift

@@ -3899,16 +3899,75 @@ extension Nexilis: MessageDelegate {
                                             }
                                             do {
                                                 var nameSound = soundId.components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: "_")
+                                                var fromPref = false
                                                 if nameSound.contains("_(Default)") {
-                                                    nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+                                                    if !Utils.getDefaultIncomingMsg().isEmpty {
+                                                        nameSound = Utils.getDefaultIncomingMsg()
+                                                        fromPref = true
+                                                    } else {
+                                                        nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+                                                    }
                                                 }
-                                                var soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
-                                                if soundURL == nil {
-                                                    soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                                                var soundURL: URL?
+                                                if fromPref {
+                                                    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
+                                                    let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
+                                                    let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
+                                                    if let dirPath = paths.first {
+                                                        let audioURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameSound)
+                                                        if !FileManager.default.fileExists(atPath: audioURL.path) && !FileEncryption.shared.isSecureExists(filename: nameSound) {
+                                                            Download().startHTTP(forKey: nameSound,downloadUrl: Utils.getURLBase() + "filepalio/ringtone/") { (name, progress) in
+                                                                guard progress == 100 else {
+                                                                    return
+                                                                }
+                                                                playAudio()
+                                                            }
+                                                        } else {
+                                                            playAudio()
+                                                        }
+                                                        
+                                                        func playAudio() {
+                                                            if FileManager.default.fileExists(atPath: audioURL.path) {
+                                                                do {
+                                                                    Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: audioURL)
+                                                                    Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                                                    Nexilis.sharedAudioPlayer?.play()
+                                                                } catch {
+                                                                    
+                                                                }
+                                                            } else if FileEncryption.shared.isSecureExists(filename: nameSound) {
+                                                                do {
+                                                                    if let audioData = try FileEncryption.shared.readSecure(filename: nameSound) {
+                                                                        let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
+                                                                        let tempPath = cachesDirectory.appendingPathComponent(nameSound)
+                                                                        try audioData.write(to: tempPath)
+                                                                        do {
+                                                                            Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: tempPath)
+                                                                            Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                                                            Nexilis.sharedAudioPlayer?.play()
+                                                                        } catch {
+                                                                            
+                                                                        }
+                                                                    }
+                                                                } catch {
+                                                                    
+                                                                }
+                                                            }
+                                                        }
+                                                    }
+                                                } else {
+                                                    soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                                                    if soundURL == nil {
+                                                        soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                                                    }
+                                                    do {
+                                                        Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
+                                                        Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                                        Nexilis.sharedAudioPlayer?.play()
+                                                    } catch {
+                                                        
+                                                    }
                                                 }
-                                                Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
-                                                Nexilis.sharedAudioPlayer?.prepareToPlay()
-                                                Nexilis.sharedAudioPlayer?.play()
                                                 DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: {
                                                     self.floating = nil
                                                 })
@@ -3941,16 +4000,75 @@ extension Nexilis: MessageDelegate {
                         }
                         do {
                             var nameSound = soundId.components(separatedBy: ":")[1].replacingOccurrences(of: " ", with: "_")
+                            var fromPref = false
                             if nameSound.contains("_(Default)") {
-                                nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+                                if !Utils.getDefaultIncomingMsg().isEmpty {
+                                    nameSound = Utils.getDefaultIncomingMsg()
+                                    fromPref = true
+                                } else {
+                                    nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+                                }
                             }
-                            var soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
-                            if soundURL == nil {
-                                soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                            var soundURL: URL?
+                            if fromPref {
+                                let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
+                                let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
+                                let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
+                                if let dirPath = paths.first {
+                                    let audioURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameSound)
+                                    if !FileManager.default.fileExists(atPath: audioURL.path) && !FileEncryption.shared.isSecureExists(filename: nameSound) {
+                                        Download().startHTTP(forKey: nameSound,downloadUrl: Utils.getURLBase() + "filepalio/ringtone/") { (name, progress) in
+                                            guard progress == 100 else {
+                                                return
+                                            }
+                                            playAudio()
+                                        }
+                                    } else {
+                                        playAudio()
+                                    }
+                                    
+                                    func playAudio() {
+                                        if FileManager.default.fileExists(atPath: audioURL.path) {
+                                            do {
+                                                Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: audioURL)
+                                                Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                                Nexilis.sharedAudioPlayer?.play()
+                                            } catch {
+                                                
+                                            }
+                                        } else if FileEncryption.shared.isSecureExists(filename: nameSound) {
+                                            do {
+                                                if let audioData = try FileEncryption.shared.readSecure(filename: nameSound) {
+                                                    let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
+                                                    let tempPath = cachesDirectory.appendingPathComponent(nameSound)
+                                                    try audioData.write(to: tempPath)
+                                                    do {
+                                                        Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: tempPath)
+                                                        Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                                        Nexilis.sharedAudioPlayer?.play()
+                                                    } catch {
+                                                        
+                                                    }
+                                                }
+                                            } catch {
+                                                
+                                            }
+                                        }
+                                    }
+                                }
+                            } else {
+                                soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                                if soundURL == nil {
+                                    soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+                                }
+                                do {
+                                    Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
+                                    Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                    Nexilis.sharedAudioPlayer?.play()
+                                } catch {
+                                    
+                                }
                             }
-                            Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
-                            Nexilis.sharedAudioPlayer?.prepareToPlay()
-                            Nexilis.sharedAudioPlayer?.play()
                         } catch {
                             
                         }

+ 66 - 11
NexilisLite/NexilisLite/Source/View/Control/NotificationSound.swift

@@ -113,19 +113,74 @@ public class NotificationSound: UIViewController, UITableViewDelegate, UITableVi
         isSelectedSound = data[indexPath.row].id
         //playSound
         var nameSound = data[indexPath.row].name.replacingOccurrences(of: " ", with: "_")
+        var fromPref = false
         if nameSound.contains("_(Default)") {
-            nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
-        }
-        var soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
-        if soundURL == nil {
-            soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+            if !Utils.getDefaultIncomingMsg().isEmpty {
+                nameSound = Utils.getDefaultIncomingMsg()
+                fromPref = true
+            } else {
+                nameSound = nameSound.replacingOccurrences(of: "_(Default)", with: "")
+            }
         }
-        do {
-            Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
-            Nexilis.sharedAudioPlayer?.prepareToPlay()
-            Nexilis.sharedAudioPlayer?.play()
-        } catch {
-            
+        var soundURL: URL?
+        if fromPref {
+            let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
+            let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
+            let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
+            if let dirPath = paths.first {
+                let audioURL = URL(fileURLWithPath: dirPath).appendingPathComponent(nameSound)
+                if !FileManager.default.fileExists(atPath: audioURL.path) && !FileEncryption.shared.isSecureExists(filename: nameSound) {
+                    Download().startHTTP(forKey: nameSound,downloadUrl: Utils.getURLBase() + "filepalio/ringtone/") { (name, progress) in
+                        guard progress == 100 else {
+                            return
+                        }
+                        playAudio()
+                    }
+                } else {
+                    playAudio()
+                }
+                
+                func playAudio() {
+                    if FileManager.default.fileExists(atPath: audioURL.path) {
+                        do {
+                            Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: audioURL)
+                            Nexilis.sharedAudioPlayer?.prepareToPlay()
+                            Nexilis.sharedAudioPlayer?.play()
+                        } catch {
+                            
+                        }
+                    } else if FileEncryption.shared.isSecureExists(filename: nameSound) {
+                        do {
+                            if let audioData = try FileEncryption.shared.readSecure(filename: nameSound) {
+                                let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
+                                let tempPath = cachesDirectory.appendingPathComponent(nameSound)
+                                try audioData.write(to: tempPath)
+                                do {
+                                    Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: tempPath)
+                                    Nexilis.sharedAudioPlayer?.prepareToPlay()
+                                    Nexilis.sharedAudioPlayer?.play()
+                                } catch {
+                                    
+                                }
+                            }
+                        } catch {
+                            
+                        }
+                    }
+                }
+            }
+        } else {
+            soundURL = Bundle.resourceBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+            if soundURL == nil {
+                soundURL = Bundle.resourcesMediaBundle(for: Nexilis.self).url(forResource: nameSound, withExtension: "mp3")
+            }
+            do {
+                Nexilis.sharedAudioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
+                Nexilis.sharedAudioPlayer?.prepareToPlay()
+                Nexilis.sharedAudioPlayer?.play()
+            } catch {
+                
+            }
         }
         tableView.reloadData()
     }