|
| 1 | +// |
| 2 | +// EnumExpression.swift |
| 3 | +// PredicateView |
| 4 | +// |
| 5 | +// Created by Phil Zakharchenko on 3/3/24. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +struct EnumExpression<Root, EnumType>: SimpleExpression where EnumType: CaseIterable & CustomStringConvertible & Codable & Hashable & Identifiable, EnumType.AllCases: RandomAccessCollection { |
| 11 | + typealias ExprView = EnumExpressionView |
| 12 | + |
| 13 | + enum Operator: String, CaseIterable { |
| 14 | + case `is` = "is" |
| 15 | + case isNot = "is not" |
| 16 | + } |
| 17 | + |
| 18 | + var id = UUID() |
| 19 | + let keyPath: KeyPath<Root, EnumType> |
| 20 | + let title: String |
| 21 | + var attribute: ExpressionAttribute<Self> = .init(operator: .is, value: EnumType.allCases.first!) |
| 22 | + |
| 23 | + func buildPredicate(using input: PredicateExpressions.Variable<Root>) -> (any StandardPredicateExpression<Bool>)? { |
| 24 | + switch attribute.operator { |
| 25 | + case .is: |
| 26 | + PredicateExpressions.Equal( |
| 27 | + lhs: PredicateExpressions.KeyPath(root: input, keyPath: keyPath), |
| 28 | + rhs: PredicateExpressions.Value(attribute.value) |
| 29 | + ) |
| 30 | + case .isNot: |
| 31 | + PredicateExpressions.NotEqual( |
| 32 | + lhs: PredicateExpressions.KeyPath(root: input, keyPath: keyPath), |
| 33 | + rhs: PredicateExpressions.Value(attribute.value) |
| 34 | + ) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + struct EnumExpressionView: ExpressionView { |
| 39 | + typealias Expression = EnumExpression<Root, EnumType> |
| 40 | + |
| 41 | + @Binding var expression: Expression |
| 42 | + |
| 43 | + var body: some View { |
| 44 | + TokenView(Root.self, header: { |
| 45 | + Text("\(expression.title) \(expression.attribute.operator.rawValue)") |
| 46 | + }, content: { |
| 47 | + Picker("Value", selection: $expression.attribute.value) { |
| 48 | + CustomStringConvertibleEnumPicker<EnumType>() |
| 49 | + } |
| 50 | + .labelsHidden() |
| 51 | + }, menu: { |
| 52 | + expression.operatorPickerView(using: $expression.attribute) |
| 53 | + }) |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +struct RawRepresentableEnumExpression<Root, EnumType>: SimpleExpression where EnumType: CaseIterable & RawRepresentable & Codable & Hashable, EnumType.AllCases: RandomAccessCollection, EnumType.RawValue: StringProtocol { |
| 59 | + typealias ExprView = EnumExpressionView |
| 60 | + |
| 61 | + enum Operator: String, CaseIterable { |
| 62 | + case `is` = "is" |
| 63 | + case isNot = "is not" |
| 64 | + } |
| 65 | + |
| 66 | + var id = UUID() |
| 67 | + let keyPath: KeyPath<Root, EnumType> |
| 68 | + let title: String |
| 69 | + var attribute: ExpressionAttribute<Self> = .init(operator: .is, value: EnumType.allCases.first!) |
| 70 | + |
| 71 | + func buildPredicate(using input: PredicateExpressions.Variable<Root>) -> (any StandardPredicateExpression<Bool>)? { |
| 72 | + switch attribute.operator { |
| 73 | + case .is: |
| 74 | + PredicateExpressions.Equal( |
| 75 | + lhs: PredicateExpressions.KeyPath(root: input, keyPath: keyPath), |
| 76 | + rhs: PredicateExpressions.Value(attribute.value) |
| 77 | + ) |
| 78 | + case .isNot: |
| 79 | + PredicateExpressions.NotEqual( |
| 80 | + lhs: PredicateExpressions.KeyPath(root: input, keyPath: keyPath), |
| 81 | + rhs: PredicateExpressions.Value(attribute.value) |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + struct EnumExpressionView: ExpressionView { |
| 87 | + typealias Expression = RawRepresentableEnumExpression<Root, EnumType> |
| 88 | + |
| 89 | + @Binding var expression: Expression |
| 90 | + |
| 91 | + var body: some View { |
| 92 | + TokenView(Root.self, header: { |
| 93 | + Text("\(expression.title) \(expression.attribute.operator.rawValue)") |
| 94 | + }, content: { |
| 95 | + Picker("Value", selection: $expression.attribute.value) { |
| 96 | + RawRepresentableEnumPicker<EnumType>() |
| 97 | + } |
| 98 | + .labelsHidden() |
| 99 | + }, menu: { |
| 100 | + expression.operatorPickerView(using: $expression.attribute) |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +fileprivate struct CustomStringConvertibleEnumPicker<EnumType>: View where EnumType: CaseIterable & Hashable & CustomStringConvertible & Identifiable, EnumType.AllCases: RandomAccessCollection { |
| 107 | + var body: some View { |
| 108 | + ForEach(EnumType.allCases, id: \.self) { option in |
| 109 | + Text(option.description).tag(option) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +fileprivate struct RawRepresentableEnumPicker<EnumType>: View where EnumType: CaseIterable & Hashable & RawRepresentable, EnumType.AllCases: RandomAccessCollection, EnumType.RawValue: StringProtocol { |
| 115 | + var body: some View { |
| 116 | + ForEach(EnumType.allCases, id: \.self) { option in |
| 117 | + Text(option.rawValue).tag(option) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments