1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

♻️ Rework loading of custom preferences

This commit is contained in:
2022-01-04 00:32:30 +01:00
parent 0cdeeec0a4
commit 3c3a0c8b45

View File

@ -32,26 +32,7 @@ class Preferences {
Preferences.handleFirstTimeLaunch() Preferences.handleFirstTimeLaunch()
cachedPreferences = Self.cache() cachedPreferences = Self.cache()
customPreferences = CustomPrefs(scanApps: []) customPreferences = CustomPrefs(scanApps: [])
loadCustomPreferences()
let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.phpmon.conf.json")
if Filesystem.fileExists(url.path) {
Log.info("A custom .phpmon.conf.json file was found. Attempting to parse...")
loadCustomPreferencesFile(url)
} else {
Log.info("There was no .phpmon.conf.json file to be loaded.")
}
}
private func loadCustomPreferencesFile(_ url: URL) {
do {
customPreferences = try JSONDecoder().decode(
CustomPrefs.self,
from: try! String(contentsOf: url, encoding: .utf8).data(using: .utf8)!
)
Log.info("The .phpmon.conf.json file was successfully parsed.")
} catch {
Log.warn("The .phpmon.conf.json file seems to be missing or malformed.")
}
} }
// MARK: - First Time Run // MARK: - First Time Run
@ -133,4 +114,28 @@ class Preferences {
Preferences.shared.cachedPreferences = Preferences.cache() Preferences.shared.cachedPreferences = Preferences.cache()
} }
// MARK: - Custom Preferences
private func loadCustomPreferences() {
let url = URL(fileURLWithPath: "/Users/\(Paths.whoami)/.phpmon.conf.json")
if Filesystem.fileExists(url.path) {
Log.info("A custom .phpmon.conf.json file was found. Attempting to parse...")
loadCustomPreferencesFile(url)
} else {
Log.info("There was no .phpmon.conf.json file to be loaded.")
}
}
private func loadCustomPreferencesFile(_ url: URL) {
do {
customPreferences = try JSONDecoder().decode(
CustomPrefs.self,
from: try! String(contentsOf: url, encoding: .utf8).data(using: .utf8)!
)
Log.info("The .phpmon.conf.json file was successfully parsed.")
} catch {
Log.warn("The .phpmon.conf.json file seems to be missing or malformed.")
}
}
} }