77
88import SwiftUI
99
10- struct BoolExpression < Root> : SimpleExpression {
11- typealias ExprView = BoolExpressionView < Root >
10+ extension AnyExpression {
11+ public init ( keyPath: KeyPath < Root , Bool > , title: String ) {
12+ self . wrappedValue = BoolExpression ( keyPath: keyPath, title: title)
13+ }
1214
15+ public init ( keyPath: KeyPath < Root , Bool ? > , title: String ) {
16+ self . wrappedValue = OptionalExpression < Root , BoolExpression > ( keyPath: keyPath, title: title)
17+ }
18+ }
19+
20+ struct BoolExpression < Root> : ContentExpression {
1321 enum Operator : String , CaseIterable {
1422 case `is` = " is "
1523 case isNot = " is not "
@@ -29,47 +37,40 @@ struct BoolExpression<Root>: SimpleExpression {
2937 }
3038 }
3139
40+ static var defaultAttribute : ExpressionAttribute < Self > { . init( operator: . is, value: true ) }
41+
3242 var id = UUID ( )
3343 let keyPath : KeyPath < Root , Bool >
3444 let title : String
35- var attribute : ExpressionAttribute < Self > = . init ( operator : . is , value : true )
45+ var attribute : ExpressionAttribute < Self > = Self . defaultAttribute
3646
37- func buildPredicate( using input: PredicateExpressions . Variable < Root > ) -> ( any StandardPredicateExpression < Bool > ) ? {
38- switch attribute. operator {
47+ static func buildPredicate< V> (
48+ for variable: V ,
49+ using value: Value ,
50+ operation: Operator
51+ ) -> ( any StandardPredicateExpression < Bool > ) ? where V: StandardPredicateExpression < Value > {
52+ switch operation {
3953 case . is:
4054 PredicateExpressions . Equal (
41- lhs: PredicateExpressions . KeyPath ( root : input , keyPath : keyPath ) ,
42- rhs: PredicateExpressions . Value ( attribute . value)
55+ lhs: variable ,
56+ rhs: PredicateExpressions . Value ( value)
4357 )
4458 case . isNot:
4559 PredicateExpressions . NotEqual (
46- lhs: PredicateExpressions . KeyPath ( root : input , keyPath : keyPath ) ,
47- rhs: PredicateExpressions . Value ( attribute . value)
60+ lhs: variable ,
61+ rhs: PredicateExpressions . Value ( value)
4862 )
4963 }
5064 }
51- }
52-
53- struct BoolExpressionView < Root> : ExpressionView {
54- typealias Expression = BoolExpression < Root >
55-
56- @Binding var expression : Expression
5765
58- var body : some View {
59- TokenView ( Root . self, header: {
60- Text ( " \( expression. title) \( expression. attribute. operator. rawValue) " )
61- } , content: {
62- Picker ( " Value " , selection: $expression. attribute. value) {
63- ForEach ( Expression . Operator. allCases, id: \. self) {
64- Text ( $0. label)
65- . tag ( $0. associatedValue)
66- }
66+ static func makeContentView( _ value: Binding < Bool > ) -> some View {
67+ Picker ( " Value " , selection: value) {
68+ ForEach ( Operator . allCases, id: \. self) { expression in
69+ Text ( expression. label)
70+ . tag ( expression. associatedValue)
6771 }
68- . pickerStyle ( . segmented)
69- . labelsHidden ( )
70- } , menu: {
71- expression. operatorPickerView ( using: $expression. attribute)
72- } )
72+ }
73+ . pickerStyle ( . segmented)
74+ . labelsHidden ( )
7375 }
7476}
75-
0 commit comments