improve: unified path entry

This commit is contained in:
yicheng 2020-04-11 11:30:48 +08:00
parent 204861eb88
commit 4474554b16
6 changed files with 19 additions and 19 deletions

View File

@ -146,14 +146,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
func setupData() {
ConfigManager.shared
.showNetSpeedIndicatorObservable.skip(1)
.bind {
_ in
ApiRequest.shared.resetTrafficStreamApi()
}.disposed(by: disposeBag)
}.disposed(by: disposeBag)
Observable
.merge([ConfigManager.shared.proxyPortAutoSetObservable,
ConfigManager.shared.isProxySetByOtherVariable.asObservable()])

View File

@ -32,7 +32,8 @@ extension NSUserNotificationCenter {
}
func postConfigErrorNotice(msg: String) {
let configName = ConfigManager.selectConfigName.count > 0 ? "\(ConfigManager.selectConfigName).yaml" : ""
let configName = ConfigManager.selectConfigName.count > 0 ?
Paths.configFileName(for: ConfigManager.selectConfigName) : ""
let message = "\(configName): \(msg)"
post(title: NSLocalizedString("Config loading Fail!", comment: ""), info: message)

View File

@ -94,7 +94,7 @@ class ApiRequest {
}
static func requestConfigUpdate(configName: String, callback: @escaping ((ErrorString?) -> Void)) {
let filePath = "\(kConfigFolderPath)\(configName).yaml"
let filePath = Paths.configPath(for: configName)
let placeHolderErrorDesp = "Error occoured, Please try to fix it by restarting ClashX. "
// DEV MODE: Use API
@ -264,12 +264,12 @@ extension ApiRequest {
resetLogStreamApi()
resetTrafficStreamApi()
}
func resetLogStreamApi() {
loggingWebSocketRetryCount = 0
requestLog()
}
func resetTrafficStreamApi() {
trafficWebSocketRetryCount = 0
requestTrafficInfo()

View File

@ -20,7 +20,7 @@ class ConfigFileManager {
}
func watchConfigFile(configName: String) {
let path = "\(kConfigFolderPath)\(configName).yaml"
let path = Paths.configPath(for: configName)
witness = Witness(paths: [path], flags: .FileEvents, latency: 0.3) {
[weak self] events in
guard let self = self else { return }
@ -40,16 +40,6 @@ class ConfigFileManager {
}
}
@discardableResult
static func backupAndRemoveConfigFile() -> Bool {
let path = kDefaultConfigFilePath
if FileManager.default.fileExists(atPath: path) {
let newPath = "\(kConfigFolderPath)config_\(Date().timeIntervalSince1970).yaml"
try? FileManager.default.moveItem(atPath: path, toPath: newPath)
}
return true
}
static func copySampleConfigIfNeed() {
if !FileManager.default.fileExists(atPath: kDefaultConfigFilePath) {
let path = Bundle.main.path(forResource: "sampleConfig", ofType: "yaml")!

View File

@ -170,7 +170,7 @@ class RemoteConfigManager {
}
config.isPlaceHolderName = false
let savePath = kConfigFolderPath.appending(config.name).appending(".yaml")
let savePath = Paths.configPath(for: config.name)
if config.name == ConfigManager.selectConfigName {
ConfigFileManager.shared.pauseForNextChange()

View File

@ -14,3 +14,13 @@ let kDefaultConfigFilePath = "\(kConfigFolderPath)config.yaml"
var kCurrentConfigPath: String {
return "\(kConfigFolderPath)\(ConfigManager.selectConfigName).yaml"
}
struct Paths {
static func configPath(for name: String) -> String {
return "\(kConfigFolderPath)\(configFileName(for: name))"
}
static func configFileName(for name: String) -> String {
return "\(name).yaml"
}
}