Swiftpack.co - apparata/SettingsKit as Swift Package

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.2.0")

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

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Submit a free job ad (while I'm testing this). The analytics numbers for this website are here.

Related Packages

Release Notes

1.0.0 Release
29 weeks ago

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