Save state in the XDG data home directory

This commit is contained in:
david-swift 2024-01-03 09:27:22 +01:00
parent 021f0f231f
commit da29f92c91
2 changed files with 7 additions and 6 deletions

View File

@ -18,7 +18,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/AparokshaUI/Libadwaita", from: "0.1.0"),
.package(url: "https://github.com/AparokshaUI/Libadwaita", from: "0.1.5"),
.package(
url: "https://github.com/david-swift/LevenshteinTransformations",
from: "0.1.1"

View File

@ -6,6 +6,7 @@
//
import Foundation
import Libadwaita
/// A property wrapper for properties in a view that should be stored throughout view updates.
@propertyWrapper
@ -109,14 +110,14 @@ public struct State<Value>: StateProtocol {
/// Get the settings directory path.
/// - Returns: The path.
private func dirPath() -> String {
"\(NSHomeDirectory())/.config/\(GTUIApp.appID)/"
private func dirPath() -> URL {
NativePeer.getUserDataDirectory().appendingPathComponent(GTUIApp.appID, isDirectory: true)
}
/// Get the settings file path.
/// - Returns: The path.
private func filePath() -> URL {
.init(fileURLWithPath: dirPath() + "\(content.storage.key ?? "temporary").json")
dirPath().appendingPathComponent("\(content.storage.key ?? "temporary").json")
}
}
@ -138,9 +139,9 @@ extension State where Value: Codable {
/// Check whether the settings file exists, and, if not, create it.
private func checkFile() {
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: dirPath()) {
if !fileManager.fileExists(atPath: dirPath().path) {
try? fileManager.createDirectory(
at: .init(fileURLWithPath: dirPath()),
at: .init(fileURLWithPath: dirPath().path),
withIntermediateDirectories: true,
attributes: nil
)