Welcome back. Did you find the bug?
More importantly, have you fixed it?
I'll start by demonstrating the problem. Run the app,
and switch to landscape mode.
Then edit a task,
then swipe it.
We don't get the confirmation dialog.
That's because we've added code to only proceed with the deletion
if the task isn't also being edited.
We get the Toast message instead.
So nothing gets deleted, and there's no dialog asking for confirmation.
But the task still disappears from the list.
When we're using a button, the OnDeleteClick function
was a good place to test the task ID
to make sure we weren't deleting the task that was being edited.
Now that we're swiping, we've got a bit more work to do.
Swiping removes the tasks from the display, so we need to put it back.
I'll cut that test out of OnDeleteClick.
Use code reformat code to tidy up the formatting.
Next, paste the code into the OnSwiped function
just before the call to OnDeleteClick.
That's put the test in the right place.
We only call OnDeleteClick
if the task to be deleted isn't also being edited.
To restore the task in the RecyclerViewList,
we call NotifyItem Changed on the adapter.
That's the same as we did when the user cancels the deletion from the dialog.
I'd like to call NotifyItemChanged
before showing the toast message but it also works if you put it afterwards.
Okay. I'll run the app and test what happens now.
The animation still runs but then the item reappears
and we get the toast message.
That's the challenge completed,
and now we've implemented delete by swiping.
We're using a long tap to start timing a task.
So if you wanted to remove the edit button
and allow tapping a task to edit it, then that could be a good exercise.
I won't be doing that on video, but you should find it's an easy thing to implement.
Apart from deleting the button from the layout,
the only changes will be in the adapter. In fact,
only one word needs to be changed.
Any other changes are only necessary because the button's been deleted from the layout.
Before I finish this video though, there's one last change to make.
It's a very easy change to forget but it's important.
Remember when we first run the app,
we display some instructions on the screen.
Those instructions are now incorrect,
they mention a delete button and we've removed it.
Go into res values string.xml
and find the instructions tag.
I'll edit that to more accurately reflect how the program now works.
To test the message, you should delete all your tasks
and make sure you get the correct instructions
or you could run the app on a new emulator if you don't want to type all the tasks in again.
In the next video, we'll look at the other change we wanted to make.
We need to deal with deleting a task that's being timed. I'll see you over there.