G'day everyone, welcome back.
In the last video, we saw how to use our AppDialog class
to get the user to confirm that they really do want to delete a record.
I'll add the function to AddEditFragment, just before saveTask, and call it isDirty.
After creating a new task, using the taskFromUi function that we used earlier,
the function returns true, if the new task doesn't equal a task that we're editing.
In the case of adding a task, task will be null and won't equal newTask.
That can cause a problem,
and the function deals with that by also checking that the name and description aren't empty,
and if the sort order has changed from the default of zero.
Without those checks, we'd get the dialogue if we tried to leave a new task, even if nothing had been typed.
Try removing that second compound condition, if you're not sure why it's necessary.
If newTask and task are equal, the user hasn't made any changes and the function returns false.
Alright. Back in MainActivity, we can use that function to decide whether to prompt the user or not.
There are two places we need to do that,
and it makes sense to create a function to show the dialogue.
I'm going to create it as a Kotlin extension function, and I'll talk about them in the video after next.
Create a new Kotlin file called ExtensionFunctions.
Kotlin extension functions can be great if you avoid the temptation to overuse them.
I'll discuss what we're doing here, once we've seen it working.
The function's code is simple enough. It just puts the required values into the bundle and creates a dialog.
Something we haven't done before, is used default values for the function arguments.
That's something that Kotlin lets us do, that you can't do in Java.
If we just want the default, OK and cancel buttons, we don't have to specify values for the positive and negative captions.
The default values will be used automatically.
You can find out more about them, in the Kotlin language reference
at https://kotlinlang.org/docs/reference/functions.html
Alright, I'll add the code to call this function in MainActivity.
As we've done before, create a constant to hold the dialog id.
We'll be using the confirmation dialogue in two places.
I'll deal with onOptionsItemSelected first.
We need to comment out this line, because we're going to replace it.
We're being quite defensive here, checking to make sure that the fragment is an AddEditFragment.
At the moment, there's only one fragment that could result in the Up button being displayed,
but if we add another fragment in the future, and try to cast it to be an AddEditFragment, we'll get an exception.
If you're going to cast something to another type, then it's a good idea to make sure it can be cast to that type.
Note that Kotlin's performed a smart cast for us, once we've checked that the fragment is the correct type.
We've got a couple of errors because we haven't created the string resources yet.
I'll get the light bulb to create all three.
The message resource will be called cancelEditDiag_message.
The positive caption will be called cancelEditDiag_positive_caption,
and the negative one will be cancelEditDiag_negative_caption.
It can take a couple of goes, sometimes, to get the lightbulb to appear for the last two, because of the error.
Click in the strings a couple of times if you need to,
until you can select the extract string resource option from the light bulb.
We also have to remove the getString calls, because we're passing the resource ID to the DialogFunction.
We can use exactly the same code in the onBackPressed function, so let's copy the entire if/else block,
and paste it in place.
Okay, I'll finish the video with a challenge.
Have a go at that, and I'll see in the next video.