1
00:00:00,450 --> 00:00:05,220
Welcome back, and this year we are going to clean up our project a little bit, so we are going to

2
00:00:05,220 --> 00:00:11,670
create separate directories and folders which will be needed because at this point we are going to use

3
00:00:11,940 --> 00:00:16,190
models and databases and then we already have two activities.

4
00:00:16,200 --> 00:00:19,260
So it makes sense to have different directories for each of them.

5
00:00:19,590 --> 00:00:23,580
OK, so let's go ahead and create a new package here.

6
00:00:25,100 --> 00:00:29,630
And the first one will be activities, then I create another one.

7
00:00:31,300 --> 00:00:33,730
Which I'm going to call models.

8
00:00:35,460 --> 00:00:38,670
And then a third one, which will be.

9
00:00:39,620 --> 00:00:46,820
My database, so in this database directory, I'm going to put all the database, relevant classes that

10
00:00:46,820 --> 00:00:49,460
we have and the activities, of course, all of my activity.

11
00:00:49,490 --> 00:00:50,810
So let's drag them in there.

12
00:00:51,470 --> 00:00:52,400
Let's refactor.

13
00:00:54,070 --> 00:01:00,220
And what that does is it will create the right imports and it will adjust to imports accordingly, because

14
00:01:00,220 --> 00:01:06,270
now our activities, they are not inside of our EU tutorials, happy places anymore, but they are on

15
00:01:06,280 --> 00:01:06,910
a different level.

16
00:01:07,690 --> 00:01:08,050
All right.

17
00:01:08,440 --> 00:01:17,890
So the next thing that I want to create is a model because we are going to need a model for our happy

18
00:01:17,890 --> 00:01:18,310
places.

19
00:01:18,310 --> 00:01:18,590
Right?

20
00:01:18,940 --> 00:01:23,080
So I'm going to call this one happy place model.

21
00:01:23,710 --> 00:01:29,980
And I'm not calling it Happy Places model, because this is the model for every single individual happy

22
00:01:29,980 --> 00:01:30,430
place.

23
00:01:31,310 --> 00:01:36,830
All right, so let's go ahead and create that now, the happy place model will be a data class, so

24
00:01:36,830 --> 00:01:38,870
it will not be a normal class.

25
00:01:38,870 --> 00:01:45,080
It will just be a very simple data class in which I want to add the different variables that I want

26
00:01:45,080 --> 00:01:47,650
to have each time that I'm creating a happy place.

27
00:01:48,290 --> 00:01:50,960
So I want, for example, to have the ID.

28
00:01:51,920 --> 00:01:59,060
So an a unique identifier then the title, which is some type of string.

29
00:01:59,780 --> 00:02:05,390
Then I want to have an image which will be of type string.

30
00:02:05,390 --> 00:02:12,890
And by this the image path is meant then a description, because if we look at it so we can check out

31
00:02:12,890 --> 00:02:19,970
our application here we have the title description, date, location, the image and then also latitude

32
00:02:19,970 --> 00:02:20,590
and longitude.

33
00:02:20,810 --> 00:02:27,380
So we need to have the location in latitude and longitude as well, not just a string, which is the

34
00:02:27,380 --> 00:02:29,900
address of the location, but both of them.

35
00:02:30,410 --> 00:02:30,820
All right.

36
00:02:31,010 --> 00:02:31,580
So then.

37
00:02:32,660 --> 00:02:33,350
The image.

38
00:02:34,550 --> 00:02:44,660
The description, so foul description, which is also type strength, the date, which is also type

39
00:02:44,660 --> 00:02:47,000
string, the location.

40
00:02:49,670 --> 00:02:59,720
After a string and then the latitude, which is of type double and the longitude, so latitude and longitude

41
00:02:59,720 --> 00:03:08,180
are the two values with which you can specifically define a spot or find a spot on the earth.

42
00:03:08,480 --> 00:03:08,930
All right.

43
00:03:09,680 --> 00:03:13,010
Longitude will also be a double.

44
00:03:13,940 --> 00:03:14,360
All right.

45
00:03:14,370 --> 00:03:15,890
So this is our happy place model.

46
00:03:15,900 --> 00:03:19,660
So each time we create a new happy place, we want to have all of this data.

47
00:03:20,210 --> 00:03:24,050
And, of course, you might say, hey, we don't have location, we don't have latitude and we don't

