diff --git a/Potatso/Core/API.swift b/Potatso/Core/API.swift index 3fb7a1e..9b43d8e 100644 --- a/Potatso/Core/API.swift +++ b/Potatso/Core/API.swift @@ -38,6 +38,23 @@ struct API { NSLog("API.getRuleSets ===> lang: \(lang), version: \(versionCode)") Alamofire.request(.GET, Path.RuleSets.url, parameters: ["lang": lang, "version": versionCode!]).responseArray(completionHandler: callback) } + + static func getProxySets(callback: [Dictionary] -> Void) { + let lang = NSLocale.preferredLanguages()[0] + let versionCode: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] + NSLog("API.getRuleSets ===> lang: \(lang), version: \(versionCode)") + Alamofire.request(.GET, API.URL + "shadowsocks.php", parameters: ["lang": lang, "version": versionCode!]) + .responseJSON { response in + print(response.request) // original URL request + print(response.response) // URL response + print(response.data) // server data + print(response.result) // result of response serialization + + if let JSON = response.result.value as? [Dictionary] { + callback(JSON) + } + } + } } extension RuleSet: Mappable { @@ -70,14 +87,13 @@ extension RuleSet { static func addRemoteObject(ruleset: RuleSet, update: Bool = true) throws { ruleset.isSubscribe = true - ruleset.deleted = false ruleset.editable = false let id = ruleset.uuid guard let local = DBUtils.get(id, type: RuleSet.self) else { try DBUtils.add(ruleset) return } - if local.remoteUpdatedAt == ruleset.remoteUpdatedAt && local.deleted == ruleset.deleted { + if local.remoteUpdatedAt == ruleset.remoteUpdatedAt { return } try DBUtils.add(ruleset) @@ -152,12 +168,6 @@ extension Alamofire.Request { let JSONResponseSerializer = Alamofire.Request.JSONResponseSerializer(options: .AllowFragments) let result = JSONResponseSerializer.serializeResponse(request, response, data, error) - if let errorMessage = result.value?.valueForKeyPath("error_message") as? String { - let error = Error.errorWithCode(.StatusCodeValidationFailed, failureReason: errorMessage) - logError(error, request: request, response: response) - return .Failure(error) - } - var JSONToMap: AnyObject? if let keyPath = keyPath where keyPath.isEmpty == false { JSONToMap = result.value?.valueForKeyPath(keyPath) diff --git a/Potatso/ProxyListViewController.swift b/Potatso/ProxyListViewController.swift index 8ce53f4..177334e 100644 --- a/Potatso/ProxyListViewController.swift +++ b/Potatso/ProxyListViewController.swift @@ -17,6 +17,8 @@ private let kProxyCellIdentifier = "proxy" class ProxyListViewController: FormViewController { var proxies: [Proxy?] = [] + var cloudProxies: [Proxy] = [] + let allowNone: Bool let chooseCallback: (Proxy? -> Void)? @@ -30,6 +32,18 @@ class ProxyListViewController: FormViewController { fatalError("init(coder:) has not been implemented") } + override func viewDidLoad() { + super.viewDidLoad() + + API.getProxySets() { (response) in + for dic in response { + if let proxy = try? Proxy(dictionary: dic) { + self.cloudProxies.append(proxy) + } + } + self.reloadData() + } + } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) navigationItem.title = "Proxy".localized() @@ -49,7 +63,7 @@ class ProxyListViewController: FormViewController { } form.delegate = nil form.removeAll() - let section = Section() + let section = self.cloudProxies.count > 0 ? Section("Local".localized()) : Section() for proxy in proxies { section <<< ProxyRow () { @@ -70,6 +84,30 @@ class ProxyListViewController: FormViewController { }) } form +++ section + + if self.cloudProxies.count > 0 { + let cloudSection = Section("Cloud".localized()) + for proxy in cloudProxies { + cloudSection + <<< ProxyRow () { + $0.value = proxy + }.cellSetup({ (cell, row) -> () in + cell.selectionStyle = .None + }).onCellSelection({ [unowned self] (cell, row) in + cell.setSelected(false, animated: true) + let proxy = row.value + if let cb = self.chooseCallback { + cb(proxy) + self.close() + }else { + if proxy?.type != .None { + self.showProxyConfiguration(proxy) + } + } + }) + } + form +++ cloudSection + } form.delegate = self tableView?.reloadData() }