Last active
April 18, 2022 17:29
-
-
Save groue/e30b6699ae81c0b5f3bb8ea8859ab5c4 to your computer and use it in GitHub Desktop.
Exploring @Query initializers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| /// A Container view which uses various techniques | |
| /// for controlling RequestView. | |
| struct ContainerView: View { | |
| @State var parameter = 0 | |
| var body: some View { | |
| List { | |
| Section { | |
| HStack { | |
| Stepper("ContainerView Parameter", value: $parameter) | |
| Spacer() | |
| Text("\(parameter)") | |
| } | |
| } | |
| Section { | |
| RequestView(title: "Initial", initialParameter: parameter) | |
| } header: { | |
| Text(""" | |
| **Independent RequestView** | |
| ❌ ContainerView | |
| ✅ RequestView | |
| """) | |
| .textCase(.none) | |
| } | |
| Section { | |
| RequestView(title: "Constant", constantParameter: parameter) | |
| } header: { | |
| Text(""" | |
| **Constant RequestView** | |
| ✅ ContainerView | |
| ❌ RequestView | |
| """) | |
| .textCase(.none) | |
| } | |
| Section { | |
| RequestView(title: "Binding", parameter: $parameter) | |
| } header: { | |
| Text(""" | |
| **Binding RequestView** | |
| ✅ ContainerView | |
| ✅ RequestView | |
| """) | |
| .textCase(.none) | |
| } | |
| } | |
| } | |
| } | |
| struct ContentView_Previews: PreviewProvider { | |
| static var previews: some View { | |
| ContainerView() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| @main | |
| struct QueryExplorationApp: App { | |
| var body: some Scene { | |
| WindowGroup { | |
| ContainerView() | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Combine | |
| import GRDBQuery | |
| import SwiftUI | |
| // MARK: - RequestView | |
| struct RequestView: View { | |
| @Query<Request> var value: Int | |
| var title: String | |
| /// Initial parameter | |
| init(title: String, initialParameter parameter: Int) { | |
| self.title = title | |
| _value = Query(Request(parameter: parameter), in: \.context) | |
| } | |
| /// Constant parameter | |
| init(title: String, constantParameter parameter: Int) { | |
| self.title = title | |
| _value = Query(constant: Request(parameter: parameter), in: \.context) | |
| } | |
| /// Parameter binding | |
| init(title: String, parameter: Binding<Int>) { | |
| self.title = title | |
| _value = Query( | |
| Binding( | |
| get: { Request(parameter: parameter.wrappedValue) }, | |
| set: { parameter.wrappedValue = $0.parameter }), | |
| in: \.context) | |
| } | |
| var body: some View { | |
| HStack { | |
| /// Modifies the `@Query` parameter | |
| Stepper(title, value: $value.parameter) | |
| Spacer() | |
| Text("\(value)") | |
| } | |
| } | |
| } | |
| // MARK: - Context | |
| /// A dummy database context | |
| struct Context { } | |
| private struct ContextKey: EnvironmentKey { | |
| static let defaultValue = Context() | |
| } | |
| extension EnvironmentValues { | |
| var context: Context { | |
| get { self[ContextKey.self] } | |
| set { self[ContextKey.self] = newValue } | |
| } | |
| } | |
| // MARK: - Request | |
| /// A dummy parameterized request that just publishes... its parameter. | |
| /// We're not exploring database updates here, but request parameters. | |
| struct Request { | |
| var parameter: Int | |
| } | |
| extension Request: Queryable { | |
| static let defaultValue = 0 | |
| func publisher(in _: Context) -> Just<Int> { | |
| Just(parameter) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment