1
00:00:00,660 --> 00:00:06,780
Welcome back and this video we are going to use to storage and we're also going to update the user,

2
00:00:06,930 --> 00:00:09,770
and before we do that, I would like to talk about two things.

3
00:00:10,140 --> 00:00:12,560
One of them is the concept of a hash map.

4
00:00:12,960 --> 00:00:17,550
The concept of a hash map is also something that is called dictionary and other programming languages

5
00:00:17,550 --> 00:00:18,390
such as Swift.

6
00:00:18,660 --> 00:00:27,480
And the idea is that you have a pair of a key and a value, but this whole thing is stored in a hash

7
00:00:27,480 --> 00:00:29,010
map instead of in an object.

8
00:00:29,070 --> 00:00:35,100
OK, so what we have done is we stored stuff in an object, so we created an extra user class for the

9
00:00:35,130 --> 00:00:36,150
user information.

10
00:00:36,420 --> 00:00:39,840
But in some cases you don't need to pass on the whole object.

11
00:00:39,840 --> 00:00:44,930
But it makes sense to pass in a hash map and it is even required in some cases.

12
00:00:45,150 --> 00:00:46,860
So we need to look into hash maps.

13
00:00:47,170 --> 00:00:55,860
So the idea is that you, for example, have a string as the key and then a value of the type any as

14
00:00:55,860 --> 00:00:56,820
the value itself.

15
00:00:57,030 --> 00:01:05,069
So in our case, we will have any as the type that is stored within the hash map and that will be the

16
00:01:05,069 --> 00:01:06,420
value type, so to speak.

17
00:01:06,780 --> 00:01:12,750
OK, so we have the key email and the value then it's a that come then we have the key FCMB token.

18
00:01:13,110 --> 00:01:19,140
The value is an empty string, then the ID image and then here the mobile is a different again.

19
00:01:19,140 --> 00:01:28,920
So we have to string as the key type and then the value type is the loan value here that we can see

20
00:01:28,920 --> 00:01:29,940
here, which is a number.

21
00:01:31,720 --> 00:01:37,450
We will see later on when that makes sense, and I think I will show you a little more then then it

22
00:01:37,450 --> 00:01:39,400
will be a lot easier to understand.

23
00:01:39,400 --> 00:01:42,520
I think the other thing that we're going to use is the storage.

24
00:01:42,700 --> 00:01:43,030
All right.

25
00:01:43,030 --> 00:01:49,420
So far, we have used authentication database and now it's time to use storage and you can simply get

26
00:01:49,420 --> 00:01:49,880
started.

27
00:01:49,900 --> 00:01:55,990
So here, store and retrieve user generated files like images, audio and video without server side

28
00:01:55,990 --> 00:01:56,320
code.

29
00:01:56,560 --> 00:01:58,950
So this super powerful, very helpful.

30
00:01:58,960 --> 00:02:03,730
We can just go ahead and store the images that we have and the storage.

31
00:02:04,180 --> 00:02:04,570
All right.

32
00:02:04,690 --> 00:02:06,030
So let's get started with it.

33
00:02:06,610 --> 00:02:10,919
You can see you have to have the security rules accordingly.

34
00:02:12,950 --> 00:02:18,890
And at one point, so after you define your data structure, you will need to write rules to secure

35
00:02:18,890 --> 00:02:19,430
your data.

36
00:02:19,460 --> 00:02:20,890
You should definitely check this out.

37
00:02:21,230 --> 00:02:25,730
But for now, we can just press next and we can now use it.

38
00:02:25,730 --> 00:02:30,000
And here the cloud storage location is going to be the same as we have selected before.

39
00:02:30,290 --> 00:02:34,640
So now I'm going to press done and it's creating a default bucket for us.

40
00:02:34,670 --> 00:02:40,250
So now we can go ahead and start storing stuff in our firebase storage.

41
00:02:42,970 --> 00:02:45,880
You can upload files from here directly if you want to.

42
00:02:46,450 --> 00:02:49,680
You can see that there are certain rules that you can define.

43
00:02:49,690 --> 00:02:55,000
So if you want to know more about the rules of you, the documents and check those out and a lot more

44
00:02:55,000 --> 00:02:55,380
depth.

