diff --git a/Examples/DerivedBehavior/ContentView.swift b/Examples/DerivedBehavior/ContentView.swift index 424ad39..d172d3c 100644 --- a/Examples/DerivedBehavior/ContentView.swift +++ b/Examples/DerivedBehavior/ContentView.swift @@ -18,6 +18,14 @@ class AppViewModel: ObservableObject { } } +struct CounterTabItem: View { + @ScopedValue(\AppViewModel.counter.count) var count + var body: some View { + Self._printChanges() + return Text("Counter \(self.count)") + } +} + struct ProfileTabItem: View { @ScopedValue(\AppViewModel.profile.favorites.count) var favoritesCount var body: some View { @@ -32,9 +40,7 @@ struct ContentView: View { return TabView { CounterView() .tabItem { - WithScopedValue(\AppViewModel.counter.count) { count in - Text("Counter \(count)") - } + CounterTabItem() } ProfileView() diff --git a/README.md b/README.md index 5e280ac..28637b4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ A collection of tools for working with observable objects in SwiftUI. * [Motivation](#motivation) * [Tools](#tools) -* [Examples](#examples) * [Installation](#installation) @@ -192,7 +191,52 @@ struct CounterView: View { } ``` -## Examples +### Dependency + +Similar to SwiftUI's `@Environment` attribute but for observable objects instead of views. + +```swift +class TwentySideDie: ObservableObject { + @Dependency(\.numberGenerator) var numberGenerator + @Published var value: Int = 20 + + func roll() { + self.value = self.numberGenerator.random(in: 1...20) + } +} +``` + +### Dependencies + +Dependencies can be defined by defining a type that conforst to the `DependencyKey` protocol, and then extending the dependencies structure with a new property: + +```swift +struct NumberGenerator { + var random: (ClosedRange) -> Int + + func random(in range: ClosedRange) -> Int { + self.random(range) + } + + static let `default` = Self { Int.random(in: $0) } +} + +enum NumberGeneratorKey: DependencyKey { + static let defaultValue: NumberGenerator = .default +} + +extension Dependencies { + var numberGenerator: NumberGenerator { + get { self[NumberGeneratorKey.self] } + set { self[NumberGeneratorKey.self] = newValue } + } +} +``` + + +### WithDependencies + +### withDependencies(\_:\_:) ## Installation