Swiftpack.co - Tunous/EnvironmentBinding as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Tunous.
Tunous/EnvironmentBinding 1.0.0
A property wrapper that reads a binding from a SwiftUI's view’s environment.
⭐️ 1
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/Tunous/EnvironmentBinding.git", from: "1.0.0")

EnvironmentBinding

CI | Documentation

Deprecated: It is possible to natively achieve the same functionallity as this library provides by using both @Envrionment and @Binding property wrappers:

struct PlayButton: View {
    @Environment(\.isPlaying) @Binding var isPlaying

    var body: some View {
        Button(isPlaying ? "Pause" : "Play") {
            isPlaying.toggle()
        }
    }
}

A property wrapper that reads a binding from a SwiftUI's view’s environment. It allows you to skip writing .wrappedValue when working with Bindings in environment.

Usage

EnvironmentBinding property wrapper works with any non-optional Binding defined in EnvironmentValues. For example if you define isPlaying environment value as follows:

private struct IsPlayingKey: EnvironmentKey {
    static let defaultValue: Binding<Bool> = .constant(false)
}

extension EnvironmentValues {
    var isPlaying: Binding<Bool> {
        get { self[IsPlayingKey.self] }
        set { self[IsPlayingKey.self] = newValue }
    }
}

Then you can later access it from environment by passing isPlaying key path to EnvironmentBinding. That way you can access and modify the underlying value directly or get access to the binding through the $ symbol.

import EnvironmentBinding

struct PlayButton: View {
    @EnvironmentBinding(\.isPlaying) var isPlaying

    var body: some View {
        Button(isPlaying ? "Pause" : "Play") {
            isPlaying.toggle()
        }
    }
}

Installation

Swift Package Manager

Add the following to the dependencies array in your "Package.swift" file:

.package(url: "https://github.com/Tunous/EnvironmentBinding.git", .upToNextMajor(from: "1.0.0"))

Or add https://github.com/Tunous/EnvironmentBinding.git, to the list of Swift packages for any project in Xcode.

Credits

This package was inspired by EnvironmentBinding article from NIL COALESCING LIMITED.

GitHub

link
Stars: 1
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1 year ago

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