Skip to content

Commit

Permalink
Feature: add speedtest sort
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Sep 23, 2018
1 parent 5722730 commit c27aceb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ClashX/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="actionSpeedTest:" target="Voe-Tx-rLC" id="deV-3n-Mtx"/>
<segue destination="gtD-dn-7Dy" kind="modal" id="opl-HQ-q26"/>
<segue destination="gtD-dn-7Dy" kind="show" id="opl-HQ-q26"/>
</connections>
</menuItem>
<menuItem title="Start at login" id="B1J-XB-BiZ">
Expand Down
8 changes: 2 additions & 6 deletions ClashX/General/ApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,14 @@ class ApiRequest{
}
}

static func getProxyDelay(proxyName:String,callback:@escaping ((String)->())) {
static func getProxyDelay(proxyName:String,callback:@escaping ((Int)->())) {
let proxyNameEncoded = proxyName.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""

request(ConfigManager.apiUrl + "/proxies/\(proxyNameEncoded)/delay"
, method: .get
, parameters: ["timeout":5000,"url":"http://www.gstatic.com/generate_204"])
.responseJSON { (res) in let json = JSON(res.result.value ?? [])
if let delay = json["delay"].int {
callback("\(delay)")
} else {
callback("fail")
}
callback(json["delay"].int ?? -1)
}
}
}
37 changes: 30 additions & 7 deletions ClashX/ViewControllers/SpeedTestViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ class SpeedTestViewController: NSViewController {

@IBOutlet weak var tableView: NSTableView!

class ProxyModel {
var name:String
var delay:String?
@objc class ProxyModel:NSObject {
@objc var name:String
@objc var delay:Int

init(_ name:String) {
self.name = name
self.delay = -2
}
}

Expand All @@ -28,7 +29,24 @@ class SpeedTestViewController: NSViewController {
self.setupData()
}

override func viewWillAppear() {
super.viewWillAppear()
NSApp.activate(ignoringOtherApps: true)
}

func setupData() {

for columns in self.tableView.tableColumns {
let sortKey:String
if columns.identifier.rawValue == "ProxyNameCell" {
sortKey = "name"
} else {
sortKey = "delay"
}
let sortDescriptor = NSSortDescriptor(key: sortKey, ascending: true)
columns.sortDescriptorPrototype = sortDescriptor
}

ApiRequest.getAllProxyList { [unowned self](proxies) in
for proxyName in proxies {
self.proxies.append(ProxyModel(proxyName))
Expand Down Expand Up @@ -78,10 +96,12 @@ extension SpeedTestViewController:NSTableViewDataSource {
let textField = cell?.viewWithTag(1) as! NSTextField
let model = proxies[row]

if let delay = model.delay {
textField.stringValue = delay
} else {
if (model.delay == -2) {
textField.stringValue = "testing"
} else if (model.delay == -1) {
textField.stringValue = "fail"
} else {
textField.stringValue = "\(model.delay)"
}
return cell
}
Expand All @@ -90,6 +110,9 @@ extension SpeedTestViewController:NSTableViewDataSource {
}

extension SpeedTestViewController:NSTableViewDelegate{

func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
self.proxies = (self.proxies as NSArray).sortedArray(using: tableView.sortDescriptors) as! [SpeedTestViewController.ProxyModel]
tableView.reloadData()
}
}

0 comments on commit c27aceb

Please sign in to comment.