From 87958a5daf0bbeeb9f3e3b7fb746539bbc9a3847 Mon Sep 17 00:00:00 2001 From: Andreas Grosam Date: Tue, 9 Dec 2025 18:19:41 +0100 Subject: [PATCH] Add configurable key prefix to AppSettingValues - Added `prefix` property to `AppSettingValues` for runtime key prefix configuration - Automatic bundle identifier fallback when prefix is not customized - Thread-safe access via `OSAllocatedUnfairLock` - Updated documentation with prefix configuration examples --- README.md | 33 +++++++++++++++------ Sources/Settings/SwiftUI/AppSetting.swift | 35 +++++++++++++++++++---- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7148ea2..d3f0c9d 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ dependencies: [ Or in Xcode: 1. File → Add Package Dependencies 2. Enter: `https://github.com/couchdeveloper/Settings.git` -3. Select version: 0.2.0 or later +3. Select version: 0.3.0 or later ## Features @@ -94,6 +94,26 @@ import SettingsMock #endif ``` +## Set a global key prefix: + +```swift +@main +struct MyApp: App { + init() { + AppSettingValues.prefix = "myapp_" + } + + var body: some Scene { + WindowGroup { + MainView() + } + } +} +``` + +> **Note:** When the prefix is not customized, Settings library will use the bundle identifier from the main bundle to derive a unique prefix for every key. + + ## Custom Settings Container ```swift @@ -186,8 +206,8 @@ struct Settings { ## Requirements -- Swift 6.1 -- macOS 10.15+ / iOS 13.0+ / tvOS 13.0+ / watchOS 6.0+ +- Swift 6.2 +- macOS 10.15+ / iOS 17.0+ / tvOS 17.0+ / watchOS 10.0+ ## Installation @@ -195,7 +215,7 @@ struct Settings { ```swift dependencies: [ - .package(url: "https://github.com/couchdeveloper/UserDefaults.git", from: "0.1.0") + .package(url: "https://github.com/couchdeveloper/Settings.git", from: "0.3.0") ] ``` @@ -230,11 +250,6 @@ for try await name in Settings.$user.stream(for: \.name) { } ``` -## Requirements - -- Swift 6.1 or later -- iOS 17.0+ / macOS 15.0+ / tvOS 17.0+ / watchOS 10.0+ - ## Contributing Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. diff --git a/Sources/Settings/SwiftUI/AppSetting.swift b/Sources/Settings/SwiftUI/AppSetting.swift index 87b110d..a78bf1c 100644 --- a/Sources/Settings/SwiftUI/AppSetting.swift +++ b/Sources/Settings/SwiftUI/AppSetting.swift @@ -49,16 +49,41 @@ import os /// AppSettingValues.resetStore() /// ``` public struct AppSettingValues: __Settings_Container { - private static let _store = OSAllocatedUnfairLock(initialState: UserDefaults.standard) + struct Config { + var store: any UserDefaultsStore = UserDefaults.standard + var prefix: String? = nil + } + + private static let _config = OSAllocatedUnfairLock(initialState: Config()) + public internal(set) static var store: any UserDefaultsStore { get { - _store.withLock { store in - store + _config.withLock { config in + config.store + } + } + set { + _config.withLock { config in + config.store = newValue } } + } + + public static var prefix: String { + get { + _config.withLock { config in + if let prefix = config.prefix { + return prefix + } + guard let identifier = Bundle.main.bundleIdentifier else { + return "app_" + } + return identifier.replacing(".", with: "_") + "_" + } + } set { - _store.withLock { store in - store = newValue + _config.withLock { config in + config.prefix = newValue } } }