48
00:03:24,050 --> 00:03:24,630
have longitude.

49
00:03:24,650 --> 00:03:27,320
Well, actually, location, we do have bits of string, right.

50
00:03:27,320 --> 00:03:29,320
That we get from the edge of TextField.

51
00:03:29,570 --> 00:03:32,460
But what we don't have is the longitude and latitude.

52
00:03:33,050 --> 00:03:35,150
Well, we will need to create those words.

53
00:03:37,190 --> 00:03:43,640
All right, the next thing I want is a new class here, and I'm going to create a database handler,

54
00:03:43,640 --> 00:03:47,300
so it will be a school wide open helper class.

55
00:03:47,450 --> 00:03:52,640
So I'm going to go ahead and create this class and I'm going to call this one database handler.

56
00:03:54,150 --> 00:04:00,730
All right, and it should inherit from CircuLite open helper, so you might recall how to do that.

57
00:04:01,260 --> 00:04:04,770
And by the way, this is something that I would ask you to try yourself.

58
00:04:04,780 --> 00:04:12,350
So try to create a database helper or handler which will take care of the whole database stuff.

59
00:04:12,630 --> 00:04:20,430
So we'll need to have a column for every single one of the values that we have for our happy places

60
00:04:20,430 --> 00:04:21,890
model or happy place model.

61
00:04:22,350 --> 00:04:27,570
So we need an ID column, we need to title column and image column, description column and so forth.

62
00:04:27,810 --> 00:04:29,250
So we'll need columns for all of that.

63
00:04:29,850 --> 00:04:33,540
The table name should be a happy place.

64
00:04:33,540 --> 00:04:41,610
This table and the database name should be Happy Places database, OK, and everything else I think

65
00:04:41,610 --> 00:04:43,110
you can already set up.

66
00:04:43,110 --> 00:04:47,450
So try to do that to try to create the table.

67
00:04:47,460 --> 00:04:53,840
So in the uncreate method, create a table and then add a function which will add a happy place.

68
00:04:53,850 --> 00:05:01,260
So you will need to put all of the different values to the content values variable before then inserting

69
00:05:01,260 --> 00:05:03,960
it to the database and closing the database.

70
00:05:03,990 --> 00:05:07,490
OK, so please go ahead and try to do that in the database handler.

71
00:05:07,890 --> 00:05:14,660
And if you don't know how to do that, then check out the demo on how to use databases.

72
00:05:14,670 --> 00:05:19,040
Again, I know this is a tough one, but this one will be super helpful for you.

73
00:05:19,320 --> 00:05:22,620
You will learn quite a bit while struggling, so to speak.

74
00:05:22,650 --> 00:05:23,070
All right.

75
00:05:23,310 --> 00:05:24,180
So good luck with that.

76
00:05:26,640 --> 00:05:30,640
Alrighty, I hope you post the video and tried it, so I'm going to go over it.

77
00:05:31,260 --> 00:05:38,220
So this is the database handler and as you might recall, we need to use Scollard Open Helper as the

78
00:05:38,220 --> 00:05:43,950
class that we inherit from then that we need to pass in the context the database name, the factory,

79
00:05:43,950 --> 00:05:45,960
which is just null and a database version.

80
00:05:46,650 --> 00:05:47,070
All right.

81
00:05:47,070 --> 00:05:49,880
And we need those variables as companion objects.

82
00:05:50,280 --> 00:05:54,690
So I'm just going to use those variables here, as you can see, database version, database name.

83
00:05:54,900 --> 00:05:57,780
And then we also need to have a name for the table.

84
00:05:58,110 --> 00:06:03,570
And I'm going to call this one happy place, this table, and then all of the column names that we have.

85
00:06:03,570 --> 00:06:08,520
And these column names are basically the ones that we have from our happy place model.

86
00:06:09,000 --> 00:06:14,850
OK, so each time we create an object, we want to store that object inside of our database because

87
00:06:14,850 --> 00:06:16,620
we cannot store objects as such.

88
00:06:16,830 --> 00:06:21,960
We need to make sure that we have the variables of those objects in the right format, which we do here.

89
00:06:21,960 --> 00:06:26,370
So we create all of those column names, which are all constants.

90
00:06:27,390 --> 00:06:31,280
And then in the uncreate, that's where we create the happy place table.

91
00:06:31,290 --> 00:06:32,730
So we create a new table.

