Swiftpack.co - apparata/SettingsKit as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by apparata.
apparata/SettingsKit 1.2.2
A Swift library for easily building settings forms in SwiftUI hooked up to UserDefaults.
⭐️ 3
🕓 6 days ago
iOS macOS tvOS
.package(url: "https://github.com/apparata/SettingsKit.git", from: "1.2.2")

SettingsKit

License

See the LICENSE file for licensing information. (It's the MIT license.)

User Defaults

ObservableUserDefaults

Base class for observable settings objects. NOTE: There must not be dots in the key, or the kvo observation does not work, for some reason.

Example:

public class Settings: ObservableUserDefaults {
     
    @UserDefault("isOnboardingEnabledSetting", default: true)
    public var isOnboardingEnabled: Bool
     
    @UserDefault("apiEnvironmentSetting", default: .development)
    public var apiEnvironment: APIEnvironment

    @UserDefault("isDebugLogEnabledSetting", default: false)
    public var isDebugLogEnabled: Bool
     
    @UserDefault("DebugLogFilename", default: "log.txt")
    public var logFilename: String
}

Subscribing to UserDefaults changes:

private func subscribeToAPIEnvironmentChanges() {
    settings.$apiEnvironment.sink { [weak self] environment in
        self?.didRequestAPIEnvironmentChange(to: environment)
    }.store(in: &cancellables)
}

@UserDefault

Example of using the @UserDefault property wrapper. If you have @AppStorage properties in your SwiftUI views, the same keys can be used with @UserDefault to access the same values.

Example:

class SomeClass {
    @UserDefault("THE_KEY", default: 8)
    var whatever: Int

Form Row Views

ButtonSetting

Example:

ButtonSetting("OK") {
    print("OK!")
}

DestructiveButtonSetting

Red button for destructive actions.

Example:

DestructiveButtonSetting("Delete") {
    print("Destroy!")
}

LinkSetting

Example:

LinkSetting("Github", url: URL(string: "https://github.com")!) {
    print("OK!")
}

EnumSetting

Setting view for picking an enum value.

The enum should be CaseIterable and Pickable, which is a typealias for the combination of Codable, Identifiable, and CustomStringConvertible.

Example:

public enum APIEnvironment: String, CaseIterable, Codable, Identifiable {
    case development = "Development"
    case staging = "Staging"
    case production = "Production"
}

extension APIEnvironment: CustomStringConvertible {
    public var description: String {
        rawValue
    }
    
    public var id: APIEnvironment { self }
}

@State private var apiEnvironment: APIEnvironment

EnumSetting("API Environment", selection: $apiEnvironment)

SegmentedEnumSetting

The enum should be CaseIterable and Pickable, which is a typealias for the combination of Codable, Identifiable, and CustomStringConvertible.

Example:

public enum APIEnvironment: String, CaseIterable, Codable, Identifiable {
    case development = "Development"
    case staging = "Staging"
    case production = "Production"
}

extension APIEnvironment: CustomStringConvertible {
    public var description: String {
        rawValue
    }
    
    public var id: APIEnvironment { self }
}

@State private var apiEnvironment: APIEnvironment

SegmentedEnumSetting(selection: $apiEnvironment)

SegmentedSetting

A segmented setting view for picking a value from a collection.

Example:

SegmentedSetting("API Environment",
                 values: APIEnvironment.allCases,
                 selection: $settings.apiEnvironment)

SwitchSetting

Example:

@State private var isLogEnabled: Bool

SwitchSetting("Enable log", isOn: $isLogEnabled)

TextFieldTextSetting

Title and text field setting.

Example:

TextFieldSetting("Log Filename",
                 value: $settings.logFilename)

LabelSetting

Example:

LabelSetting("Name")
LabelSetting("Name", value: "Steve")

PickerSetting

A setting view for picking a value from a collection.

Example:

PickerSetting("API Environment",
              values: APIEnvironment.allCases,
              selection: $settings.apiEnvironment)

GitHub

link
Stars: 3
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

1.0.0 Release
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics