8000 GitHub - hainayanda/SwiftEnvironment: SwiftEnvironment is a Swift library designed to simplify environment value management in SwiftUI applications
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

SwiftEnvironment is a Swift library designed to simplify environment value management in SwiftUI applications

License

Notifications You must be signed in to change notification settings

hainayanda/SwiftEnvironment

Repository files navigation

SwiftEnvironment

SwiftEnvironment is a Swift library designed to allow global access to SwiftUI EnvironmentValues.

GitHub Release Codacy Badge SwiftPM Compatible Unit Test

Requirements

  • Swift 5.9 or higher
  • iOS 15.0 or higher
  • MacOS 12.0 or higher
  • TVOS 15.0 or higher
  • WatchOS 8.0 or higher
  • VisionOS 1.0 or higher
  • Xcode 15 or higher

Installation

Swift Package Manager (Xcode)

To install using Xcode's Swift Package Manager:

  1. Go to File > Swift Package > Add Package Dependency
  2. Enter the URL: https://github.com/hainayanda/SwiftEnvironment.git
  3. Choose Up to Next Major for the version rule and set the version to 4.1.4
  4. Click "Next" and wait for the package to be fetched

Swift Package Manager (Package.swift)

To add SwiftEnvironment as a dependency in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/hainayanda/SwiftEnvironment.git", .upToNextMajor(from: "4.1.4"))
]

Then include it in your target:

.target(
    name: "MyModule",
    dependencies: ["SwiftEnvironment"]
)

Usage

Defining Global Values

Define your global values using the @GlobalEntry macro:

extension GlobalValues {
    @GlobalEntry var myValue: SomeDependency = SomeDependency()
}

Accessing Global Values

Access global values directly:

let value = GlobalValue.myValue

Or use property wrappers in SwiftUI views:

@GlobalEnvironment(\.myValue) var myValue

The @GlobalEnvironment property wrapper implements DynamicProperty, ensuring your view automatically updates whenever the source value changes, similar to SwiftUI's @Environment:

struct MyView: View {
    @GlobalEnvironment(\.myValue) var myValue
    
    var body: some View {
        Text(myValue.description)
    }
}

Setting Global Values

There are several ways to set and manage global values:

Basic Setting

Set a value directly:

GlobalValues.environment(\.myValue, SomeNewValue())

Transient Values

Create values that are always newly instantiated on access:

GlobalValues.transient(\.myValue, SomeNewValue())

Weak References

Create values that can be deallocated when no longer referenced:

GlobalValues.weak(\.myValue, SomeNewValue())

Advanced Options

Using Closures

You can provide a closure for value creation (all value setters use autoclosure under the hood):

GlobalValues.environment(\.myValue) { 
    SomeNewValue() 
}

Queue Specification

Specify which DispatchQueue should handle value access:

GlobalValues.environment(\.myValue, resolveOn: .main, SomeNewValue())

Note: Queue specification is available for all value types (environment, transient, and weak).

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

License

SwiftEnvironment is available under the MIT license. See the LICENSE file for more info.

Credits

Maintained by Nayanda Haberty

About

SwiftEnvironment is a Swift library designed to simplify environment value management in SwiftUI applications

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

0