Optimize: ask to confirm when not api port found

This commit is contained in:
yicheng 2018-09-05 19:30:38 +08:00
parent 3f07277111
commit 98770c23d0

View File

@ -65,22 +65,29 @@ class ConfigFileFactory {
static func saveToClashConfigFile(str:String) {
// save to ~/.config/clash/config.ini
self.backupAndRemoveConfigFile()
self.backupAndRemoveConfigFile(showAlert: false)
try? str.write(to: URL(fileURLWithPath: kConfigFilePath), atomically: true, encoding: .utf8)
}
static func backupAndRemoveConfigFile() {
static func backupAndRemoveConfigFile(showAlert:Bool = false) -> Bool {
let path = kConfigFilePath
if (showAlert) {
if (!self.showReplacingConfigFileAlert()) {
return false;
}
}
if (FileManager.default.fileExists(atPath: path)) {
let newPath = "\(kConfigFolderPath)config_\(Date().timeIntervalSince1970).ini"
try? FileManager.default.moveItem(atPath: path, toPath: newPath)
}
return true
}
static func copySimpleConfigFile() {
if (!backupAndRemoveConfigFile(showAlert: true)) {
return;
}
let path = Bundle.main.path(forResource: "initConfig", ofType: "ini")!
backupAndRemoveConfigFile()
try? FileManager.default.copyItem(atPath: path, toPath: kConfigFilePath)
NSUserNotificationCenter.default.postGenerateSimpleConfigNotice()
}
@ -196,4 +203,17 @@ class ConfigFileFactory {
self.saveToClashConfigFile(str: configStr)
NotificationCenter.default.post(Notification(name: kShouldUpDateConfig))
}
static func showReplacingConfigFileAlert() -> Bool{
let alert = NSAlert()
alert.messageText = """
Can't Reconglized your config file. We will backup and replace your config file in your config folder.
Otherwise the functions of ClashX will not work properly. You may need to restart ClashX or reload Config manually.
"""
alert.alertStyle = .warning
alert.addButton(withTitle: "Replace")
alert.addButton(withTitle: "Cancel")
return alert.runModal() == .alertFirstButtonReturn
}
}