diff --git a/Example/SMRemote/ViewController.swift b/Example/SMRemote/ViewController.swift index 1f20215..8d1a24e 100644 --- a/Example/SMRemote/ViewController.swift +++ b/Example/SMRemote/ViewController.swift @@ -28,8 +28,14 @@ class ViewController: UIViewController { } func loadConfig() { - SMRemote.sharedInstance.load(smConfig: Dev()) { success in - print("Tải thành công config từ remote") + //deprecated +// SMRemote.sharedInstance.load(smConfig: Dev()) { success in +// print("Tải thành công config từ remote") +// } + + SMRemote.sharedInstance.loadConfig(smConfig: Dev()) { (optionalJson) in + guard let json = optionalJson else { return } + print(json) } } @@ -73,6 +79,11 @@ class ViewController: UIViewController { //Tạo 1 class kế thừa từ SMConfig open class Dev : SMRemoteConfig { - @objc var custom_property = 1 - @objc var banner_home = 1 +// @deprecated +// @objc var custom_property = 1 +// @objc var banner_home = 1 + + @objc var home_click_full_start = 0 + @objc var home_click_full_loop = 0 + @objc var banner_home = 0 } diff --git a/SMRemote.podspec b/SMRemote.podspec index 23de149..750d404 100644 --- a/SMRemote.podspec +++ b/SMRemote.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SMRemote' - s.version = '2.1.1' + s.version = '3.0.0' s.summary = 'Sản phẩm thuộc về ONEWEEK STUDIO' s.swift_versions = '4.2' # This description is used to generate tags and improve search results. diff --git a/SMRemote/Classes/SMRemote.swift b/SMRemote/Classes/SMRemote.swift index 3ff693a..1443ad9 100644 --- a/SMRemote/Classes/SMRemote.swift +++ b/SMRemote/Classes/SMRemote.swift @@ -24,6 +24,7 @@ open class SMRemote : NSObject { }() //Load + @available(*, deprecated, renamed: "loadConfig") open func load( smConfig: SMRemoteConfig, completionHandler: ((Bool) -> Void)?) { remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void in if status == .success { @@ -39,6 +40,24 @@ open class SMRemote : NSObject { } } + open func loadConfig( smConfig: SMRemoteConfig, completionHandler: ((Any?) -> Void)?) { + remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void in + if status == .success { + print("Config fetched!") + self.remoteConfig.activate(completionHandler: nil) + self.setToolConfig(smConfig) { (json) in + completionHandler?(json) + } + completionHandler?(nil) + } else { + completionHandler?(nil) + print("Config not fetched") + print("Error: \(error?.localizedDescription ?? "No error available.")") + } + } + } + + @available(*, deprecated, renamed: "setToolConfig") private func setConfig(_ config : SMRemoteConfig) { if let quangcao = self.remoteConfig["quangcao"].jsonValue as? [String: Any]{ @@ -79,6 +98,55 @@ open class SMRemote : NSObject { } + + private func setToolConfig(_ config : SMRemoteConfig, completionHandler:@escaping (Any) -> Void) { + + if let quangcao = self.remoteConfig["quangcao"].jsonValue as? [String: Any]{ + print("Quảng cáo :\n \(quangcao)") + SMAdsManager.shared.quangcao = AdsModel(quangcao) + } + print("JSON key Config:") + print(remoteConfig["tools"].jsonValue ?? "None") + guard let json = remoteConfig["tools"].jsonValue as? [String:Any] else {return } + + let mirror = Mirror.init(reflecting: config) + if let mirror_super = mirror.superclassMirror { + for i in mirror_super.children { + guard let key = i.label else { return } + if let value = json[key] as? Int { + print("Super: nhận về key \(key) value \(value)") + self.set(key: key + adsPrefix, value: value) + if key == "ad_dialog_loop" { + print("Super: Không update counter của ad_dialog") + if self.getCounter(key: key) == 0 { + self.set(key: key + adsPrefixCounter, value: 1) + } + } else if key == "ad_dialog_start" { + print("Super: Không update counter của ad_dialog") + if self.getCounter(key: key) == 0 { + self.set(key: key + adsPrefixCounter, value: 1) + } + } else { + print("Super set key : \(key)") + self.set(key: key + adsPrefixCounter, value: 1) + } + } else { + print("Không nhận giá trị của key \(key)") + } + } + } + + for i in mirror.children { + guard let key = i.label else { return } + guard let value = json[key] as? Int else { return } + print("Child: nhận về key \(key) value \(value)") + self.set(key: key + adsPrefix, value: value) + self.set(key: key + adsPrefixCounter, value: 1) + } + + completionHandler(json) + } + fileprivate func set(key:String, value: Any) { UserDefaults.standard.setValue(value, forKey: key) UserDefaults.standard.synchronize()