45
00:02:56,020 --> 00:03:00,850
But what is interesting for us is going to be to upload files.

46
00:03:00,850 --> 00:03:06,250
By the way, we also have here usage where we can see how much bytes are stored, how many objects there

47
00:03:06,250 --> 00:03:09,960
are, the bandwidth that was used so far and everything.

48
00:03:09,970 --> 00:03:10,330
All right.

49
00:03:10,600 --> 00:03:16,480
So let's go ahead and upload files, but of course, not by using it manually, but via code.

50
00:03:17,920 --> 00:03:24,400
Therefore, we need to go to our gradle and add an implementation here, so.

51
00:03:25,640 --> 00:03:32,960
Let me add this implementation, which is come that Google, that firebase Colen fire based on historic

52
00:03:32,960 --> 00:03:34,840
cold in 1931, that one.

53
00:03:36,020 --> 00:03:36,410
All right.

54
00:03:36,620 --> 00:03:38,870
So once you have added that, let's think it.

55
00:03:40,890 --> 00:03:42,810
And once it sink, we can use it.

56
00:03:43,200 --> 00:03:47,080
So now let's go back over to our my profile activity in here.

57
00:03:47,280 --> 00:03:54,090
I will create a new function which will help me to understand the file extension of a file that we have

58
00:03:54,090 --> 00:03:58,170
downloaded because a file could have multiple different extension types.

59
00:03:58,170 --> 00:03:58,440
Right.

60
00:03:58,440 --> 00:04:03,810
And it's interesting for us, because if it's an image type, then we can use it as an image.

61
00:04:04,080 --> 00:04:08,510
If it's another type, then we cannot use it as an image, for example, not as a profile image.

62
00:04:08,820 --> 00:04:15,510
So we're just going to go ahead and create a function which will get the file extension based on the

63
00:04:15,510 --> 00:04:16,170
URAI.

64
00:04:16,680 --> 00:04:18,660
So we get a your eye, which is a link.

65
00:04:18,660 --> 00:04:19,019
Right.

66
00:04:19,230 --> 00:04:23,670
And we can see is it an image or whatever type is it.

67
00:04:24,510 --> 00:04:24,990
All right.

68
00:04:25,020 --> 00:04:28,920
So we can return a string or we can return nothing.

69
00:04:29,380 --> 00:04:29,730
Right.

70
00:04:29,760 --> 00:04:30,580
It really depends.

71
00:04:31,050 --> 00:04:33,360
Now, you see, we, of course, need to return something.

72
00:04:33,570 --> 00:04:37,900
And what we will use is we will use the mime type class.

73
00:04:37,920 --> 00:04:44,250
So return mime type map, get Singleton.

74
00:04:46,660 --> 00:04:49,630
Thought, Oh, here, get.

75
00:04:50,660 --> 00:04:58,580
Extension and by the way, we need to import MYM type map as to that real quick and then get extension

76
00:04:59,210 --> 00:05:00,410
from my type.

77
00:05:02,190 --> 00:05:11,670
And here we can pass into your right, so content resolver, get type with the URL that we pass in and

78
00:05:11,670 --> 00:05:14,400
it's our you or I or it's all.

79
00:05:14,400 --> 00:05:16,370
Let's look over what's happening here.

80
00:05:16,410 --> 00:05:17,610
So MYM type map.

81
00:05:18,810 --> 00:05:25,620
The two way map that maps modern times to file extensions and vice versa, so it's basically a clause

82
00:05:25,620 --> 00:05:32,790
which allows us to find out the type of a you're right, then we use get Singleton in order to get an

83
00:05:32,790 --> 00:05:36,420
instance of this mind type map class so that we can use its functions.

84
00:05:37,200 --> 00:05:40,200
Then we use to get extension from a mine type.

85
00:05:40,230 --> 00:05:45,140
So we basically find out extension for the given mine type.

86
00:05:45,150 --> 00:05:48,430
So we just go ahead and say, OK, use this.

87
00:05:48,690 --> 00:05:54,970
I get to type of the you or I used the content resource over here from the context.

88
00:05:55,400 --> 00:06:00,150
So basically we just say, OK, use this URAI here and then return the.

89
00:06:01,160 --> 00:06:05,820
Extension, so the type of it or find it out based on the extension.

