Swiftpack.co -  Package - apparata/SettingsKit
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
apparata/SettingsKit
A Swift library for easily building settings forms in SwiftUI hooked up to UserDefaults.
.package(url: "https://github.com/apparata/SettingsKit.git", from: "1.0.0")

SettingsKit

License

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

User Defaults

ObservableUserDefaults

Base class for observable settings objects.

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.

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: 1
Last commit: 2 weeks ago

Release Notes

1.0.0 Release
2 weeks ago

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