Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -186,16 +206,16 @@ 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

### Swift Package Manager

```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")
]
```

Expand Down Expand Up @@ -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.
Expand Down
35 changes: 30 additions & 5 deletions Sources/Settings/SwiftUI/AppSetting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,41 @@ import os
/// AppSettingValues.resetStore()
/// ```
public struct AppSettingValues: __Settings_Container {
private static let _store = OSAllocatedUnfairLock<any UserDefaultsStore>(initialState: UserDefaults.standard)
struct Config {
var store: any UserDefaultsStore = UserDefaults.standard
var prefix: String? = nil
}

private static let _config = OSAllocatedUnfairLock<Config>(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
}
}
}
Expand Down