90
00:06:05,840 --> 00:06:11,720
So if we have a file, which is let's just use the Homer Simpson example again.

91
00:06:13,040 --> 00:06:17,690
So he it's stored right here, let's use this value.

92
00:06:19,680 --> 00:06:26,650
So you can see it has to start P and G, so this is the extension is the ending of the file.

93
00:06:27,000 --> 00:06:30,740
It starts with a dot so we know, OK, it's of type and G.

94
00:06:31,560 --> 00:06:33,000
So this will be an image.

95
00:06:33,000 --> 00:06:38,250
If it's of type three, then it will be an audio file if it's of type MyFord and it will be a video

96
00:06:38,250 --> 00:06:38,580
file.

97
00:06:38,910 --> 00:06:39,390
All right.

98
00:06:40,860 --> 00:06:48,870
So basically, we're passing in the whole link, this GPS and so forth, that PNG and we will get a

99
00:06:48,870 --> 00:06:57,060
return type, which will be of type string, and it will basically give us the extension type.

100
00:06:57,510 --> 00:06:58,030
OK.

101
00:06:58,770 --> 00:06:59,430
All right.

102
00:06:59,430 --> 00:07:02,760
Now it's time to upload something to our storage.

103
00:07:02,760 --> 00:07:07,350
So private phone upload user images.

104
00:07:07,420 --> 00:07:08,610
I'm going to call this function.

105
00:07:09,090 --> 00:07:15,570
And what it should do is it should, first of all, show the progress, dialogue, social progress,

106
00:07:15,570 --> 00:07:18,600
dialogue with our please wait text.

107
00:07:18,600 --> 00:07:20,250
So please wait.

108
00:07:21,300 --> 00:07:27,600
Actually, we have to get it from the resources, so here we sources get string.

109
00:07:28,690 --> 00:07:34,930
Heartstring thought, and here was something like, please wait.

110
00:07:35,430 --> 00:07:36,470
Yeah, there it is.

111
00:07:36,970 --> 00:07:40,860
So we have this string, right, which is called please wait, this one.

112
00:07:41,170 --> 00:07:46,620
So of course, we want to make our application easily extendable and adjustable.

113
00:07:46,630 --> 00:07:48,160
So that's why we used this approach.

114
00:07:48,460 --> 00:07:51,070
OK, so we showed a progress dial up and then.

115
00:07:52,440 --> 00:08:01,350
If the selected image file eye is not empty, so if I am selected image file your eye, that's the variable

116
00:08:01,350 --> 00:08:03,550
that were created in the last video.

117
00:08:04,050 --> 00:08:06,900
If that is not now, then do something with it.

118
00:08:07,290 --> 00:08:12,870
So basically, you want to then store it in our storage, OK?

119
00:08:13,070 --> 00:08:16,650
And in order to do that, we need to have a storage reference, first of all.

120
00:08:17,010 --> 00:08:17,370
All right.

121
00:08:17,370 --> 00:08:22,530
So we use VUL SRF, which stands for storage.

122
00:08:22,800 --> 00:08:28,960
Referents will be our firebase and is, by the way, to type.

123
00:08:29,010 --> 00:08:30,270
It's not equal sign here.

124
00:08:30,510 --> 00:08:32,370
Colon storage reference.

125
00:08:32,370 --> 00:08:33,450
We need to import that.

126
00:08:33,630 --> 00:08:34,880
It's also from Firebase.

127
00:08:34,890 --> 00:08:35,130
Right.

128
00:08:35,150 --> 00:08:44,100
So this is a class which is given by Firebase and we can go ahead and use Firebase Storage now to get

129
00:08:44,100 --> 00:08:45,060
the instance of it.

130
00:08:47,250 --> 00:08:49,980
And here in Port Firebase Storage as well.

131
00:08:52,670 --> 00:08:56,490
Or write it correctly like this, OK, then you don't need to.

132
00:08:56,510 --> 00:08:57,830
Well, actually, we need to import it.

133
00:08:57,860 --> 00:08:58,510
Let's do that.

134
00:09:01,280 --> 00:09:03,050
Then you enter reference.

135
00:09:04,250 --> 00:09:05,420
Thought child.

136
00:09:06,980 --> 00:09:13,190
So this is basically the structure that you will need every time if you want to store something, so

