Welcome back.
Now we've created the menu for our reports activity,
the next step is to add the code in durations report.
I'll get android studio to generate the stubs,
just after the OnClick function.
We need OnCreateOptionsMenu
and OnOptionsItemSelected.
OnCreateOptionsMenu
just inflates our menu resource, so that's easy.
The work of responding to taps on the menu
will be done in OnOptionsItemSelected.
I'm going to take a top-down approach here.
We'll be calling functions that we haven't yet written.
Obviously, that will give us errors.
But they'll come right when the functions have been written.
I'll delete the nullable type marker,
the question mark
because this function can't be called by selecting something that's null.
I've added branches for the other 2 menu options,
we'll be implementing them later.
The only slightly tricky bit in the code
is the way we're going to toggle the first menu item.
When it's tapped, I'll switch between displaying a single day
or displaying a full week.
Our ViewModel will track whether the report should display a week
or just a single day
and will toggle that true or false when the menu item's tapped,
that call to invalidate options menu
will cause the menu to be redrawn,
which will cause it to display the new menu icon after we've toggled it.
Use ctrl Q or ctrl J on a mac
to see what invalidate menu options does.
When we call invalidate options menu,
it tells android that the menus changed and needs to be redrawn.
When android redraws it, it'll use the other menu icon.
Android doesn't know which icons we want to use of course,
so we have to tell it. We'll do that in
OnPrepareOptionsMenu.
I'll get android studio to generate a stub for that function below
OnOptionsItemSelected,
then add the code. I'll also remove the nullable type marker
from the menu parameter.
It's not doing anything clever,
just swapping the icon and the text for the menu item.
The OnPrepareOptionsMenu function
is the place to make changes to the appearance of menus,
setting the icon and title in on options item selected doesn't work.
All our code is doing is swapping the menu icon and title.
If we're displaying a weak starter, the icon will show one day,
and the title will change to show what will happen when the menu item's tapped.
If display week returns false,
then tapping the menu will cause a week start to be shown,
and the icon title are changed to reflect that.
If that sounds backwards, the icon shows what will be displayed,
not what's currently being displayed,
that will make more sense when you see it running.
We can't run the app until we've made some changes to the ViewModel.
We need to implement the ToggleDisplayWeek function
and add the display week property.
I'll start with the property.
This is a good opportunity to use a read-only property.
We don't want the calling code to set it directly.
We only want to provide the ability to toggle it.
If it was true, it becomes false and vice versa.
I'll add the property after sort order before our initial code.
Code that uses this view model can access the value of display week
but can't change it.
To do that, it'll have to call the ToggleDisplayWeek function.
I'll add that after the init block.
Okay. We should be able to run the app and see the icon change.
Long tapping on a menu icon will display the title.
That's useful for the users
and also lets us make sure that we are setting things correctly.
When we are displaying a single day,
the icon changes to represent a week
and the text changes to show week.
It reflects what tapping the icon will do.
The report doesn't update yet, we're just getting the menu working first.
Now that the menu is working, we need to provide some way for the user
to select the date to display.
We can't write the code to filter the report data
until we've got some way to provide new dates.
In the next video, we'll see how to use a DatePickerDialogue to do that.
I'll see you there.