Skip to content

Commit 0cb4432

Browse files
committed
Update README
1 parent bf8c415 commit 0cb4432

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ This repo is an experiment on designing and implementing a cross-platform SwiftU
1616

1717
A `PredicateView` can be initialized using a binding to a [`Predicate`](https://developer.apple.com/documentation/foundation/predicate) and a collection of row templates. Note: it purposefully uses the same terminology for row templates as `NSPredicateEditor`.
1818

19+
`PredicateView` has built-in support for common primitive and compound Swift types, including `Bool`, `String`, `Numeric`, `CaseIterable`, and `RawRepresentable`. It uses appropriate customized UI controls for each one, such as menus for enums, segmented controls for booleans, and steppers for numeric data.
20+
21+
You construct a `PredicateView` by providing a `Binding` to a `Predicate` over your model type. You then provide a collection of row templates, which define the set of properties the user will be able to filter through. Each template must contain a `KeyPath` in your model type, as well as a user-friendly title.
22+
23+
As the user makes changes to the predicate using the `PredicateView` UI, the value of the predicate will update. If your model instance conforms to `Sequence`, you can easily filter it using the predicate by calling the Swift standard library `filter(_:)` method. Note that it is a good practice to opt into updating the filtered-down representation of the model live as the user interacts with the `PredicateView` UI, instead of requiring the user to interact with an additional control, such as an explicit Search button.
24+
25+
### Optionals
26+
27+
`PredicateView` has built-in support for optionals of all supported types. If a given row template uses an optional `KeyPath`, the user will be able to filter over the existence or non-existence of values, in addition to the values themselves.
28+
29+
### Collections
30+
31+
You can provide nested row templates for `Collection` backed data, which will allow the user to customize their query even more, filtering over containment or filtering elements that satisfy all of the conditions.
32+
33+
```swift
34+
.init(keyPath: \.preferredColors, title: "Preferred Colors", rowTemplates: [
35+
.init(keyPath: \.name, title: "Name"),
36+
.init(keyPath: \.cost, title: "Cost"),
37+
]),
38+
```
39+
40+
![Collections Menu](Resources/CollectionsMenu.png "User-facing Collections Menu")
41+
42+
### Sample Code
43+
44+
See the built-in `PredicateDemoView` for a complete sample use case.
45+
1946
```swift
2047
/// A sample model type.
2148
struct Model: Identifiable {
@@ -42,16 +69,19 @@ PredicateView(predicate: $predicate, rowTemplates: [
4269
.init(keyPath: \.firstName, title: "First Name"),
4370
.init(keyPath: \.age, title: "Age"),
4471
.init(keyPath: \.employmentStatus, title: "Employment Status"),
45-
.init(keyPath: \.isRegistered, title: "Registration Status")
72+
.init(keyPath: \.isRegistered, title: "Registration Status"),
73+
.init(keyPath: \.preferredColors, title: "Preferred Colors", rowTemplates: [
74+
.init(keyPath: \.name, title: "Name"),
75+
.init(keyPath: \.cost, title: "Cost"),
76+
]),
4677
])
4778
```
4879

49-
See the built-in `PredicateDemoView` for a complete sample use case.
50-
5180
## Features
5281

5382
- [x] Easy setup
5483
- [x] Type safety
84+
- [x] Built-in support for optionals and nested expressions
5585
- [x] Rich representations for supported data types
5686

5787
## Not Features

Resources/CollectionsMenu.png

63.6 KB
Loading

0 commit comments

Comments
 (0)