137
00:09:13,190 --> 00:09:20,090
you need a storage reference first, then you get the child of that firebase storage instance.

138
00:09:21,470 --> 00:09:27,320
And here you can pass in the name of the image that you want to then use.

139
00:09:27,540 --> 00:09:35,540
OK, so in our case, what I want to do is I want to call this one user image.

140
00:09:37,220 --> 00:09:48,770
Plus system, that current time in milliseconds, plus a dot and then the file extension, so get a

141
00:09:48,770 --> 00:09:57,940
file extension based on our M and how do we call it M selected image urai.

142
00:09:58,820 --> 00:10:02,690
OK, so let's look at it in one line so it's a little more readable.

143
00:10:03,380 --> 00:10:07,540
So basically what we want to have is an image which has a very unique name.

144
00:10:07,910 --> 00:10:09,760
So it's going to be called user image.

145
00:10:10,430 --> 00:10:17,060
Then it's going to take the current time in milliseconds and then it will add the file extension.

146
00:10:18,490 --> 00:10:24,920
OK, so this will generate a rather unique value for us so that each image has a unique name.

147
00:10:25,630 --> 00:10:29,160
So it's going to be whatever disk will return.

148
00:10:29,170 --> 00:10:31,870
So it's going to return that time in milliseconds.

149
00:10:31,870 --> 00:10:32,080
Right.

150
00:10:32,100 --> 00:10:35,200
So it's going to be a very long, long value.

151
00:10:35,690 --> 00:10:41,050
OK, plus, I don't intend to file extension that we get from our selected image file.

152
00:10:41,080 --> 00:10:42,960
You're right that we have stored on our device.

153
00:10:43,390 --> 00:10:50,650
So that's what we use to get file extension function for so that we get the extension, that we can

154
00:10:50,650 --> 00:10:53,830
add that to the location where we want to put it.

155
00:10:54,220 --> 00:10:58,510
So this is the storage reference where we want to put our image and now let's put the image there.

156
00:10:59,620 --> 00:11:07,070
So we can go ahead and say as ref dot put file and which file we want to put there?

157
00:11:07,210 --> 00:11:13,280
Well, our M selected image file you're right now thinks this is a nullable.

158
00:11:13,300 --> 00:11:17,690
So what we do is we force it here because we check that it's not known.

159
00:11:17,840 --> 00:11:19,860
So we only do the code if it's not all right.

160
00:11:20,890 --> 00:11:27,810
And then when we put a file onto our storage, we can check if it was successful or not.

161
00:11:27,820 --> 00:11:29,830
So year on or add on.

162
00:11:30,180 --> 00:11:33,380
Listener Adam Complete listener at Unsuccess Listener.

163
00:11:33,410 --> 00:11:35,620
So all of those are available again.

164
00:11:35,840 --> 00:11:38,710
And I want to use the add on success listener.

165
00:11:39,820 --> 00:11:43,090
And there I'm going to get a snapshot.

166
00:11:44,550 --> 00:11:50,190
And as a result, I want to then use this test snapshot to log it for now.

167
00:11:53,390 --> 00:11:58,340
So what I'm going to do is I'm just going to take the snapshot, get the metadata from it, get the

168
00:11:58,340 --> 00:12:01,910
reference from it, get to download your URL from it and make a string from it.

169
00:12:02,510 --> 00:12:08,430
OK, so basically with this line here, I make a string out of where the file is stored.

170
00:12:08,780 --> 00:12:16,520
OK, and I'm just going to log that as an arrow into my log saying Firebase immaturely could of course

171
00:12:16,520 --> 00:12:18,210
use info here as well instead.

172
00:12:18,770 --> 00:12:24,500
So we're going to get an info saying Firebase Image URL and then the imagery where it is stored.

173
00:12:24,500 --> 00:12:28,370
And we can later on check this value out in our Lockette.

174
00:12:30,160 --> 00:12:35,570
All right, so what is happening here is that once we put the file, we get an unsuccess, listen.

175
00:12:35,570 --> 00:12:35,860
All right.

176
00:12:35,860 --> 00:12:42,180
And when once it was successful, we get a snapshot of that task and that task will give us to download

177
00:12:42,180 --> 00:12:42,670
Jurrell.