92
00:06:33,720 --> 00:06:39,630
Based on the name that we have in Table Happy Place, which is our happy place, this table, and then

93
00:06:40,260 --> 00:06:47,220
we create the ID, which is our primary key of type in the title, which is just a text, the image,

94
00:06:47,220 --> 00:06:48,180
which is also a text.

95
00:06:48,180 --> 00:06:50,790
So strings can basically always be stored as text.

96
00:06:51,390 --> 00:06:54,360
And you can see a key description as a text, key date as a text.

97
00:06:54,370 --> 00:06:59,750
So basically what I did here is I just use texts here even for the latitude and longitude.

98
00:06:59,760 --> 00:07:01,550
I also use text here.

99
00:07:01,800 --> 00:07:09,250
You might have used real instead, but we're just going to use text here, OK?

100
00:07:09,510 --> 00:07:14,420
And then we, of course, need to execute that signal light command, which is our create happy place

101
00:07:14,430 --> 00:07:15,290
table command.

102
00:07:15,660 --> 00:07:18,120
So it's this one containing all of that.

103
00:07:18,870 --> 00:07:25,890
So at this point when this database handler is so the uncreate method is called, that's the point where

104
00:07:26,190 --> 00:07:33,390
our table will be created and then in an upgrade, well, we just say, OK, drop table if they exist

105
00:07:33,510 --> 00:07:34,200
and then.

106
00:07:36,130 --> 00:07:42,610
Called the uncreate method, so nothing new here and then that happy place function, which will return

107
00:07:42,610 --> 00:07:47,920
a long which basically will be the value if it succeeded or if it didn't succeed.

108
00:07:48,400 --> 00:07:48,700
All right.

109
00:07:48,700 --> 00:07:53,650
So what we're doing is we need a writable database first because we want to write something into the

110
00:07:53,650 --> 00:07:54,270
database.

111
00:07:54,640 --> 00:07:59,130
Then we have the content values to which we put of all our values here.

112
00:07:59,170 --> 00:08:03,850
So we put the key title, computer key image in and so forth.

113
00:08:04,210 --> 00:08:06,490
And here you get where we get them from.

114
00:08:06,500 --> 00:08:10,940
So we get them from the happy place that is passed to us when we call this function.

115
00:08:11,290 --> 00:08:16,120
So we want to have a happy place object that is passed to us, which contains all of this information

116
00:08:16,540 --> 00:08:17,620
because it's a data class.

117
00:08:17,620 --> 00:08:22,150
So we can create objects of it, of course, and passing all of those values in.

118
00:08:22,360 --> 00:08:28,000
And then we can use all of those values to store them or for now to put them into the content values

119
00:08:28,270 --> 00:08:29,090
variable here.

120
00:08:29,650 --> 00:08:30,160
This one.

121
00:08:31,110 --> 00:08:38,010
To then go ahead and insert that to the database, so we insert that to the table, which we call a

122
00:08:38,010 --> 00:08:43,530
happy place, and of course, it has a different name, it called this called here Happy Place Table.

123
00:08:43,679 --> 00:08:45,420
So that's a very specific name.

124
00:08:45,780 --> 00:08:48,250
But the variable, of course, called Table Happy Place.

125
00:08:49,050 --> 00:08:53,820
Then we pass and the column which has just said to null and the content values.

126
00:08:54,420 --> 00:09:01,950
So that's the spot where we insert it to the database and this returns a result which is of type lunk.

127
00:09:02,520 --> 00:09:06,990
So you can see here insert returns a long while.

128
00:09:06,990 --> 00:09:14,520
This is a Java method here, but basically the same concepts apply and then we close the database and

129
00:09:14,520 --> 00:09:15,770
we return the result.

130
00:09:15,810 --> 00:09:18,390
So whatever the long result was.

131
00:09:19,970 --> 00:09:27,230
All right, and now we can, of course, go ahead and save our data, because if we look at it here.

132
00:09:28,340 --> 00:09:32,930
All of that stuff should be safe once we click on that button, so we need to add an on click listener

133
00:09:32,930 --> 00:09:39,250
for that button and we need to run a method call, so to speak, when we click on that button.

134
00:09:39,920 --> 00:09:42,920
So let's go to our activity where we handle that.

135
00:09:42,920 --> 00:09:45,040
And that's our ad happy place activity.

136
00:09:45,380 --> 00:09:50,480
And then in the uncreate method, that's where we added all of the.

137
00:09:52,280 --> 00:09:59,600
On classlessness, so here I need a button on this cause, safe thought set on Click Listener.

138
00:10:00,560 --> 00:10:07,400
And I'm going to pass on this, because this class at a happy place activity class will be our listener.

139
00:10:08,460 --> 00:10:14,760
All right, and then, of course, we also need to do the same thing as we did with the other IDs,

140
00:10:14,760 --> 00:10:18,750
here are dot, I.D. dot, and this time it's the.

141
00:10:19,720 --> 00:10:29,860
But unsafe, so once we click on button safe, what do we want to do well, and here we want to store

142
00:10:30,520 --> 00:10:37,060
or safe the data model to the database.

143
00:10:38,170 --> 00:10:40,320
OK, that's what we're going to do later on.

144
00:10:40,780 --> 00:10:45,030
But for now, we need to add something which we haven't added yet.

145
00:10:45,040 --> 00:10:52,330
And these are variables that are important for our project to run because we need to pass them right.

146
00:10:52,360 --> 00:10:57,720
We need to, for example, have an image which is going to be the URAI.

147
00:10:58,390 --> 00:11:03,670
So we are not really passing the image itself, but we're just wanting the URAI.

148
00:11:03,680 --> 00:11:08,470
So the location of the image, because it doesn't make sense to recreate the image and past the image

149
00:11:08,470 --> 00:11:12,820
all the time, but it's make us a lot more sense to just passing the reference.

150
00:11:13,120 --> 00:11:22,660
So I'm just going to create a new variable for this private variable called Save Image to internal storage.

151
00:11:23,380 --> 00:11:28,030
And it's going to be of type URAI nullable.

152
00:11:30,250 --> 00:11:31,720
So at the beginning, it's going to be not.

153
00:11:34,090 --> 00:11:36,880
And then later on, we can assign a value there.

154
00:11:38,290 --> 00:11:42,920
All right, then we, of course, need to have the latitude and longitude, so I'm just going to create

155
00:11:42,920 --> 00:11:57,280
them private for and let me tud double, which will be zero zero and private four and longitude, which

156
00:11:57,280 --> 00:12:00,470
will be also a double and 020.

157
00:12:00,490 --> 00:12:05,590
So at this point, each time that we use the default values, we're going to be in the middle of the

158
00:12:05,590 --> 00:12:11,110
Atlantic, somewhere west of Africa, not too far away from.

159
00:12:12,060 --> 00:12:14,280
Africa, I'd say, OK.

160
00:12:15,460 --> 00:12:23,650
So for now, let's just set a value for the safe image to eternal storage and the spots where it would

161
00:12:23,650 --> 00:12:27,400
make sense to do so are going to be these ones here.

162
00:12:27,700 --> 00:12:35,560
So here we had this variable called safe image to internal storage, but here it was a immutable variable

163
00:12:35,710 --> 00:12:40,210
and it was one that is only visible in the scope of this on activity result.

164
00:12:40,600 --> 00:12:46,180
But I, of course, don't only wanted to use in this try and catch block, but I want to store it permanently.

165
00:12:46,660 --> 00:12:50,680
So I'm going to use while I'm just going to get rid of the valve.

166
00:12:50,680 --> 00:12:51,010
Right.

167
00:12:51,100 --> 00:12:53,280
So I use the exact same name.

168
00:12:53,290 --> 00:12:58,090
So now I'm just going to store it at the moment when we get an image from the gallery.

169
00:12:58,710 --> 00:13:04,330
And of course, the same thing when we get an image from taking a picture with a camera.

170
00:13:06,770 --> 00:13:12,530
At this moment, of course, we are still not storing anything because we are never calling this database

171
00:13:12,530 --> 00:13:16,520
handler method and happy place, as you can see, still grayed out.

172
00:13:16,800 --> 00:13:19,130
But that's what we're going to do in the next videos.

173
00:13:19,320 --> 00:13:22,730
So for now, I think it's enough for this video.

174
00:13:22,730 --> 00:13:27,710
It's long enough in the next hour, we're going to add a little more spice dudes and we're going to

175
00:13:27,710 --> 00:13:30,770
be able to store the data accordingly.

176
00:13:31,160 --> 00:13:32,990
OK, so see you in the next one.

