Skip to content

Commit

Permalink
Nâng version, hỗ trợ map json từ key tool
Browse files Browse the repository at this point in the history
  • Loading branch information
oneweekstudio committed Nov 12, 2019
1 parent b53abe4 commit f52ee45
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
19 changes: 15 additions & 4 deletions Example/SMRemote/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion SMRemote.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions SMRemote/Classes/SMRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f52ee45

Please sign in to comment.