1
00:00:00,106 --> 00:00:02,856
(cheerful music)

2
00:00:05,240 --> 00:00:07,340
Alright, so we talked about in the last video

3
00:00:07,340 --> 00:00:09,480
how storing state was a bad idea,

4
00:00:09,480 --> 00:00:11,630
because you'll often end up striking to keep

5
00:00:11,630 --> 00:00:13,570
your stored value up to date.

6
00:00:13,570 --> 00:00:15,330
So how do we go about fixing this

7
00:00:15,330 --> 00:00:16,770
so we're not doing that?

8
00:00:16,770 --> 00:00:18,067
Well firstly, for this particular app,

9
00:00:18,067 --> 00:00:22,610
I'm going to start by getting rid of the readGranted field

10
00:00:22,610 --> 00:00:24,530
and if you recall, we added that right at the top

11
00:00:24,530 --> 00:00:26,633
so I'm gonna comment that out on line 26.

12
00:00:28,290 --> 00:00:30,070
And as a consequence of that, we no longer

13
00:00:30,070 --> 00:00:32,930
need to set that value here so I'm gonna comment that out

14
00:00:32,930 --> 00:00:35,180
remembering that that was one of our to do's.

15
00:00:36,110 --> 00:00:37,720
Now I'm commenting that out.

16
00:00:37,720 --> 00:00:40,470
Now if you go down to the on request permissions result

17
00:00:41,914 --> 00:00:44,680
then to this function here on line 91,

18
00:00:44,680 --> 00:00:47,850
we need to remove all references now to readGranted.

19
00:00:47,850 --> 00:00:48,683
So let's do that.

20
00:00:48,683 --> 00:00:51,140
Now what I'm going to do firstly

21
00:00:51,140 --> 00:00:53,540
is I'm going to take a copy of this line

22
00:00:54,476 --> 00:00:56,393
and I'm going to comment that out.

23
00:00:57,440 --> 00:01:01,740
I'm going to remove the part of the code

24
00:01:01,740 --> 00:01:04,340
that was actually going to assign the value to readGranted

25
00:01:04,340 --> 00:01:07,750
and I'm gonna comment out the true and the false

26
00:01:07,750 --> 00:01:09,420
'cause we no longer need that anymore

27
00:01:09,420 --> 00:01:11,380
'cause we're going to start an explicit call there,

28
00:01:11,380 --> 00:01:13,780
we're not basically saving state of that

29
00:01:13,780 --> 00:01:15,540
or saving the state of that.

30
00:01:15,540 --> 00:01:18,520
Alright, now we've done that, we can change the condition

31
00:01:18,520 --> 00:01:20,160
in the fabs on click listener

32
00:01:20,160 --> 00:01:21,760
so let's go back up and do that.

33
00:01:23,150 --> 00:01:25,910
And here's our code there, starting on line 46.

34
00:01:25,910 --> 00:01:28,360
So I'm basically going to comment out that line

35
00:01:30,150 --> 00:01:34,023
and replace it with an iffy so if parentheses,

36
00:01:35,010 --> 00:01:40,010
ContextCompat dot checkSelfPermission.

37
00:01:40,350 --> 00:01:42,980
Then in parentheses, it's gonna do this,

38
00:01:42,980 --> 00:01:46,590
comma, then it's gonna be READ underscore Contacts,

39
00:01:46,590 --> 00:01:49,960
one right parentheses and that's equal to

40
00:01:49,960 --> 00:01:54,240
Packagemanager dot Permission underscore Granted,

41
00:01:54,240 --> 00:01:57,950
closing parentheses, and then we'll add our left curly brace

42
00:01:57,950 --> 00:01:59,680
to make that line valid now.

43
00:01:59,680 --> 00:02:02,530
Now, we'll talk a bit more about what we've done later,

44
00:02:02,530 --> 00:02:06,060
but first let's actually check and see that this works.

45
00:02:06,060 --> 00:02:09,889
So I'm going to stop this and we're going to go back

46
00:02:09,889 --> 00:02:13,280
to our emulator and we'll delete this app again.

47
00:02:13,280 --> 00:02:14,230
We'll uninstall it.

48
00:02:15,730 --> 00:02:18,510
I wanna do the same testing we did previously.

49
00:02:18,510 --> 00:02:19,530
Let's go ahead and do that.

