Swiftpack.co - 0x0001SUI/FelixFelicis as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by 0x0001SUI.
0x0001SUI/FelixFelicis v1.0.0
A new global environment to your SwiftUI app, that contains only values from the key window.
⭐️ 1
🕓 2 weeks ago
iOS macOS
.package(url: "https://github.com/0x0001SUI/FelixFelicis.git", from: "v1.0.0")

Felix 🪄 Felicis

The package adds a new global environment to your SwiftUI app, that contains only values from the key window.

The package comes in handy when the application supports multiple windows, and you, for example, need to open a sheet from commands only in the active window.

Getting Started

The interface is similar to the EnvironmentValues.

import SwiftUI
import FelixFelicis

First, you need to define a custom key using FelicisKey protocol:

private struct FavoriteBindingFelicisKey: FelicisKey {
    typealias Value = Binding<Bool>
}

And extend FelicisValues structure with a new property:

extension FelicisValues {
    var isFavorite: FavoriteBindingFelicisKey.Value? {
        get { self[FavoriteBindingFelicisKey.self] }
        set { self[FavoriteBindingFelicisKey.self] = newValue }
    }
}

Now you can set the value inside a view:

struct MyView: View {
    @State private var isFavorite = false

    var body: some View {
        Group {
            if isFavorite {
                Text("Favorite")
            }
        }
        .felicisBinding(\.isFavorite, $isFavorite)
    }
}

And access that value in any part of your app:

struct FavoriteCommands: Commands {
    @FelicisBinding(\.isFavorite) private var isFavorite: Bool?

    var body: some Commands {
        CommandGroup(after: .toolbar) {
            if let isFavorite = isFavorite {
                Button {
                    isFavorite.toggle()
                } label: {
                    if isFavorite {
                        Text("Unfavorite")
                    } else {
                        Text("Favorite")
                    }
                }
            }
        }
    }
}

The value you get will only be from the key window.

Overview

The package provides you with three property wrappers:

  • @FelicisValue — to get a key window value
  • @FelicisBinding — to get a key window state binding
  • @FelicisObject — to get a key window observable object

And three view modifiers, respectively:

  • felicisValue(_:_:) — to add a value to the environment
  • felicisBinding(_:_:) — to add a binding to the environment
  • felicisObject(_:) — to add an observable object to the environment

The package is well documented, so if you have any additional questions, you can build documentation in Xcode. You can also explore a simple example project.

GitHub

link
Stars: 1
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

v1.0.0
2 weeks ago

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