Skip to content

Lightweight implementation of Apple's @AppStorage with a little benefits.

Notifications You must be signed in to change notification settings

0xstragner/CodableStorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodableStorage

Lightweight implementation of Apple's @AppStorage with a little benefits.

Features

  • SwiftUI compatible
  • UIKit compatible

Requirements

  • iOS 13.0+
  • macOS 10.15+
  • watchOS 6.0+
  • tvOS 12.0+

Installation

.package(
    url: "https://github.com/0xstragner/CodableStorage",
    .upToNextMajor(from: "0.1.0")
)

Usage

SwiftUI

import SwiftUI
import CodableStorage

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, world!")
                .padding()
                .background(testValue % 2 == 0 ? Color.red : Color.green)

            Button(action: {
                testValue += 1
            }, label: {
                Text("Click me")
            })
        }
    }

    @CodableStorage("key", defaultValue: 0)
    private var testValue: Int
}

UIKit

class Subview: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        $value.eraseToAnyPublisher().sink(receiveValue: { value in
            print(value)
        }).store(in: &cancellables)
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: Private

    @CodableStroage("test", defaultValue: 0)
    private var value: Int

    private var cancellables: Set<AnyCancellable> = .init()
}

Typed keys

extension CodableStorageValueKey {
    static let myKey = CodableStorageValueKey("mykey")
}

class Subview: UIView {
    @CodableStroage(.myKey)
    private var value: Int?
}

Authors

About

Lightweight implementation of Apple's @AppStorage with a little benefits.

Topics

Resources

Stars

Watchers

Forks

Languages