Skip to content

Commit

Permalink
Add remove proxy server
Browse files Browse the repository at this point in the history
  • Loading branch information
ruqli committed Nov 17, 2016
1 parent 7dd2c9d commit 1303442
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
26 changes: 18 additions & 8 deletions Potatso/Core/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>] -> 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<String, String>] {
callback(JSON)
}
}
}
}

extension RuleSet: Mappable {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 39 additions & 1 deletion Potatso/ProxyListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ private let kProxyCellIdentifier = "proxy"
class ProxyListViewController: FormViewController {

var proxies: [Proxy?] = []
var cloudProxies: [Proxy] = []

let allowNone: Bool
let chooseCallback: (Proxy? -> Void)?

Expand All @@ -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()
Expand All @@ -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 () {
Expand All @@ -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()
}
Expand Down

0 comments on commit 1303442

Please sign in to comment.