[MUSIC PLAYING]
MURAT YENER: Hi.
Welcome to the second episode of this Navigation series.
In the previous episode, we learned
how to use navigation UI, implemented a bottom navigation
bar in our app, and also edited a selection fragment
to enable or disable coffee tracking.
However, whether we disable or enable the coffee tracker,
users can still navigate to the coffee list fragment,
which doesn't seem quite right.
In this episode, we'll implement conditional navigation
and direct our users to make a selection when they launch
the app for the first time.
We'll use the data store API to purchase the user's selection
and use this to decide, showing the coffee destination
in the button navigation.
OK, enough talking.
Let's dive in and see how to do that.
To follow along, you can check out the code
from this report, which I linked in the notes below.
Let's do a quick review of changes
I made since last episode.
First, I edited the user preferences repository,
which uses the data store API to persist the user's selection.
To access the repository, I made some changes in view model
factories and how donut list and selection view
models are constructed.
The app now can be in three different states--
donut only, which means the user has
disabled the coffee tracking functionality, donut
and coffee, which means the user wants to track both donut
and coffee consumption, and finally not selected,
which means the user hasn't made a selection yet
and is possibly running the app for the first time,
or maybe they just have a hard time making up their mind.
Now that we are caught up, I'll start
implementing conditional navigation within the selection
fragment.
First, I get an instance of selection view model,
so I get access to data store.
Next, I observe the user's selection
and use it to restore the status of the checkbox.
I also update the state when the checkbox
is clicked by calling save coffee tracker selection
function.
This will persist the user's selection.
Now it's time to update the bottom tabs
with the user's selection.
If the user chooses to disable the coffee tracking,
the only option left in the bottom tabs
will be the donut list, which means we can safely
remove the bottom tabs.
In main activity, I add an observer
and update the visibility of the bottom navigation,
according to user's selection.
Running tab in this state, you'll
see that enabling or disabling the coffee tracker
adds or removes the bottom tabs from the app.
This is great, but wouldn't it be nice if we automatically
sent the user to make a selection when they
ran the app for the first time?
Donut list is the default fragment
in our start destination.
This means the app always starts with the donut list.
I check if the user previously made the selection and trigger
navigation to selection fragment if they haven't.
Before I test this, I uninstall the app from the device,
so that I'm sure there are no saved preferences
from my previous runs.
Now when I run the app, it takes me to the selection fragment.
Later launches of the app will remember the selection I made
and navigate me to the correct start destination.
And that's it.
We edit conditional navigation to the donut tracker app,
but how can we test this flow?
Deleting the app or the app data each time
before running the test doesn't seem so ideal.
This is where testing comes to rescue.
I create a new class called one time flow
test in Android test folder.
Then I create a test called test first run
and [? annotate ?] it with a test.
Now I can start implementing the test.
First, I create a test nav host controller
by using the application context.
I also set the nav grab from the app
to the test navigation controller
instance I just created.
Now that the mock navigation controller is ready to use,
it's time to create the scenario.
To do that, I start the app with the donut list fragment
and set the mock navigation controller instance
I created before.
Then check if the app automatically
navigates to the selection fragment, as expected.
Now I run the test and wait for the results.
As you can see, the test passes with flying colors,
or, well, maybe just green.
In this episode, I added conditional navigation
to the donut tracker app and also added a test
to verify that the flow works.
With conditional navigation, the donut tracker app
will trigger the one-time flow to take the users
to the selection fragment when they launch
the app for the first time.
If the user chooses to disable the coffee tracker,
the app removes the coffee list from the navigation menu.
Now the coffee tracker functionality is complete.
In the upcoming episodes, we'll learn how to use nested graphs
and modularize this app.
Thanks for watching.
See you in the next episode.
[MUSIC PLAYING]