Add file watch for iCloud

This commit is contained in:
yicheng 2020-06-02 19:05:32 +08:00
parent 256f045edc
commit 351641c0ff
6 changed files with 22 additions and 35 deletions

View File

@ -108,7 +108,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
updateLoggingLevel()
// start watch config file change
ConfigFileManager.shared.watchConfigFile(configName: ConfigManager.selectConfigName)
ConfigManager.watchCurrentConfigFile()
RemoteConfigManager.shared.autoUpdateCheck()

View File

@ -109,9 +109,8 @@ class ApiRequest {
let filePath = Paths.localConfigPath(for: configName)
requestConfigUpdate(configPath: filePath, callback: callback)
}
}
static func requestConfigUpdate(configPath: String, callback: @escaping ((ErrorString?) -> Void)) {
let placeHolderErrorDesp = "Error occoured, Please try to fix it by restarting ClashX. "

View File

@ -19,8 +19,7 @@ class ConfigFileManager {
pause = true
}
func watchConfigFile(configName: String) {
let path = Paths.localConfigPath(for: configName)
func watchFile(path: String) {
witness = Witness(paths: [path], flags: .FileEvents, latency: 0.3) {
[weak self] events in
guard let self = self else { return }
@ -39,7 +38,7 @@ class ConfigFileManager {
}
}
}
func stopWatchConfigFile() {
witness = nil
pause = false

View File

@ -48,11 +48,19 @@ class ConfigManager {
}
set {
UserDefaults.standard.set(newValue, forKey: "selectConfigName")
if iCloudManager.shared.isICloudEnable() {
iCloudManager.shared.watchConfigFile(name: newValue)
} else {
ConfigFileManager.shared.watchConfigFile(configName: newValue)
watchCurrentConfigFile()
}
}
static func watchCurrentConfigFile() {
if iCloudManager.shared.isICloudEnable() {
iCloudManager.shared.getUrl { url in
guard let url = url else { return }
let configUrl = url.appendingPathComponent(Paths.configFileName(for: selectConfigName))
ConfigFileManager.shared.watchFile(path: configUrl.path)
}
} else {
ConfigFileManager.shared.watchFile(path: Paths.localConfigPath(for: selectConfigName))
}
}

View File

@ -172,14 +172,13 @@ class RemoteConfigManager {
}
config.isPlaceHolderName = false
if iCloudManager.shared.isICloudEnable() {
ConfigFileManager.shared.stopWatchConfigFile()
ConfigFileManager.shared.stopWatchConfigFile()
} else if config.name == ConfigManager.selectConfigName {
ConfigFileManager.shared.pauseForNextChange()
}
let saveAction:((String)->Void) = {
let saveAction: ((String) -> Void) = {
savePath in
do {
if FileManager.default.fileExists(atPath: savePath) {
@ -191,10 +190,10 @@ class RemoteConfigManager {
complete?(err.localizedDescription)
}
}
if iCloudManager.shared.isICloudEnable() {
iCloudManager.shared.getUrl { url in
guard let url = url else {return}
guard let url = url else { return }
let saveUrl = url.appendingPathComponent(Paths.configFileName(for: config.name))
saveAction(saveUrl.path)
}
@ -204,7 +203,6 @@ class RemoteConfigManager {
}
}
}
static func verifyConfig(string: String) -> ErrorString? {
let res = verifyClashConfig(string.goStringBuffer())?.toString() ?? "unknown error"

View File

@ -56,7 +56,7 @@ class iCloudManager {
return
}
let files = try? FileManager.default.contentsOfDirectory(atPath: url.path)
if let count = files?.count,count == 0 {
if let count = files?.count, count == 0 {
let path = Bundle.main.path(forResource: "sampleConfig", ofType: "yaml")!
try? FileManager.default.copyItem(atPath: path, toPath: kDefaultConfigFilePath)
try? FileManager.default.copyItem(atPath: Bundle.main.path(forResource: "sampleConfig", ofType: "yaml")!, toPath: url.appendingPathComponent("config.yaml").path)
@ -99,26 +99,9 @@ class iCloudManager {
NotificationCenter.default.addObserver(self, selector: #selector(iCloudAccountAvailabilityChanged), name: NSNotification.Name.NSUbiquityIdentityDidChange, object: nil)
}
func watchConfigFile(name: String) {
metaQuery?.stop()
NotificationCenter.default.removeObserver(self, name: .NSMetadataQueryDidUpdate, object: metaQuery)
metaQuery = nil
let query = NSMetadataQuery()
query.searchScopes = [NSMetadataQueryUbiquitousDocumentsScope]
query.predicate = NSPredicate(format: "%K like %@", NSMetadataItemFSNameKey, "\(name).yaml")
if query.start() {
NotificationCenter.default.addObserver(self, selector: #selector(fileDidUpdate(_:)), name: .NSMetadataQueryDidUpdate, object: query)
metaQuery = query
}
}
@objc func iCloudAccountAvailabilityChanged() {
icloudAvailable = isICloudAvailable()
}
@objc func fileDidUpdate(_ note:NSNotification) {
print("fileDidUpdate")
}
}
extension iCloudManager {