178
00:12:45,050 --> 00:12:51,680
And we need to download the URL in order to store it in the user data as well, so in order to store

179
00:12:51,680 --> 00:12:52,180
it here, right.

180
00:12:52,280 --> 00:12:57,950
If you want to store it here, we need to get the link, the link of the file in the storage.

181
00:12:58,550 --> 00:12:58,910
All right.

182
00:12:58,920 --> 00:13:01,100
So in the storage, it gets a unique link.

183
00:13:01,250 --> 00:13:03,560
And now we want to also storage in the database.

184
00:13:03,560 --> 00:13:04,790
So let's prepare that.

185
00:13:05,630 --> 00:13:12,020
Therefore, we take this task snapshot, we get its metadata and so forth.

186
00:13:12,020 --> 00:13:14,540
So we pretty much do the same thing as here.

187
00:13:17,080 --> 00:13:19,420
Except for this two string part.

188
00:13:20,340 --> 00:13:23,490
And we add another Unsuccess listener here.

189
00:13:25,020 --> 00:13:27,840
So here this time, we gather your eye.

190
00:13:30,290 --> 00:13:32,120
Which is the actual link.

191
00:13:32,840 --> 00:13:35,910
OK, now we can go ahead and store that, you or I, somewhere.

192
00:13:36,770 --> 00:13:40,490
So what we can do is we can have another log entry.

193
00:13:42,160 --> 00:13:49,300
Which is going to be the you are I as a string, so downloadable imagery, and that will now be to your

194
00:13:49,300 --> 00:13:50,050
eye to string.

195
00:13:51,130 --> 00:13:56,530
And now what we can do is we can go ahead and create a variable in which we store that value.

196
00:13:56,710 --> 00:13:59,250
So let's go out and create a new variable here.

197
00:13:59,620 --> 00:14:03,070
So we have to select that image file URAI, which is on the device.

198
00:14:03,070 --> 00:14:06,130
But now we need to have the downloadable urai.

199
00:14:06,700 --> 00:14:12,070
So private var and profile.

200
00:14:13,950 --> 00:14:21,240
Image, you're going to call it simple string, because that's going to be fine as well, an empty string

201
00:14:21,240 --> 00:14:21,620
for now.

202
00:14:22,050 --> 00:14:26,910
So the profile image, you, Earl, and now we can go ahead and set that.

203
00:14:27,240 --> 00:14:30,630
So in the upload user image function.

204
00:14:31,650 --> 00:14:34,230
Here we can go ahead and set it.

205
00:14:36,290 --> 00:14:46,010
So let's say profile image, your URL is going to be a you or I to string so that you write that we

206
00:14:46,010 --> 00:14:50,480
get as a string will be stored in a profile image URL.

207
00:14:52,780 --> 00:14:59,560
Now, at this point, what we want to do is we want to update the user profile data with this profile

208
00:14:59,560 --> 00:14:59,900
image.

209
00:15:00,040 --> 00:15:10,900
OK, so here to do update user profile data, so we will need to create a function which will update

210
00:15:10,900 --> 00:15:16,130
the user profile data, which includes the name and all of the changes that the user could have made.

211
00:15:16,280 --> 00:15:17,780
It could have changed to email.

212
00:15:18,130 --> 00:15:23,110
Well, not that, but the phone number, his name, and then whatever settings you want to add later

213
00:15:23,110 --> 00:15:23,650
on as well.

214
00:15:25,330 --> 00:15:31,230
OK, but of course, when something can go right, there's also something that can go wrong.

215
00:15:31,750 --> 00:15:33,220
So let's add.

216
00:15:34,180 --> 00:15:42,610
For this part, here for is putting file something into our database or into our storage and on failure

217
00:15:42,700 --> 00:15:43,280
listener.

218
00:15:43,660 --> 00:15:46,410
So what will happen if we get an error here?

219
00:15:46,930 --> 00:15:48,640
So then we will get an exception.

220
00:15:48,670 --> 00:15:49,080
Right.

221
00:15:49,420 --> 00:15:51,550
And what do we want to do with this exception?

222
00:15:51,580 --> 00:15:55,460
Well, what we're going to do is just write a little toast with the message.

223
00:15:55,480 --> 00:16:01,630
OK, so we're just going to take this exception and then display the exception message.

