From 234f4977c0f40cbeed5563ad4b8770181e5b91cf Mon Sep 17 00:00:00 2001 From: Lumi Date: Tue, 20 Aug 2024 12:59:25 +0900 Subject: [PATCH] Add ContainerValues' description into README --- README.md | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 881cba9..418b36e 100644 --- a/README.md +++ b/README.md @@ -67,22 +67,53 @@ SwiftUI-AnySubViews unifies the following types, ensuring consistent API usage a | AnySubview | Subview | \_VariadicView_Children.Element | | AnySubviewsCollection | SubviewsCollection | \_VariadicView_Children | | AnySubviewsCollectionSlice | SubviewsCollectionSlice | Slice<_VariadicView_Children> | -| AnyContainerValues | ContainerValues | TODO | +| AnyContainerValues | ContainerValues | _ViewTraitKey | -## TODO +### ContainerValues + +[ContainerValues](https://developer.apple.com/documentation/SwiftUI/ContainerValues) is also supported in AnySubviews. +To define your custom container values associated with a view, use macro `AnyEntry` within `AnyContainerValueKeys`'s extension: -- [x] Support [hasTag](https://developer.apple.com/documentation/swiftui/containervalues/hastag(_:)) -- [ ] Support [container values](https://developer.apple.com/documentation/swiftui/containervalues) +```swift +// Define +// #AnyEntry(keyName, defaultValue) +extension AnyContainerValueKeys { + #AnyEntry("myNumber", 0) + #AnyEntry("myName", "Name") +} + +// Set +someView + .anyContainerValue(keyPath: \.myNumber, 1) + .anyContainerValue(keyPath: \.myName, "Lumi") + +// Read +BackportGroup(subviews: content) { subviews in + ForEach(subviews) { subview in + let number = subview.containerValues[\.myNumber]?.description ?? "nil" + let name = subview.containerValues[\.myName] ?? "nil" + Text(number + " " + name) + } +} +``` ## Dropping iOS 17 When your app no longer needs to support iOS 17 and earlier versions, you can simplify your codebase: -1. Remove the SwiftUI-AnySubviews package from your project. +1. Remove the SwiftUI-AnySubviews package from your project completely. 2. Replace all occurrences of `BackportGroup` with `Group` throughout your codebase. +3. For `ContainerValues` especially: + 1. Replace definition part with official API `extension ContainerValues { @Entry ... }`. + 2. Replace setting part with official API `.containerValue(key: MyKey.self, value: 2)`. + 3. Reaplce reading part with official API `subview.containerValues.myCustomValue`. Note: Only proceed with this step when you're certain that your app's minimum supported iOS version is 18 or later. +## TODO + +- [ ] Enrich the error desecription of Macro `AnyEntry` + ## Contributing Contributions are welcome! Please feel free to submit a Issue or Pull Request.