Browse Source

show random background from BE

alqindiirsyam 2 năm trước cách đây
mục cha
commit
571d8d3d3b

+ 6 - 0
appbuilder-ios/AppBuilder/AppBuilder/AppDelegate.swift

@@ -146,6 +146,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
                             if let iconSS = j["app_builder_icon_ss"] as? String {
                                 PrefsUtil.setIconSS(value: iconSS)
                             }
+                            if let background = j["app_builder_background"] as? String {
+                                PrefsUtil.setBackground(value: background)
+                            }
+                            if let background = j["app_builder_compressor"] as? String {
+                                PrefsUtil.setCompressor(value: background)
+                            }
                         }
                     }
                 }

+ 15 - 0
appbuilder-ios/AppBuilder/AppBuilder/FourthTabViewController.swift

@@ -247,6 +247,21 @@ public class FourthTabViewController: UIViewController, UITableViewDelegate, UIT
     
     override public func viewWillAppear(_ animated: Bool) {
         backgroundImage.backgroundColor = .white
+        backgroundImage.image = nil
+        DispatchQueue.global().async {
+            if let listBg = PrefsUtil.getBackground() {
+                var bgChoosen = ""
+                let arrayBg = listBg.split(separator: ",")
+                bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
+                ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase()! + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
+                    guard let data = data, error == nil else { return }
+                    // always update the UI from the main thread
+                    DispatchQueue.main.async() { [self] in
+                        backgroundImage.image = UIImage(data: data)!
+                    }
+                }
+            }
+        }
         if notInTab {
             self.title = "Settings".localized()
             self.navigationController?.navigationBar.topItem?.title = ""

+ 12 - 1
appbuilder-ios/AppBuilder/AppBuilder/PrefsUtil.swift

@@ -61,6 +61,12 @@ class PrefsUtil {
     static func getIconSS() -> String? {
         return UserDefaults.standard.string(forKey: "app_builder_icon_ss")
     }
+    static func getBackground() -> String? {
+        return UserDefaults.standard.string(forKey: "app_builder_background")
+    }
+    static func getCompressor() -> String? {
+        return UserDefaults.standard.string(forKey: "app_builder_compressor")
+    }
     static func getUrlSS() -> String? {
         return PrefsUtil.getURLBase()! + "dashboardv2/uploads/splashscreen/" + PrefsUtil.getIconSS()!
     }
@@ -87,7 +93,12 @@ class PrefsUtil {
     static func setIconSS(value: String) {
         UserDefaults.standard.set(value, forKey: "app_builder_icon_ss")
     }
-    
+    static func setBackground(value: String) {
+        UserDefaults.standard.set(value, forKey: "app_builder_background")
+    }
+    static func setCompressor(value: String) {
+        UserDefaults.standard.set(value, forKey: "app_builder_compressor")
+    }
     static func getTerms() -> Bool {
         return UserDefaults.standard.bool(forKey: "terms_app")
     }

+ 14 - 0
appbuilder-ios/AppBuilder/AppBuilder/SecondTabViewController.swift

@@ -301,6 +301,20 @@ class SecondTabViewController: UIViewController, UIScrollViewDelegate, UIGesture
         let lang = UserDefaults.standard.string(forKey: "i18n_language")
         speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: lang ?? "en"))
         backgroundImage.backgroundColor = .white
+        DispatchQueue.global().async {
+            if let listBg = PrefsUtil.getBackground() {
+                var bgChoosen = ""
+                let arrayBg = listBg.split(separator: ",")
+                bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
+                ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase()! + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
+                    guard let data = data, error == nil else { return }
+                    // always update the UI from the main thread
+                    DispatchQueue.main.async() { [self] in
+                        backgroundImage.image = UIImage(data: data)!
+                    }
+                }
+            }
+        }
         if segment.numberOfSegments == 2 {
             segment.setTitle("Chats".localized(), forSegmentAt: 0)
             segment.setTitle("Groups".localized(), forSegmentAt: 1)

+ 58 - 32
appbuilder-ios/AppBuilder/AppBuilder/ViewController.swift

@@ -127,10 +127,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
             let menu = UIMenu(title: "", children: childrenMenu)
             if PrefsUtil.getIconDock() != nil {
                 DispatchQueue.global().async {
-                    let data = try? Data(contentsOf: URL(string: PrefsUtil.getUrlDock()!)!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                    DispatchQueue.main.async {
-                        if data != nil {
-                            self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: self.resizeImage(image: UIImage(data: data!)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
+                    ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
+                        guard let data = data, error == nil else { return }
+                        // always update the UI from the main thread
+                        DispatchQueue.main.async() { [self] in
+                            navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(data: data)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
                         }
                     }
                 }
@@ -328,10 +329,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
         ViewController.middleButton = UIButton(frame: CGRect(x: buttonCenterX - 50 , y: buttonCenterY - 55, width: 100, height: 100))
         if PrefsUtil.getIconDock() != nil {
             DispatchQueue.global().async {
-                let data = try? Data(contentsOf: URL(string: PrefsUtil.getUrlDock()!)!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                DispatchQueue.main.async {
-                    if data != nil {
-                        ViewController.middleButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
+                    guard let data = data, error == nil else { return }
+                    // always update the UI from the main thread
+                    DispatchQueue.main.async() {
+                        ViewController.middleButton.setBackgroundImage(UIImage(data: data), for: .normal)
                     }
                 }
             }
@@ -396,10 +398,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
             let menu = UIMenu(title: "", children: childrenMenu)
             if PrefsUtil.getIconDock() != nil {
                 DispatchQueue.global().async {
-                    let data = try? Data(contentsOf: URL(string: PrefsUtil.getUrlDock()!)!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                    DispatchQueue.main.async {
-                        if data != nil {
-                            self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: self.resizeImage(image: UIImage(data: data!)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
+                    ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getUrlDock()!)!) { data, response, error in
+                        guard let data = data, error == nil else { return }
+                        // always update the UI from the main thread
+                        DispatchQueue.main.async() { [self] in
+                            navigationItem.rightBarButtonItem = UIBarButtonItem(image: resizeImage(image: UIImage(data: data)!, targetSize: CGSize(width: 25, height: 25)).withRenderingMode(.alwaysOriginal), primaryAction: .none, menu: menu)
                         }
                     }
                 }
@@ -450,6 +453,20 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
     func showWelocomeView() {
         if let viewWelcome = welcomeVC.view {
             viewWelcome.backgroundColor = .white
+            DispatchQueue.global().async {
+                if let listBg = PrefsUtil.getBackground() {
+                    var bgChoosen = ""
+                    let arrayBg = listBg.split(separator: ",")
+                    bgChoosen = String(arrayBg[Int.random(in: 0..<arrayBg.count)])
+                    ViewController.getDataImageFromUrl(from: URL(string: PrefsUtil.getURLBase()! + "/dashboardv2/uploads/background/" + bgChoosen)!) { data, response, error in
+                        guard let data = data, error == nil else { return }
+                        // always update the UI from the main thread
+                        DispatchQueue.main.async() {
+                            viewWelcome.backgroundColor = UIColor(patternImage: UIImage(data: data)!)
+                        }
+                    }
+                }
+            }
             let welcomeTitle = UILabel()
             welcomeTitle.text = "Welcome to".localized() + " " + (Bundle.main.displayName ?? "")
             welcomeTitle.font = .systemFont(ofSize: 25, weight: .bold)
@@ -808,10 +825,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
                                             if count == 0 {
                                                 if !icon.isEmpty {
                                                     DispatchQueue.global().async {
-                                                        let data = try? Data(contentsOf: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                                                        DispatchQueue.main.async {
-                                                            if data != nil {
-                                                                ViewController.chatButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                                                        ViewController.getDataImageFromUrl(from: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) { data, response, error in
+                                                            guard let data = data, error == nil else { return }
+                                                            // always update the UI from the main thread
+                                                            DispatchQueue.main.async() {
+                                                                ViewController.chatButton.setBackgroundImage(UIImage(data: data), for: .normal)
                                                             }
                                                         }
                                                     }
@@ -821,10 +839,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
                                             } else if count == 1 {
                                                 if !icon.isEmpty {
                                                     DispatchQueue.global().async {
-                                                        let data = try? Data(contentsOf: URL(string: "https://\(Nexilis.ADDRESS)/filepalio/image/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                                                        DispatchQueue.main.async {
-                                                            if data != nil {
-                                                                ViewController.callButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                                                        ViewController.getDataImageFromUrl(from: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) { data, response, error in
+                                                            guard let data = data, error == nil else { return }
+                                                            // always update the UI from the main thread
+                                                            DispatchQueue.main.async() {
+                                                                ViewController.callButton.setBackgroundImage(UIImage(data: data), for: .normal)
                                                             }
                                                         }
                                                     }
@@ -834,10 +853,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
                                             } else if count == 2 {
                                                 if !icon.isEmpty {
                                                     DispatchQueue.global().async {
-                                                        let data = try? Data(contentsOf: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                                                        DispatchQueue.main.async {
-                                                            if data != nil {
-                                                                ViewController.ccButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                                                        ViewController.getDataImageFromUrl(from: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) { data, response, error in
+                                                            guard let data = data, error == nil else { return }
+                                                            // always update the UI from the main thread
+                                                            DispatchQueue.main.async() {
+                                                                ViewController.ccButton.setBackgroundImage(UIImage(data: data), for: .normal)
                                                             }
                                                         }
                                                     }
@@ -847,10 +867,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
                                             } else if count == 3 {
                                                 if !icon.isEmpty {
                                                     DispatchQueue.global().async {
-                                                        let data = try? Data(contentsOf: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                                                        DispatchQueue.main.async {
-                                                            if data != nil {
-                                                                ViewController.postButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                                                        ViewController.getDataImageFromUrl(from: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) { data, response, error in
+                                                            guard let data = data, error == nil else { return }
+                                                            // always update the UI from the main thread
+                                                            DispatchQueue.main.async() {
+                                                                ViewController.postButton.setBackgroundImage(UIImage(data: data), for: .normal)
                                                             }
                                                         }
                                                     }
@@ -860,10 +881,11 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
                                             } else if count == 4 {
                                                 if !icon.isEmpty {
                                                     DispatchQueue.global().async {
-                                                        let data = try? Data(contentsOf: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
-                                                        DispatchQueue.main.async {
-                                                            if data != nil {
-                                                                ViewController.streamingButton.setBackgroundImage(UIImage(data: data!), for: .normal)
+                                                        ViewController.getDataImageFromUrl(from: URL(string: "http://202.158.33.26/filepalio/image/\(icon)")!) { data, response, error in
+                                                            guard let data = data, error == nil else { return }
+                                                            // always update the UI from the main thread
+                                                            DispatchQueue.main.async() {
+                                                                ViewController.streamingButton.setBackgroundImage(UIImage(data: data), for: .normal)
                                                             }
                                                         }
                                                     }
@@ -976,6 +998,10 @@ class ViewController: UITabBarController, UITabBarControllerDelegate, SettingMAB
 //        ViewController.postButton.removeFromSuperview()
         ViewController.middleButton.isHidden = true
     }
+    
+    public static func getDataImageFromUrl(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
+        URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
+    }
 
 }