Skip to content

Commit

Permalink
fix yanue#254
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Mar 3, 2020
1 parent d9cc86f commit 90ef0c0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
21 changes: 10 additions & 11 deletions V2rayU/Preference/PreferenceGeneral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,27 @@ final class PreferenceGeneralViewController: NSViewController, PreferencePane {
@IBOutlet weak var autoClearLog: NSButtonCell!
@IBOutlet weak var autoUpdateServers: NSButtonCell!
@IBOutlet weak var autoSelectFastestServer: NSButtonCell!

override func viewDidLoad() {
super.viewDidLoad()
// fix: https://github.com/sindresorhus/Preferences/issues/31
self.preferredContentSize = NSMakeSize(self.view.frame.size.width, self.view.frame.size.height);

let autoLaunchState = UserDefaults.getBool(forKey: .autoLaunch)
let autoCheckVersionState = UserDefaults.getBool(forKey: .autoCheckVersion)
let autoClearLogState = UserDefaults.getBool(forKey: .autoClearLog)
let autoCheckServerState = UserDefaults.getBool(forKey: .autoUpdateServers)
if autoLaunchState {
if UserDefaults.getBool(forKey: .autoLaunch) {
autoLaunch.state = .on
}
if autoCheckVersionState {
if UserDefaults.getBool(forKey: .autoCheckVersion) {
autoCheckVersion.state = .on
}
if autoClearLogState {
if UserDefaults.getBool(forKey: .autoClearLog) {
autoClearLog.state = .on
}
if autoCheckServerState {
if UserDefaults.getBool(forKey: .autoUpdateServers) {
autoUpdateServers.state = .on
}
if UserDefaults.getBool(forKey: .autoSelectFastestServer) {
autoSelectFastestServer.state = .on
}
}

@IBAction func SetAutoLogin(_ sender: NSButtonCell) {
Expand All @@ -64,11 +63,11 @@ final class PreferenceGeneralViewController: NSViewController, PreferencePane {
@IBAction func SetAutoUpdateServers(_ sender: NSButtonCell) {
UserDefaults.setBool(forKey: .autoUpdateServers, value: sender.state == .on)
}

@IBAction func SetAutoSelectFastestServer(_ sender: NSButton) {
UserDefaults.setBool(forKey: .autoSelectFastestServer, value: sender.state == .on)
}

@IBAction func goFeedback(_ sender: NSButton) {
guard let url = URL(string: "https://github.com/yanue/v2rayu/issues") else {
return
Expand Down
40 changes: 34 additions & 6 deletions V2rayU/ping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,23 @@ class PingSpeed: NSObject {
self.pingServers = []

let normalTitle = menuController.statusMenu.item(withTag: 1)?.title ?? "Ping Speed..."
menuController.statusMenu.item(withTag: 1)?.title = "\(normalTitle) - In Testing"
let langStr = Locale.current.languageCode
var pingTip: String = ""
if UserDefaults.getBool(forKey: .autoSelectFastestServer) {
if langStr == "en" {
pingTip = "Ping Speed - In Testing(Choose fastest server)"
} else {
pingTip = "Ping Speed - 测试中(选择最快服务器)"
}
} else {
if langStr == "en" {
pingTip = "Ping Speed - In Testing "
} else {
pingTip = "Ping Speed - 测试中"
}
}
menuController.statusMenu.item(withTag: 1)?.title = pingTip


let queue = DispatchQueue.global()
queue.async {
Expand All @@ -52,13 +68,25 @@ class PingSpeed: NSObject {
// refresh servers
// reload
V2rayServer.loadConfig()

// if auto select fastest server
if UserDefaults.getBool(forKey: .autoSelectFastestServer) && fastV2rayName.count > 0 {
if V2rayServer.getIndex(name: fastV2rayName) > -1 {
// set current
UserDefaults.set(forKey: .v2rayCurrentServerName, value: fastV2rayName)
// stop first
V2rayLaunch.Stop()
// start
menuController.startV2rayCore()
}
}

// refresh server
menuController.showServers()
// reload config
if menuController.configWindow != nil {
menuController.configWindow.serversTableView.reloadData()
}

inPing = false
}
}
Expand Down Expand Up @@ -105,16 +133,16 @@ class PingSpeed: NSObject {
}

func parsePingResult() {
let jsonText = try? String(contentsOfFile: pingJsonFilePath, encoding: String.Encoding.utf8) ?? ""
guard var json = try? JSON(data: jsonText!.data(using: String.Encoding.utf8, allowLossyConversion: false)!) else {
let jsonText = try? String(contentsOfFile: pingJsonFilePath, encoding: String.Encoding.utf8)
guard let json = try? JSON(data: (jsonText ?? "").data(using: String.Encoding.utf8, allowLossyConversion: false)!) else {
return
}

var pingResHash: Dictionary<String, String> = [:]
if json.arrayValue.count > 0 {
for val in json.arrayValue {
var name = val["name"].stringValue
var ping = val["ping"].stringValue
let name = val["name"].stringValue
let ping = val["ping"].stringValue
pingResHash[name] = ping
}
}
Expand Down
4 changes: 4 additions & 0 deletions V2rayU/zh-HK.lproj/PreferenceGeneral.strings
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@

/* Class = "NSTextFieldCell"; title = "Toggle V2ray On/Off:"; ObjectID = "0rn-re-Wn3"; */
"0rn-re-Wn3.title" = "打开关闭代理:";
/* Class = "NSButtonCell"; title = "Automatically select the fastest server by ping"; ObjectID = "Kbp-GI-fkh"; */
"Kbp-GI-fkh.title" = "自动通过ping选择最快server";
/* Class = "NSButtonCell"; title = "Automatically update servers from subscriptions"; ObjectID = "1FR-Yu-tqb"; */
"1FR-Yu-tqb.title" = "启动或唤醒时自动从订阅中更新";
2 changes: 2 additions & 0 deletions V2rayU/zh-Hans.lproj/PreferenceGeneral.strings
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
"0rn-re-Wn3.title" = "打开关闭代理:";
/* Class = "NSButtonCell"; title = "Automatically update servers from subscriptions"; ObjectID = "1FR-Yu-tqb"; */
"1FR-Yu-tqb.title" = "启动或唤醒时自动从订阅中更新";
/* Class = "NSButtonCell"; title = "Automatically select the fastest server by ping"; ObjectID = "Kbp-GI-fkh"; */
"Kbp-GI-fkh.title" = "自动通过ping选择最快server";

0 comments on commit 90ef0c0

Please sign in to comment.