(cheerful music)
Alright, so we talked about in the last video
how storing state was a bad idea,
because you'll often end up striking to keep
your stored value up to date.
So how do we go about fixing this
so we're not doing that?
Well firstly, for this particular app,
I'm going to start by getting rid of the readGranted field
and if you recall, we added that right at the top
so I'm gonna comment that out on line 26.
And as a consequence of that, we no longer
need to set that value here so I'm gonna comment that out
remembering that that was one of our to do's.
Now I'm commenting that out.
Now if you go down to the on request permissions result
then to this function here on line 91,
we need to remove all references now to readGranted.
So let's do that.
Now what I'm going to do firstly
is I'm going to take a copy of this line
and I'm going to comment that out.
I'm going to remove the part of the code
that was actually going to assign the value to readGranted
and I'm gonna comment out the true and the false
'cause we no longer need that anymore
'cause we're going to start an explicit call there,
we're not basically saving state of that
or saving the state of that.
Alright, now we've done that, we can change the condition
in the fabs on click listener
so let's go back up and do that.
And here's our code there, starting on line 46.
So I'm basically going to comment out that line
and replace it with an iffy so if parentheses,
ContextCompat dot checkSelfPermission.
Then in parentheses, it's gonna do this,
comma, then it's gonna be READ underscore Contacts,
one right parentheses and that's equal to
Packagemanager dot Permission underscore Granted,
closing parentheses, and then we'll add our left curly brace
to make that line valid now.
Now, we'll talk a bit more about what we've done later,
but first let's actually check and see that this works.
So I'm going to stop this and we're going to go back
to our emulator and we'll delete this app again.
We'll uninstall it.
I wanna do the same testing we did previously.
Let's go ahead and do that.
I'm gonna run it again.
Bring the emulator up.
Alright, so we're at the same position
we were previously when we started
testing in the last video.
So our first test is to deny access
so let's go ahead and do that.
Permission refused.
We've still got that snackbar.
Still comes back.
And if you come back and click on Grant Access,
that works as you can see and you can see
we've got our call sharing here
and Request Permissions was called, as you can see.
I can click on this time the box,
I'll check that and click on Deny Access again.
Permission refused appears again in the lockout,
which we would expect.
And we're gonna tap the fab again.
We've got Grant Access showing
and you click on that and we get taken to the App Settings
so that's good, that's working okay.
Now what I will do first is I'll click
or use the back button to come straight back
without granting access.
That takes us back to our code
and click on the fab again.
It's still requesting access
and taking us back to the settings, hopefully,
when I click on Grant Access.
Yes, it does.
So that's working.
If we go into the Settings, but don't grant access
then the app still can't access the contacts,
which is correct.
If I click on the Grant Access link again,
I'm gonna click on back first, open up fab,
and click on Grant Access again.
This time I go into Permissions.
This time I will check or select
the Contacts permission to give us access to that
and now if I go back to the app
by clicking on back a few times,
I tap the fab and you can now see,
we immediately have got access to our contacts.
So that's now working fine now that we've removed
the state within our application
or removed saving of that state.
Alright, so I'm gonna finish the video here.
In the next one, I'm gonna talk a bit more about tests
and how it's a good idea to create test scripts
to make sure that you're covering all possible situations.
So I'll see you in the next video.