|
| 1 | +// |
| 2 | +// CustomExpressionDemoView.swift |
| 3 | +// PredicateViewExample |
| 4 | +// |
| 5 | +// Created by Phil Zakharchenko on 9/28/24. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftData |
| 9 | +import SwiftUI |
| 10 | +import PredicateView |
| 11 | + |
| 12 | +// MARK: - StatusExpressionView |
| 13 | + |
| 14 | +/// A custom expression view for handling status predicates in a predicate builder. |
| 15 | +struct StatusExpressionView: CustomExpressionView { |
| 16 | + /// Defines the operators available for status comparisons. |
| 17 | + enum Operator: String, ExpressionOperator { |
| 18 | + case equal = "is" |
| 19 | + case notEqual = "is not" |
| 20 | + } |
| 21 | + |
| 22 | + /// The title of the expression view. |
| 23 | + static let title = "Status" |
| 24 | + |
| 25 | + /// The key path to the status property of an ``Item``. |
| 26 | + static var keyPath: KeyPath<Item, Item.Status.RawValue?> { \._status } |
| 27 | + |
| 28 | + /// The default value for the status. |
| 29 | + /// This will be the value picked automatically when the user inserts this view into an expression. |
| 30 | + static var defaultValue: Item.Status.RawValue? { Item.Status.todo.rawValue } |
| 31 | + |
| 32 | + /// Creates a predicate for a given status value and operator. |
| 33 | + /// |
| 34 | + /// - Parameters: |
| 35 | + /// - value: The status value to compare against. |
| 36 | + /// - op: The operator to use for comparison. |
| 37 | + /// - Returns: A `Predicate<Item>` representing the status condition. |
| 38 | + static func predicate(for value: Item.Status.RawValue?, operator op: Operator) -> Predicate<Item> { |
| 39 | + switch op { |
| 40 | + case .equal: |
| 41 | + return #Predicate<Item> { $0._status == value } |
| 42 | + case .notEqual: |
| 43 | + return #Predicate<Item> { $0._status != value } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Decodes a predicate expression into a `DecodedKeyPathExpression`. |
| 48 | + /// |
| 49 | + /// - Parameter expression: The predicate expression to decode. |
| 50 | + /// - Returns: A `DecodedKeyPathExpression<Self>` if the expression can be decoded, otherwise `nil`. |
| 51 | + static func decode<PredicateExpressionType: PredicateExpression<Bool>>( |
| 52 | + _ expression: PredicateExpressionType |
| 53 | + ) -> DecodedKeyPathExpression<Self>? { |
| 54 | + switch expression { |
| 55 | + case let expression as PredicateExpressions.Equal<KeyPathPredicateExpression, ValuePredicateExpression>: |
| 56 | + .init(keyPathExpression: expression.lhs, operator: .equal, value: expression.rhs.value) |
| 57 | + case let expression as PredicateExpressions.NotEqual<KeyPathPredicateExpression, ValuePredicateExpression>: |
| 58 | + .init(keyPathExpression: expression.lhs, operator: .notEqual, value: expression.rhs.value) |
| 59 | + default: |
| 60 | + nil |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// The binding to the current status value. |
| 65 | + @Binding var value: Item.Status.RawValue? |
| 66 | + |
| 67 | + /// The body of the view, which creates a picker for selecting the status. |
| 68 | + var body: some View { |
| 69 | + Picker("", selection: $value) { |
| 70 | + ForEach(Item.Status.allCases, id: \.rawValue) { item in |
| 71 | + Text(item.rawValue) |
| 72 | + .tag(item.rawValue) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: - CustomExpressionDemoView |
| 79 | + |
| 80 | +struct CustomExpressionDemoView: View { |
| 81 | + @Environment(\.modelContext) private var modelContext |
| 82 | + @State var predicate: Predicate<Item> = #Predicate<Item> { |
| 83 | + $0.title.localizedStandardContains("Item") |
| 84 | + } |
| 85 | + |
| 86 | + var body: some View { |
| 87 | + VStack(alignment: .leading) { |
| 88 | + Text("This demo provides a sample implementation of the `CustomExpressionView` protocol that allows you to build custom expression views for key paths not covered by the standard set of built-in expressions. In this example, the status picker is a custom expression view that maps between an `Optional<String>` model representation and an enum value.") |
| 89 | + |
| 90 | + predicateView(for: $predicate) |
| 91 | + |
| 92 | + Table(items) { |
| 93 | + TableColumn("Title", value: \.title) |
| 94 | + TableColumn("Status", value: \.status.rawValue) |
| 95 | + TableColumn("Created") { value in |
| 96 | + Text(value.creationDate, style: .date) |
| 97 | + } |
| 98 | + TableColumn("Modified") { value in |
| 99 | + Text(value.modificationDate, style: .date) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + .padding() |
| 104 | + } |
| 105 | + |
| 106 | + private var items: [Item] { |
| 107 | + try! modelContext.fetch(.init(predicate: predicate)) |
| 108 | + } |
| 109 | + |
| 110 | + private func predicateView(for predicate: Binding<Predicate<Item>>) -> some View { |
| 111 | + ScrollView(.horizontal) { |
| 112 | + PredicateView(predicate: predicate, rowTemplates: [ |
| 113 | + .init(keyPath: \.title, title: "Title"), |
| 114 | + .init(keyPath: \.creationDate, title: "Creation date"), |
| 115 | + .init(keyPath: \.modificationDate, title: "Modification date"), |
| 116 | + .init(StatusExpressionView.self), |
| 117 | + ]) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments