Mutation and update cache

Now let's do the integration part. Open TaskFragment.kt and add the following code to define the delete mutation

githubTaskFragment.kt
private fun removeTodoMutationCloud(todoId: Int){
// Init Query
removeTodoMutation = RemoveTodoMutation.builder().id(todoId).build()
// Apollo runs query on background thread
Network.apolloClient.mutate(removeTodoMutation)?.enqueue(object : ApolloCall.Callback<RemoveTodoMutation.Data>() {
override fun onFailure(error: ApolloException) {
Log.d("Todo", error.toString() )
}
override fun onResponse(@NotNull response: Response<RemoveTodoMutation.Data>) {
// get data from local cache and update the list
val index = listItems?.indexOfFirst { todo -> todo.id() == todoId}
var todos = (listItems?.toMutableList())?.removeAt(index!!)
Network.apolloClient
.apolloStore()
.write(GetMyTodosQuery(), GetMyTodosQuery.Data(mutableListOf(todos))).execute()
getMyTodosQueryLocal()
}
})
}

We have a function defined to handle the onClick of delete icon to remove a todo. Let's update the function.

override fun delete(taskId: Int) {
- // Todo : Method for deleting a task
+ removeTodoMutationCloud(taskId)
}