Welcome back. In our duration's report activity,
we've still got the delete menu option to implement.
The first step is to show the DatePickerDialog.
I'll use the light bulb to convert the dialog title to a string resource
called date_title_delete.
Deleting the rows is fairly straightforward.
We just tell the content provider to delete
all records earlier than a certain date.
A slight complication is we want to get confirmation first.
The user could potentially be deleting thousands of records.
It's only fair to warn them first.
What we'll do is respond to the date they choose in our
OnDateSet function,
that will use our AppDialog class to display the confirmation dialog,
and we'll perform the deletion in the
OnPositiveDialogResult function.
Let's start with the OnDateSet function
in our durations report activity.
When OnDateSet's called,
we're provided with the year,
month and day that a user selected from the DatePicker.
Rather than just go ahead and delete all the rows before that date,
we'll pop up a dialog
to let the user confirm that they really want to delete the rows.
Displaying the dialog is easy, we've done it quite a few times now.
A slight complication is that we should show the date
in the user's preferred format.
That should be easy, shouldn't it.
We've already done something similar in the DurationsViewModel class.
Unfortunately, the date format API
doesn't provide a suitable function when we haven't got a date.
We've got three values: the year, month and day.
Before we can format that in a way that the user is familiar with,
we need to convert those three values into a date.
If you're prompted for the imports, the Gregorian calendar
comes from java.util
There is a Gregorian calendar class in android.icu.util,
but that requires a minimum SDK of 24.
Our app runs on all android versions back to 17,
so we can't use that class.
The date format class we want is in android.text.format.
We create a new Gregorian calendar object
and use that to convert our three values into a date.
In this case, the time in milliseconds.
That gives us something we can pass to date format's format function.
Now we've got a string that makes sense to the user.
All right. The remaining code
sets up the arguments for our dialog.
The light bulb doesn't give us the option to extract the message
into a string resource.
We have to edit strings.xml,
and add the resource manually.
Cut all the text inside the speech marks without the speech marks,
then open strings.xml.
I'll call the new resource
delete_timings_message.
We also need to change the placeholder.
At the moment, it's using a Kotlin variable,
and we need a resource placeholder.
Back in our code, we'll use the resource ID.
Next, we put the date value,
the full date time in milliseconds into the args bundle.
We're going to need that later when we react to the positive dialog button
and perform the deletion.
I've used a constant to store the value in the args bundle.
We'll use the same constant to get the value back out again
so I'd better declare it up at the start of the file.
All right. If the user confirms the deletion,
app dialog will call our on PositiveDialogResult function.
I'll make the class implement the dialog events interface.
We'll get an error until we implement the interface functions.
Android studio can generate the stub for the implementation function
just below the OnDateSet function,
then I'll add the code.
If we get confirmation
and the dialog calls OnPositiveDialogResult,
we can tell the view model to delete the records.
We'll get an error until we write the delete function
in the DurationsViewModel class, of course,
I'll put it right at the end.
That's the code finished.
In the next video, we'll run the app and test it.
I'll see you there.