You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/[platform]/start/quickstart/index.mdx
+76-65Lines changed: 76 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1884,7 +1884,15 @@ Once you are done, add the API dependencies to your project. Select **File > Add
1884
1884
1885
1885

1886
1886
1887
-
After adding the dependencies, update the `init` part of your `MyAmplifyAppApp.swift` file with the following code:
1887
+
After adding the dependencies, update the `import` part of your `MyAmplifyAppApp.swift` file with the following code:
1888
+
1889
+
```swift title="MyAmplifyAppApp.swift"
1890
+
import Amplify
1891
+
import AWSCognitoAuthPlugin
1892
+
import AWSAPIPlugin
1893
+
```
1894
+
1895
+
Then, update the `init()` part of your `MyAmplifyAppApp.swift` file with the following code:
1888
1896
1889
1897
```swift title="MyAmplifyAppApp.swift"
1890
1898
init() {
@@ -1902,31 +1910,31 @@ Create a new file called `TodoViewModel.swift` and the `createTodo` function wit
1902
1910
1903
1911
```swift title="TodoViewModel.swift"
1904
1912
import Amplify
1913
+
import SwiftUI
1905
1914
1906
1915
@MainActor
1907
1916
class TodoViewModel: ObservableObject {
1908
-
funccreateTodo() {
1917
+
funccreateTodo() async{
1909
1918
let creationTime=Temporal.DateTime.now()
1910
1919
let todo=Todo(
1911
-
content: "Random Todo \(creationTime)",
1920
+
content: "Random Todo \(creationTime.iso8601String)",
1912
1921
isDone: false,
1913
1922
createdAt: creationTime,
1914
1923
updatedAt: creationTime
1915
1924
)
1916
-
Task {
1917
-
do {
1918
-
let result =tryawaitAmplify.API.mutate(request: .create(todo))
1919
-
switchresult {
1920
-
case .success(lettodo):
1921
-
print("Successfully created todo: \(todo)")
1922
-
case .failure(leterror):
1923
-
print("Got failed result with \(error.errorDescription)")
1924
-
}
1925
-
} catchlet error as APIError {
1926
-
print("Failed to create todo: ", error)
1927
-
} catch {
1928
-
print("Unexpected error: \(error)")
1925
+
do {
1926
+
let result =tryawaitAmplify.API.mutate(request: .create(todo))
1927
+
switchresult {
1928
+
case .success(lettodo):
1929
+
print("Successfully created todo: \(todo)")
1930
+
todos.append(todo)
1931
+
case .failure(leterror):
1932
+
print("Got failed result with \(error.errorDescription)")
1929
1933
}
1934
+
} catch let error asAPIError {
1935
+
print("Failed to create todo: ", error)
1936
+
} catch {
1937
+
print("Unexpected error: \(error)")
1930
1938
}
1931
1939
}
1932
1940
}
@@ -1946,23 +1954,21 @@ class TodoViewModel: ObservableObject {
1946
1954
/// ...
1947
1955
}
1948
1956
1949
-
funclistTodos() {
1957
+
funclistTodos() async{
1950
1958
let request=GraphQLRequest<Todo>.list(Todo.self)
1951
-
Task {
1952
-
do {
1953
-
let result =tryawaitAmplify.API.query(request: request)
1954
-
switchresult {
1955
-
case .success(lettodos):
1956
-
print("Successfully retrieved list of todos: \(todos)")
1957
-
self.todos=todos.elements
1958
-
case .failure(leterror):
1959
-
print("Got failed result with \(error.errorDescription)")
1960
-
}
1961
-
} catchlet error as APIError {
1962
-
print("Failed to query list of todos: ", error)
1963
-
} catch {
1964
-
print("Unexpected error: \(error)")
1959
+
do {
1960
+
let result =tryawaitAmplify.API.query(request: request)
1961
+
switchresult {
1962
+
case .success(lettodos):
1963
+
print("Successfully retrieved list of todos: \(todos)")
1964
+
self.todos=todos.elements
1965
+
case .failure(leterror):
1966
+
print("Got failed result with \(error.errorDescription)")
1965
1967
}
1968
+
} catch let error asAPIError {
1969
+
print("Failed to query list of todos: ", error)
1970
+
} catch {
1971
+
print("Unexpected error: \(error)")
1966
1972
}
1967
1973
}
1968
1974
}
@@ -1987,8 +1993,7 @@ struct ContentView: View {
1987
1993
}
1988
1994
}
1989
1995
Button(action: {
1990
-
vm.createTodo()
1991
-
vm.listTodos()
1996
+
Task { awaitvm.createTodo() }
1992
1997
}) {
1993
1998
HStack {
1994
1999
Text("Add a New Todo")
@@ -2026,49 +2031,46 @@ class TodoViewModel: ObservableObject {
2026
2031
// ...
2027
2032
}
2028
2033
2029
-
funcdeleteTodos(indexSet: IndexSet) {
2034
+
funcdeleteTodos(indexSet: IndexSet) async{
2030
2035
for index in indexSet {
2031
-
let todo =todos[index]
2032
-
Task {
2033
-
do {
2034
-
let result =tryawaitAmplify.API.mutate(request: .delete(todo))
2035
-
switchresult {
2036
-
case .success(lettodo):
2037
-
print("Successfully deleted todo: \(todo)")
2038
-
case .failure(leterror):
2039
-
print("Got failed result with \(error.errorDescription)")
2040
-
}
2041
-
} catchlet error as APIError {
2042
-
print("Failed to deleted todo: ", error)
2043
-
} catch {
2044
-
print("Unexpected error: \(error)")
2045
-
}
2046
-
}
2047
-
}
2048
-
}
2049
-
2050
-
func updateTodo(todo: Todo) {
2051
-
Task {
2052
2036
do {
2053
-
let result =tryawaitAmplify.API.mutate(request: .update(todo))
2037
+
let todo =todos[index]
2038
+
let result =tryawaitAmplify.API.mutate(request: .delete(todo))
2054
2039
switchresult {
2055
2040
case .success(lettodo):
2056
-
print("Successfully updated todo: \(todo)")
2041
+
print("Successfully deleted todo: \(todo)")
2042
+
todos.remove(at: index)
2057
2043
case .failure(leterror):
2058
2044
print("Got failed result with \(error.errorDescription)")
2059
2045
}
2060
2046
} catchlet error as APIError {
2061
-
print("Failed to updated todo: ", error)
2047
+
print("Failed to deleted todo: ", error)
2062
2048
} catch {
2063
2049
print("Unexpected error: \(error)")
2064
2050
}
2065
2051
}
2066
2052
}
2053
+
2054
+
func updateTodo(todo: Todo) async {
2055
+
do {
2056
+
let result=tryawaitAmplify.API.mutate(request: .update(todo))
2057
+
switch result {
2058
+
case .success(lettodo):
2059
+
print("Successfully updated todo: \(todo)")
2060
+
case .failure(leterror):
2061
+
print("Got failed result with \(error.errorDescription)")
2062
+
}
2063
+
} catchleterrorasAPIError {
2064
+
print("Failed to updated todo: ", error)
2065
+
} catch {
2066
+
print("Unexpected error: \(error)")
2067
+
}
2068
+
}
2067
2069
}
2068
2070
2069
2071
```
2070
2072
2071
-
Update the `List` in the `ContentView.swift` file with the following code:
2073
+
Update the `List` in the `ContentView.swift` file with code to fetch the todos when the View is displayed and to call `deleteTodos(indexSet:)` when the user left-swipe a todo.
Lastly, create a new file called `TodoRow.swift` with the following code:
2097
2102
2098
2103
```swift title="TodoRow.swift"
2104
+
import SwiftUI
2105
+
2099
2106
struct TodoRow: View {
2100
2107
@ObservedObjectvarvm: TodoViewModel
2101
2108
@Bindingvartodo: Todo
@@ -2104,15 +2111,19 @@ struct TodoRow: View {
2104
2111
Toggle(isOn:$todo.isDone) {
2105
2112
Text(todo.content??"")
2106
2113
}
2107
-
.toggleStyle(SwitchToggleStyle())
2114
+
.toggleStyle(.switch)
2108
2115
.onChange(of: todo.isDone) { _, newValue in
2109
2116
var updatedTodo=todo
2110
2117
updatedTodo.isDone=newValue
2111
-
vm.updateTodo(todo: updatedTodo)
2112
-
vm.listTodos()
2118
+
Task { awaitvm.updateTodo(todo: updatedTodo) }
2113
2119
}
2114
2120
}
2115
2121
}
2122
+
2123
+
#Preview {
2124
+
@Statevartodo=Todo(content: "Hello Todo World 20240706T15:23:42.256Z", isDone: false)
2125
+
returnTodoRow(vm: TodoViewModel(), todo: $todo)
2126
+
}
2116
2127
```
2117
2128
2118
2129
This will update the UI to show a toggle to update the todo `isDone` and a swipe to delete the todo. Now if you run the application you should see the following flow.
0 commit comments