Welcome back, everyone.
Another change we wanted to make
is to update the display when we delete a task
that's currently being timed.
Let's see the problem you're trying to solve then you can have a go at fixing it.
Run the app, and make sure we're not timing anything.
At the top of the screen, we've got the message "No task being timed".
Long tap a task to start timing it.
The display updates and now we're currently timing task 1.
The problem we want to fix is what happens when I swipe the task left
to delete it.
When I do that and tap the delete button to confirm,
the display doesn't change.
We're still currently timing task 1.
Of course, we're not,we just deleted it.
The fix is easy,
and the only change you need to make is in our TaskTimerViewModel.
As long as you've taken the time to understand how all this works
and how MainActivity is observing changes in the ViewModel,
this is an easy change to make.
So your challenge is to fix the code in TaskTimerViewModel
to display the correct message when we delete the task that we're timing.
Pause the video now, and I'll go over my solution when you come back.
Welcome back. How did you get on?
You should have been able to fix the display with just two lines of code
inside a condition.
Let's review how all this works before writing any code.
Open MainActivity.
The display is updated in MainActivity
by observing the timing LiveData object in the ViewModel.
When the ViewModel deletes a task,
it should check to see if the task is being timed.
It can then update its LiveData object to allow MainActivity
to update the display.
We add the code in the TaskTimerViewModel's DeleteTask function.
We're storing the task that's being timed in current timing,
that may be null so we need a safe call operator.
That's on line 135.
If current timing's task ID is the same as the ID of the task
being deleted,
we update the TaskTimingLiveData object.
Setting it to null will let MainActivity know that nothing's being timed.
We also set current timing to null.
If we've just deleted the task
then, we're obviously not still timing it.
I'll go into code, reformat the code, and then run the app again to test it.
StartTiming a task then swipe to delete.
The first test I'll perform is to cancel the deletion.
This should restore the task and we should still be timing it,
tap cancel and the task reappears and it's still being timed,
so that works.
Repeat the operation but this time tap the dialog's delete button.
Task is deleted, and the display updates.
There are other tests you should perform.
Make sure that deleting a task that isn't being timed, also still works.
You'll want to perform that test twice too,
make sure that both the cancel and the delete work
when the confirmation dialog appears.
In the next video, we'll have a look at how we can implement
parameterized queries in SQLite. I'll see you there.