Gardening some examples (#64)
* Gardening some examples * Revert variable names
This commit is contained in:
parent
f3ccc0b3a1
commit
45a77a21ba
|
@ -14,7 +14,7 @@ object and synchronizing it to view state with the `bind` view modifier that shi
|
||||||
library.
|
library.
|
||||||
|
|
||||||
For example, suppose you have a sign in flow where if the API request to sign in fails, you want
|
For example, suppose you have a sign in flow where if the API request to sign in fails, you want
|
||||||
to refocus the email field. The model can be implement like so:
|
to refocus the email field. The model can be implemented like so:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class SignInModel: ObservableObject {
|
class SignInModel: ObservableObject {
|
||||||
|
|
|
@ -36,7 +36,7 @@ struct EditView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else: {
|
} else: {
|
||||||
Text("\(self.string)")
|
Text(self.string)
|
||||||
Button("Edit") {
|
Button("Edit") {
|
||||||
self.editableString = self.string
|
self.editableString = self.string
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ struct EditView: View {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
This is the most optimal way to model this domain. Without the ability to deriving a
|
This is the most optimal way to model this domain. Without the ability to derive a
|
||||||
`Binding<String>` from a `Binding<String?>` we would have had to hold onto extra state to represent
|
`Binding<String>` from a `Binding<String?>` we would have had to hold onto extra state to represent
|
||||||
whether or not we are in editing mode:
|
whether or not we are in editing mode:
|
||||||
|
|
||||||
|
@ -94,15 +94,15 @@ struct EditView: View {
|
||||||
TextField("Edit string", text: $string)
|
TextField("Edit string", text: $string)
|
||||||
HStack {
|
HStack {
|
||||||
Button("Cancel") {
|
Button("Cancel") {
|
||||||
self.editableString = nil
|
self.editableString = .inactive
|
||||||
}
|
}
|
||||||
Button("Save") {
|
Button("Save") {
|
||||||
self.string = string
|
self.string = string
|
||||||
self.editableString = nil
|
self.editableString = .inactive
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else: {
|
} else: {
|
||||||
Text("\(self.string)")
|
Text(self.string)
|
||||||
Button("Edit") {
|
Button("Edit") {
|
||||||
self.editableString = .active(self.string)
|
self.editableString = .active(self.string)
|
||||||
}
|
}
|
||||||
|
@ -136,7 +136,7 @@ enum ItemStatus {
|
||||||
case outOfStock(isOnBackOrder: Bool)
|
case outOfStock(isOnBackOrder: Bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct InventoryItemView {
|
struct InventoryItemView: View {
|
||||||
@State var status: ItemStatus
|
@State var status: ItemStatus
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import SwiftUI
|
||||||
/// case outOfStock(isOnBackOrder: Bool)
|
/// case outOfStock(isOnBackOrder: Bool)
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// struct InventoryItemView {
|
/// struct InventoryItemView: View {
|
||||||
/// @State var status: ItemStatus
|
/// @State var status: ItemStatus
|
||||||
///
|
///
|
||||||
/// var body: some View {
|
/// var body: some View {
|
||||||
|
|
|
@ -11,7 +11,7 @@ import SwiftUI
|
||||||
/// optional binding.
|
/// optional binding.
|
||||||
///
|
///
|
||||||
/// ```swift
|
/// ```swift
|
||||||
/// struct InventoryItemView {
|
/// struct InventoryItemView: View {
|
||||||
/// @State var quantity: Int?
|
/// @State var quantity: Int?
|
||||||
///
|
///
|
||||||
/// var body: some View {
|
/// var body: some View {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import SwiftUI
|
||||||
/// case outOfStock(isOnBackOrder: Bool)
|
/// case outOfStock(isOnBackOrder: Bool)
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// struct InventoryItemView {
|
/// struct InventoryItemView: View {
|
||||||
/// @State var status: ItemStatus
|
/// @State var status: ItemStatus
|
||||||
///
|
///
|
||||||
/// var body: some View {
|
/// var body: some View {
|
||||||
|
|
Loading…
Reference in New Issue