224
00:16:03,890 --> 00:16:08,810
And of course, what we should also do is hide our progress dialogue, because otherwise it will be

225
00:16:08,810 --> 00:16:12,410
constantly displaying if we had the error and would not disappear.

226
00:16:14,650 --> 00:16:19,150
All right, so at this point, let's just test it real quick, and in order to be able to test it,

227
00:16:19,150 --> 00:16:22,930
we need to give our update button a functionality.

228
00:16:22,930 --> 00:16:27,160
So let's go ahead and use the button update.

229
00:16:28,540 --> 00:16:30,670
Said on Click Listener.

230
00:16:32,360 --> 00:16:40,160
And here we're just going to say, if I'm selected, image URL is not empty, so it's not null, then

231
00:16:40,670 --> 00:16:44,720
run our update or upload user image.

232
00:16:45,680 --> 00:16:46,740
Of course, it's not done yet.

233
00:16:46,760 --> 00:16:48,390
We still need to do a lot more here.

234
00:16:48,710 --> 00:16:51,730
So what if the image you URL is empty?

235
00:16:51,740 --> 00:16:52,830
What do you want to do then?

236
00:16:53,180 --> 00:16:56,030
Well, then we just want to update the other information.

237
00:16:56,030 --> 00:16:59,770
For example, with the user, changed the name or changed the phone number.

238
00:16:59,780 --> 00:17:00,920
And we still want to update those.

239
00:17:00,920 --> 00:17:01,220
Right.

240
00:17:01,730 --> 00:17:02,110
All right.

241
00:17:02,690 --> 00:17:04,430
Now, now test tested with what we got.

242
00:17:05,500 --> 00:17:08,310
OK, so let's go over to our profile.

243
00:17:10,339 --> 00:17:13,220
Now, let's select the image of Homer.

244
00:17:15,520 --> 00:17:17,319
And let's see if we can upload anything.

245
00:17:19,109 --> 00:17:26,310
OK, so let's press update, please wait and of course, we don't have the functionality that will close

246
00:17:26,310 --> 00:17:29,700
this because currently if we look at our code.

247
00:17:31,200 --> 00:17:39,660
When we upload it, upload image URL, we start a progress dialogue and we only close it in the unfilial

248
00:17:39,660 --> 00:17:41,820
listener, so here it is, hard progress dialogue.

249
00:17:43,080 --> 00:17:47,500
Of course, we will later on do that in the update user profile data.

250
00:17:47,670 --> 00:17:52,260
So this high progress dialogue, but for now, we can do it in here as well.

251
00:17:52,800 --> 00:17:58,140
But now let's just seen the Lockette to check out if we get something.

252
00:17:58,140 --> 00:18:00,720
And what we should get is a downloadable.

253
00:18:01,930 --> 00:18:09,580
And Maduro, so let's search for downloadable and we can see downloadable images, URL as Firebase Dorridge,

254
00:18:09,580 --> 00:18:13,230
Google APIs, dot com slash V and so forth.

255
00:18:13,240 --> 00:18:18,670
So the name of our project here, app spot dot com slash user image and then this.

256
00:18:19,580 --> 00:18:22,070
Value and then tarping.

257
00:18:24,100 --> 00:18:30,400
So it's one five eight two five five search for these values here in our storage.

258
00:18:31,510 --> 00:18:36,260
And let's see if we got it there, we can see one five eight two five five.

259
00:18:36,760 --> 00:18:37,120
All right.

260
00:18:37,120 --> 00:18:41,640
So the images here and if we check out this image, we can see it's this Holmer image.

261
00:18:41,920 --> 00:18:43,870
It has roughly 75 kilobytes.

262
00:18:44,000 --> 00:18:46,240
But overall, you can see it works.

263
00:18:47,910 --> 00:18:53,400
So now you know how you can store something on your firebase storage, but of course what we want to

264
00:18:53,400 --> 00:18:57,710
do is also store it in our database here as well.

265
00:18:57,930 --> 00:19:04,830
So where we just update the image here, what the New York URL, and that's what we're going to look

266
00:19:04,830 --> 00:19:05,750
at in the next video.

267
00:19:05,790 --> 00:19:06,630
So I'll see you there.