50
00:02:19,530 --> 00:02:20,680
I'm gonna run it again.

51
00:02:22,840 --> 00:02:23,940
Bring the emulator up.

52
00:02:27,380 --> 00:02:29,040
Alright, so we're at the same position

53
00:02:29,040 --> 00:02:30,440
we were previously when we started

54
00:02:30,440 --> 00:02:31,940
testing in the last video.

55
00:02:31,940 --> 00:02:34,550
So our first test is to deny access

56
00:02:34,550 --> 00:02:36,720
so let's go ahead and do that.

57
00:02:36,720 --> 00:02:37,753
Permission refused.

58
00:02:39,040 --> 00:02:40,710
We've still got that snackbar.

59
00:02:40,710 --> 00:02:42,190
Still comes back.

60
00:02:42,190 --> 00:02:44,540
And if you come back and click on Grant Access,

61
00:02:45,870 --> 00:02:47,500
that works as you can see and you can see

62
00:02:47,500 --> 00:02:49,620
we've got our call sharing here

63
00:02:49,620 --> 00:02:54,440
and Request Permissions was called, as you can see.

64
00:02:54,440 --> 00:02:56,910
I can click on this time the box,

65
00:02:56,910 --> 00:02:59,713
I'll check that and click on Deny Access again.

66
00:03:01,740 --> 00:03:04,120
Permission refused appears again in the lockout,

67
00:03:04,120 --> 00:03:05,660
which we would expect.

68
00:03:05,660 --> 00:03:08,320
And we're gonna tap the fab again.

69
00:03:08,320 --> 00:03:10,340
We've got Grant Access showing

70
00:03:10,340 --> 00:03:13,970
and you click on that and we get taken to the App Settings

71
00:03:13,970 --> 00:03:15,810
so that's good, that's working okay.

72
00:03:15,810 --> 00:03:17,640
Now what I will do first is I'll click

73
00:03:17,640 --> 00:03:19,700
or use the back button to come straight back

74
00:03:19,700 --> 00:03:21,690
without granting access.

75
00:03:21,690 --> 00:03:23,570
That takes us back to our code

76
00:03:23,570 --> 00:03:25,880
and click on the fab again.

77
00:03:25,880 --> 00:03:27,950
It's still requesting access

78
00:03:27,950 --> 00:03:29,640
and taking us back to the settings, hopefully,

79
00:03:29,640 --> 00:03:30,930
when I click on Grant Access.

80
00:03:30,930 --> 00:03:32,050
Yes, it does.

81
00:03:32,050 --> 00:03:33,270
So that's working.

82
00:03:33,270 --> 00:03:35,980
If we go into the Settings, but don't grant access

83
00:03:35,980 --> 00:03:38,090
then the app still can't access the contacts,

84
00:03:38,090 --> 00:03:39,470
which is correct.

85
00:03:39,470 --> 00:03:41,570
If I click on the Grant Access link again,

86
00:03:41,570 --> 00:03:44,560
I'm gonna click on back first, open up fab,

87
00:03:44,560 --> 00:03:46,160
and click on Grant Access again.

88
00:03:47,430 --> 00:03:49,690
This time I go into Permissions.

89
00:03:49,690 --> 00:03:51,870
This time I will check or select

90
00:03:51,870 --> 00:03:54,660
the Contacts permission to give us access to that

91
00:03:54,660 --> 00:03:55,920
and now if I go back to the app

92
00:03:55,920 --> 00:03:58,753
by clicking on back a few times,

93
00:03:59,660 --> 00:04:02,350
I tap the fab and you can now see,

94
00:04:02,350 --> 00:04:04,280
we immediately have got access to our contacts.

95
00:04:04,280 --> 00:04:07,544
So that's now working fine now that we've removed

96
00:04:07,544 --> 00:04:09,770
the state within our application

97
00:04:09,770 --> 00:04:12,300
or removed saving of that state.

98
00:04:12,300 --> 00:04:14,640
Alright, so I'm gonna finish the video here.

99
00:04:14,640 --> 00:04:17,880
In the next one, I'm gonna talk a bit more about tests

100
00:04:17,880 --> 00:04:20,620
and how it's a good idea to create test scripts

101
00:04:20,620 --> 00:04:23,690
to make sure that you're covering all possible situations.

102
00:04:23,690 --> 00:04:25,390
So I'll see you in the next video.

