From the course: Shared Economy for iOS Development

Assigning to delivery - iOS Tutorial

From the course: Shared Economy for iOS Development

Start my 1-month free trial

Assigning to delivery

- [Instructor] Currently we're fetching all the requests but we want to filter out the requests that we didn't make that doesn't make any sense. So we're going to add a filter just like we did in the ViewController, so I'm going to go to the ViewController, fetchUsers function and copy that code where we created the filter lines 82 to 84. Go back to the RequestTableViewController I'm going to paste these in and we're going to change it up a little bit. So we still want it to not equal our name but it's not the user and it's not the AppUserFilter, it's the ServiceRequestFilter. And it's not username it's custUName. So we only want to see the requests from customers that are not us. We can't pay ourselves to mow the lawn or it wouldn't make any sense if we did. Also we want to implement the function for accepting a job in the RequestDetailViewController here. But there really isn't any field to change in the database on the server for when we accept it but we can change that it's not that difficult. So we're going to open up the terminal and we're going to edit our scheme again. We're going to add a field down here at the bottom of the ServiceRequest. So in VI I'm going to press shift+A that gives me insert mode at the end of the line and I'm going to put accepted as the field and it's a Boolean. So I'll hit escape to get in command mode+:+WQ to quit. And then I'm going to do amplify push, it has me verify that the API has been updated and yes I want to continue. It'll ask me the same questions before about generating the code and so on and I'll just accept the defaults. While it's doing it's job I'm going to switch back to Xcode and make the same change there in our class. In our ServiceRequest class I'm going to add another property called accepted just like we did for the server and I'll default its value to false. Now when we create one of these we have to set it and also when we fetch them they'll automatically be parsed. So let's look at our MowerDetailsViewController where we create the request. So we have the input here, we also want to specify the accepted, we don't have the generated code yet for that, let's see how it's doing in the terminal. Okay, so it's done, let's go look at the code. So now we should have the accepted property for creating the ServiceRequest. Create ServiceRequest there it is accepted. So I'm going to copy that, paste it here and have its default to false. Let's build make sure that this is okay and it looks pretty good. Now we just need to set it to true when the user taps the accept button. Since we're only testing with one user, to test that I can accept a request, I'm going to comment out the filter so we can see even our own requests for now. And then in the doButtonAccept we're going to set the accept to true. So we have the ServiceRequestID, let's just make sure by unwrapping that. So I'm going to say let sId equal serviceRequest.ID else return. And now I'm going to create the input mutation and perform it. So let me say let input equal, so I'll use the UpdateServiceRequestInput and I'm only going to pass in the ID that we just got and that accepted is now true. And then for the mutation we use that input. So on line 50 I'll type let mut UpdateServiceRequestMutation and it takes the input and then I need to perform it with the appSyncClient. We don't have a property for that here, we could pass it in or use it as a singleton somehow but just for now I'm going to go straight to the UIApplication, shared instance, get the delegate, test it as the AppDelegate and get the appSyncClient. And with that I'll perform and I'll pass in the mutation, mut, I'm going to delete the other parameters except for the resultHandler. So I'll highlight that hit Return get my result get my error and print those out. So we'll print the error, print result. Now before we run this let's delete our old request from the database, since it doesn't have the accepted column it's going to throw off our parsing and not get listed. So I'll go to DynamoDB select that row and delete it. We could modify it and add the accept column or we could handle it in our code but for now we're just going to delete it. So now I'll run the code, I'll put a breakpoint here in the returned after we perform our mutation. Now we see our users, I'm going to tap on one and create a new request. Let's go see that in the database. I'll refresh the table and there's our new ServiceRequest with accept being false. Now back in the simulator I'll tap on mower to see the request tap on it and accept it. We see that we don't have an error, we continue to run and now let's check it in the database. I'll refresh the table again and the accept is true. So now we have an agreement to come mow the yard.

Contents