@@ -73,11 +73,13 @@ class ManageTeamViewController: UIViewController, UITableViewDelegate, UITableVi
73
73
removeTeamMember ( email: members [ indexPath. row] . name)
74
74
}
75
75
76
+ // :code-block-start: fetch-team-members
76
77
// Calls a Realm function to fetch the team members and adds them to the list
77
78
func fetchTeamMembers( ) {
78
79
// Start loading indicator
79
80
activityIndicator. startAnimating ( )
80
- app. functions. getMyTeamMembers ( [ ] ) { [ weak self] ( result, error) in
81
+ // :hide-start:
82
+ app. currentUser ( ) !. functions. getMyTeamMembers ( [ ] ) { [ weak self] ( result, error) in
81
83
DispatchQueue . main. sync {
82
84
guard self != nil else {
83
85
// This can happen if the view is dismissed
@@ -102,70 +104,47 @@ class ManageTeamViewController: UIViewController, UITableViewDelegate, UITableVi
102
104
self !. tableView. reloadData ( )
103
105
}
104
106
}
107
+ // :replace-with:
108
+ // // TODO: use the app's current user's functions object to call the getMyTeamMembers function
109
+ // // on the backend. Create Member objects to represent the result in the completion handler
110
+ // // and reload the table data to refresh the view.
111
+ // :hide-end:
105
112
}
113
+ // :code-block-end:
106
114
115
+ // :code-block-start: add-team-member
107
116
func addTeamMember( email: String ) {
108
117
print ( " Adding member: \( email) " )
109
118
activityIndicator. startAnimating ( )
110
- app. functions. addTeamMember ( [ AnyBSON ( email) !] , { [ weak self] ( result, realmError) in
111
- // There are two kinds of errors:
112
- // - The Realm function call itself failed (for example, due to network error)
113
- // - The Realm function call succeeded, but our business logic within the function returned an error,
114
- // (for example, email not found).
115
- var errorMessage : String ? = nil
116
-
117
- if ( realmError != nil ) {
118
- // Error from Realm (failed function call, network error...)
119
- errorMessage = realmError!. localizedDescription
120
- } else if let resultDocument = result? . documentValue {
121
- // Check for user error. The addTeamMember function we defined returns an object
122
- // with the `error` field set if there was a user error.
123
- errorMessage = resultDocument [ " error " ] ?? . stringValue
124
- } else {
125
- // The function call did not fail but the result was not a document.
126
- // This is unexpected.
127
- errorMessage = " Unexpected result returned from server "
128
- }
129
-
130
- // Log the success or failure to console.
131
- if ( errorMessage == nil ) {
132
- print ( " Successfully added user \( email) " )
133
- } else {
134
- print ( " Failed to add user \( email) : \( errorMessage!) " )
135
- }
136
-
137
- // Now deal with the UI.
138
- DispatchQueue . main. sync {
139
- guard self != nil else {
140
- return
141
- }
142
-
143
- // Always be sure to stop the activity indicator
144
- self !. activityIndicator. stopAnimating ( )
145
-
146
- // Present error message if any
147
- guard errorMessage == nil else {
148
- let alertController = UIAlertController (
149
- title: " Error " ,
150
- message: " \( errorMessage!) " ,
151
- preferredStyle: . alert
152
- ) ;
153
-
154
- alertController. addAction ( UIAlertAction ( title: " OK " , style: . cancel) )
155
- self !. present ( alertController, animated: true )
156
- return
157
- }
158
-
159
- // Otherwise, fetch new team members list
160
- self ? . fetchTeamMembers ( )
161
- }
162
- } )
119
+ // :hide-start:
120
+ app. currentUser ( ) !. functions. addTeamMember ( [ AnyBSON ( email) !] , self . onTeamMemberOperationComplete)
121
+ // :replace-with:
122
+ // // TODO: use the app's current user's functions object to call the addTeamMember function
123
+ // // on the backend with the given email converted to AnyBSON. Use `self.onTeamMemberOperationComplete`
124
+ // // as the completion handler.
125
+ // :hide-end:
163
126
}
127
+ // :code-block-end:
164
128
129
+ // :code-block-start: remove-team-member
165
130
func removeTeamMember( email: String ) {
166
131
print ( " Removing member: \( email) " )
167
132
activityIndicator. startAnimating ( )
168
- app. functions. removeTeamMember ( [ AnyBSON ( email) !] , { [ weak self] ( result, realmError) in
133
+ // :hide-start:
134
+ app. currentUser ( ) !. functions. removeTeamMember ( [ AnyBSON ( email) !] , self . onTeamMemberOperationComplete)
135
+ // :replace-with:
136
+ // // TODO: use the app's current user's functions object to call the removeTeamMember function
137
+ // // on the backend with the given email converted to AnyBSON. Use `self.onTeamMemberOperationComplete`
138
+ // // as the completion handler.
139
+ // :hide-end:
140
+ }
141
+ // :code-block-end:
142
+
143
+ private func onTeamMemberOperationComplete( result: AnyBSON ? , realmError: Error ? ) {
144
+ DispatchQueue . main. sync {
145
+ // Always be sure to stop the activity indicator
146
+ activityIndicator. stopAnimating ( )
147
+
169
148
// There are two kinds of errors:
170
149
// - The Realm function call itself failed (for example, due to network error)
171
150
// - The Realm function call succeeded, but our business logic within the function returned an error,
@@ -184,39 +163,24 @@ class ManageTeamViewController: UIViewController, UITableViewDelegate, UITableVi
184
163
// This is unexpected.
185
164
errorMessage = " Unexpected result returned from server "
186
165
}
187
-
188
- // Log the success or failure to console.
189
- if ( errorMessage == nil ) {
190
- print ( " Successfully removed user \( email) " )
191
- } else {
192
- print ( " Failed to remove user \( email) : \( errorMessage!) " )
193
- }
194
-
195
- // Now deal with the UI.
196
- DispatchQueue . main. sync {
197
- guard self != nil else {
198
- return
199
- }
200
166
201
- // Always be sure to stop the activity indicator
202
- self !. activityIndicator. stopAnimating ( )
167
+ // Present error message if any
168
+ guard errorMessage == nil else {
169
+ print ( " Team operation failed: \( errorMessage!) " )
170
+ let alertController = UIAlertController (
171
+ title: " Error " ,
172
+ message: errorMessage!,
173
+ preferredStyle: . alert
174
+ ) ;
203
175
204
- // Present error message if any
205
- guard errorMessage == nil else {
206
- let alertController = UIAlertController (
207
- title: " Error " ,
208
- message: " \( errorMessage!) " ,
209
- preferredStyle: . alert
210
- ) ;
211
-
212
- alertController. addAction ( UIAlertAction ( title: " OK " , style: . cancel) )
213
- self !. present ( alertController, animated: true )
214
- return
215
- }
216
-
217
- // Otherwise, fetch new team members list
218
- self ? . fetchTeamMembers ( )
176
+ alertController. addAction ( UIAlertAction ( title: " OK " , style: . cancel) )
177
+ present ( alertController, animated: true )
178
+ return
219
179
}
220
- } )
180
+
181
+ // Otherwise, fetch new team members list
182
+ print ( " Team operation successful " )
183
+ fetchTeamMembers ( )
184
+ }
221
185
}
222
186
}
0 commit comments