1
00:00:08,040 --> 00:00:11,220
Hey everybody what's going on this is Caleb with deps slopes dot com.

2
00:00:11,230 --> 00:00:16,810
And in this video we're going to go ahead and build the model layer for our application that's going

3
00:00:16,810 --> 00:00:23,440
to allow us to create an instance of a hub repository will be able to download the data inject it into

4
00:00:23,440 --> 00:00:26,920
our instance and then use it to display that data and our table view.

5
00:00:27,250 --> 00:00:31,450
But first we're going to actually start by taking a look at the get hub developer guide.

6
00:00:31,480 --> 00:00:36,850
So I'm actually going to pull it up in Safari and we're going to be looking at the search functionalities

7
00:00:36,940 --> 00:00:38,710
of the Get hub API.

8
00:00:38,710 --> 00:00:42,490
And of course if you want to know more you can come here and you can read through all of it it's very

9
00:00:42,520 --> 00:00:45,210
robust very in-depth.

10
00:00:45,370 --> 00:00:52,660
And basically you can read all of the specifics but what we need to actually look at is the ability

11
00:00:52,660 --> 00:00:56,200
to search repositories and that's exactly what we want to do.

12
00:00:56,350 --> 00:01:01,880
So we can do that by using slash search slash repositories that's hard to say.

13
00:01:02,080 --> 00:01:03,950
But anyway that is what we can do.

14
00:01:03,970 --> 00:01:11,340
Now there are three parameters to sort and order and that allows us to one give a query a search query.

15
00:01:11,350 --> 00:01:17,860
What we want to look for we can sort it by the number of stars and number of forks or when it was last

16
00:01:17,950 --> 00:01:24,970
updated of course by default they're sorted by best match as Herb sees fit I guess.

17
00:01:24,970 --> 00:01:26,810
Then of course there is order.

18
00:01:26,970 --> 00:01:27,470
OK.

19
00:01:27,610 --> 00:01:33,620
Now you can sort them ascending or descending meaning from least the greatest or greatest at least.

20
00:01:33,700 --> 00:01:40,900
Now we can also in any of these additional parameters like create it meaning the date of creation the

21
00:01:41,110 --> 00:01:48,370
forked repositories or we can basically set it to be false or true so we can view for depositories or

22
00:01:48,370 --> 00:01:56,200
not we can filter them based on the number of fork's search by programming language by repo or user

23
00:01:56,200 --> 00:01:59,350
or the size number of stars et cetera et cetera.

24
00:01:59,350 --> 00:02:01,460
So there's a ton of different things we can do.

25
00:02:01,750 --> 00:02:04,260
And there's a great example right here.

26
00:02:04,300 --> 00:02:09,270
OK this is an example to search for Tetris repositories that are popular.

27
00:02:09,440 --> 00:02:09,690
OK.

28
00:02:09,700 --> 00:02:15,990
So it's going to be sorted by stars meaning the most popular written in the assembly programming language.

29
00:02:15,990 --> 00:02:21,280
So this is an example of what it might look like and let me zoom in here so we can see it really well.

30
00:02:21,280 --> 00:02:21,960
There we go.

31
00:02:22,210 --> 00:02:31,000
So we have API get hub dot com slash search slash repositories question mark Q equals and there is our

32
00:02:31,000 --> 00:02:32,870
search query Tetris.

33
00:02:33,040 --> 00:02:36,520
Now on top of the search query we can do.

34
00:02:36,520 --> 00:02:44,610
Plus language or plus fork's plus size we can do any of those and we've done plus language assembly

35
00:02:44,620 --> 00:02:48,760
because remember we're looking for Tetris that has been built in assembly.

36
00:02:48,760 --> 00:02:53,880
Then we can call and sort K and we can sort it by the number of stars.

37
00:02:53,950 --> 00:02:55,750
The order is descending.

38
00:02:55,750 --> 00:03:00,000
So it's going to sort them by the highest number of stars down to the least.

39
00:03:00,150 --> 00:03:01,610
OK that's pretty cool.

40
00:03:01,630 --> 00:03:03,180
Let's go ahead and let's close it out.

41
00:03:03,190 --> 00:03:07,460
Let's see what this query actually looks like when we call it.

42
00:03:07,720 --> 00:03:09,880
We get a lot of Jason data.

43
00:03:09,880 --> 00:03:16,120
And if you've worked with Jason before you know that this is just a way that applies as far as restful

44
00:03:16,120 --> 00:03:17,010
API go.

45
00:03:17,200 --> 00:03:22,990
They tend to return data in a Jason format which is really easy for us to work with actually.

46
00:03:22,990 --> 00:03:28,570
So I'm going to go ahead and pass this in to what's called a Jason beautifier which you can find easily

47
00:03:28,570 --> 00:03:30,330
by searching in Google.

48
00:03:30,520 --> 00:03:32,640
And this is the same Jason code.

49
00:03:32,650 --> 00:03:34,270
But it has been beautified.

50
00:03:34,270 --> 00:03:35,440
Let me see if I can zoom in.

51
00:03:35,440 --> 00:03:36,210
There we go.

52
00:03:36,520 --> 00:03:43,370
And basically this allows us to kind of collapse and open the different pieces here and dent it nicely

53
00:03:43,410 --> 00:03:45,470
bowled the keys and shows the values.

54
00:03:45,580 --> 00:03:46,690
It's really cool.

55
00:03:46,690 --> 00:03:50,490
So as you can see this is the first result called Tetris.

56
00:03:50,500 --> 00:03:51,110
OK.

57
00:03:51,250 --> 00:03:53,150
Now the owner is Daniel Eve.

58
00:03:53,170 --> 00:03:54,270
We can see his username.

59
00:03:54,400 --> 00:03:56,150
We can see his User ID.

60
00:03:56,200 --> 00:03:58,510
We can get a link to his avatar.

61
00:03:58,510 --> 00:03:59,350
You are l.

62
00:03:59,350 --> 00:04:00,380
That's pretty cool.

63
00:04:00,760 --> 00:04:02,850
So let's go ahead and close that down.

64
00:04:02,860 --> 00:04:05,170
We can see this is not a private repository.

65
00:04:05,280 --> 00:04:09,330
The U R L to the directory there which is pretty sweet.

66
00:04:09,370 --> 00:04:13,460
We can see your elf for the Tetris from his repo's.

67
00:04:13,540 --> 00:04:14,710
Part of his profile.

68
00:04:14,860 --> 00:04:16,670
You can see there's a ton of stuff here.

69
00:04:16,750 --> 00:04:20,640
Now as I scroll down you'll notice there's not going to be a number of contributors.

70
00:04:20,650 --> 00:04:25,870
If you think back that is one of the things that we use there is a fork's count that shows how many

71
00:04:25,870 --> 00:04:27,610
times it has been forked.

72
00:04:27,790 --> 00:04:33,670
But you'll notice there is not a number of contributors and that's because we need to go into contributor's

73
00:04:33,670 --> 00:04:42,010
you RL And we need to create another Jaison call and we need to download the Jaison for the contributor's

74
00:04:42,070 --> 00:04:45,380
pull out the count of contributors from there and we'll do that later on.

75
00:04:45,400 --> 00:04:48,700
But as you can see all of our data here is coming in.

76
00:04:48,790 --> 00:04:55,290
So we're going to go ahead and use this as a way of framing our mind when creating our model layer.

77
00:04:55,300 --> 00:05:00,000
So I'm going to go ahead and close out of this and pull open my X code project.

78
00:05:00,010 --> 00:05:04,380
I would recommend that you do the same because you're probably watching this wanting to learn anyway.

79
00:05:04,580 --> 00:05:13,490
So let's continue go to your project go ahead and right click on the model folder and select new file.

80
00:05:13,490 --> 00:05:18,270
Click on swift file DoubleClick and we're going to name this repo.

81
00:05:18,500 --> 00:05:25,570
Because that is what our instance will be called repo of course is short for repository that we go.

82
00:05:25,690 --> 00:05:31,060
So go ahead and we can just collapse this for now because we're just going to be writing some code and

83
00:05:31,270 --> 00:05:34,210
we're going to think our repo needs a few things.

84
00:05:34,390 --> 00:05:37,190
We're going to need a name obviously.

85
00:05:37,210 --> 00:05:41,740
So let's go ahead and let's actually create these properties now first let's create the class.

86
00:05:41,770 --> 00:05:43,920
Class repo.

87
00:05:44,350 --> 00:05:50,860
And we're going to go ahead and create var name which of course is going to come in as a string.

88
00:05:51,280 --> 00:05:52,270
OK.

89
00:05:52,270 --> 00:05:55,200
Let's use our table view here as an example.

90
00:05:55,200 --> 00:05:58,360
I mean quit out and open up the finished version.

91
00:05:58,360 --> 00:06:00,490
So let's see we have a name.

92
00:06:00,490 --> 00:06:01,810
We have a description.

93
00:06:01,960 --> 00:06:06,670
So let's create our description of type string.

94
00:06:06,670 --> 00:06:07,500
What do we have next.

95
00:06:07,540 --> 00:06:13,510
We have a number of what is this number of forks.

96
00:06:13,680 --> 00:06:16,200
OK so let's say number.

97
00:06:16,230 --> 00:06:17,650
Oops var.

98
00:06:17,730 --> 00:06:19,140
Come on var.

99
00:06:19,450 --> 00:06:21,880
Number of forks.

100
00:06:21,880 --> 00:06:22,120
OK.

101
00:06:22,130 --> 00:06:24,090
And that is going to be of type integer.

102
00:06:24,230 --> 00:06:28,160
OK what's next we have the language.

103
00:06:28,180 --> 00:06:34,310
So let's just say our language of type string.

104
00:06:34,300 --> 00:06:45,490
Var contributor's So we'll just say number of contribute towards of type int and we have what else.

105
00:06:45,490 --> 00:06:48,670
For the read me we need to know the U R L for the certain repository.

106
00:06:48,670 --> 00:06:54,360
So let's go ahead and just say var Ribault u r l of type string.

107
00:06:54,460 --> 00:06:57,760
Instead of casting it as a u r l it will be easier to use string.

108
00:06:58,150 --> 00:06:59,540
And let's think what else.

109
00:06:59,800 --> 00:07:07,140
This has everything a name description fork's language contributor's you r l o.

110
00:07:07,180 --> 00:07:11,200
Each repo needs an image will download an image and save it in the repository as well and I'm going

111
00:07:11,200 --> 00:07:17,490
to put it up here at the top var image of type II image but you'll notice that class is not coming in.

112
00:07:17,560 --> 00:07:23,200
So we need to actually import it to access that and it's OK because you I can include foundation all

113
00:07:23,200 --> 00:07:24,760
the same stuff is in there.

114
00:07:25,060 --> 00:07:27,070
But we're getting an error now.

115
00:07:27,160 --> 00:07:29,800
Class repo has no initializers.

116
00:07:30,010 --> 00:07:31,260
Let's create one.

117
00:07:31,270 --> 00:07:32,890
So let's go ahead and below.

118
00:07:32,890 --> 00:07:34,400
Go ahead and type in it.

119
00:07:34,690 --> 00:07:40,350
And we need to pass in every one of these variables and then set the variables using our initializers.

120
00:07:40,360 --> 00:07:48,410
So go ahead and type image of type II image description of type string.

121
00:07:48,460 --> 00:07:56,440
You know what I should do name before that name of type String comma description of type String comma

122
00:07:56,470 --> 00:08:10,600
language comma number of forks of type int language of type string number of contributors of type ints

123
00:08:10,960 --> 00:08:14,350
and repo you are l of type strings.

124
00:08:14,590 --> 00:08:16,410
All right so there is all of our properties.

125
00:08:16,420 --> 00:08:22,800
And now we can go ahead and set self image meaning the image from this class not from the initializer.

126
00:08:22,870 --> 00:08:28,600
And we're going to set it to be whatever image we pass in we'll do the same thing for name is equal

127
00:08:28,600 --> 00:08:36,160
to name self-caused description is equal to description self-doubt number of forks is equal to number

128
00:08:36,160 --> 00:08:37,050
of forks.

129
00:08:37,060 --> 00:08:45,130
Self-taught language is equal to language self-taught number of contributors is equal to number of contributors

130
00:08:45,160 --> 00:08:46,430
and self.

131
00:08:46,480 --> 00:08:49,060
Repo your L is equal to.

132
00:08:49,360 --> 00:08:50,470
That's not real euro.

133
00:08:50,680 --> 00:08:51,170
There we go.

134
00:08:51,280 --> 00:08:55,080
OK so now we have properly initialized all of this.

135
00:08:55,150 --> 00:08:57,560
But there is a big problem.

136
00:08:57,610 --> 00:09:00,640
All of these variables are publicly accessible.

137
00:09:00,700 --> 00:09:03,640
We can access them we can modify them.

138
00:09:03,670 --> 00:09:05,740
We can change them from anywhere.

139
00:09:05,740 --> 00:09:09,840
So we need to basically think about how we can be doing this.

140
00:09:09,880 --> 00:09:15,880
And in the past we've used data encapsulation where we've typed you know private var image and then

141
00:09:15,880 --> 00:09:23,440
down here we've said Whoops sorry with an underscore and then we'll save our image of type II image

142
00:09:23,660 --> 00:09:25,730
return image.

143
00:09:25,740 --> 00:09:31,100
Hey that's how we have basically set up private variables and we have a public accessor.

144
00:09:31,360 --> 00:09:32,850
And that is one way to do it.

145
00:09:32,860 --> 00:09:35,600
But there's actually a way easier way to do it.

146
00:09:35,680 --> 00:09:37,370
That requires a lot less code.

147
00:09:37,540 --> 00:09:45,190
And what we can do is we can set each variable itself to be public meaning we can access it from anywhere

148
00:09:45,670 --> 00:09:52,710
but we can set the setter to be private so that it can only be set inside this class repo.

149
00:09:52,870 --> 00:09:55,420
So this repo class.

150
00:09:55,420 --> 00:10:01,530
So it's going to be a public variable with a private sector.

151
00:10:01,870 --> 00:10:02,900
Whoops there we go.

152
00:10:03,100 --> 00:10:09,940
And by doing this we create a variable that is public but we allow it to only be set inside the class

153
00:10:09,940 --> 00:10:11,110
that it belongs to.

154
00:10:11,110 --> 00:10:18,220
So to save time I'm going to copy this and paste it before each variable but we are basically saving

155
00:10:18,220 --> 00:10:19,600
ourselves a ton of code.

156
00:10:19,600 --> 00:10:21,860
This is a really cool thing that Swift can do.

157
00:10:21,910 --> 00:10:27,280
We can set private variables are get a variable privately but access it publicly.

158
00:10:27,450 --> 00:10:31,500
So if we build and run you'll notice there are no problems no errors.

159
00:10:31,500 --> 00:10:34,730
We have done a great job now just to see how it works.

160
00:10:34,740 --> 00:10:39,450
Let's go ahead and let's try to create an instance of this just below that we're not actually going

161
00:10:39,450 --> 00:10:42,530
to use this but I just want to show you how you're going to call it so.

162
00:10:42,690 --> 00:10:45,230
Var Ribault equals repo.

163
00:10:45,330 --> 00:10:49,680
And if we use the parentheses there we can go ahead and access all these properties.

164
00:10:49,830 --> 00:10:55,800
Image of type image name description number of fork's language contributors and repo you are l.

165
00:10:55,860 --> 00:11:00,780
So we have successfully created our model layer for our repository.

166
00:11:00,780 --> 00:11:06,420
All this information we're going to get from the get hub API which is really cool and publicly available

167
00:11:06,420 --> 00:11:11,040
which is amazing and we're going to go ahead and move on to the next video where we're going to use

168
00:11:11,040 --> 00:11:17,760
this repo class to basically connect dummy data to our table view and start displaying cells with dummy

169
00:11:17,760 --> 00:11:18,090
data.

170
00:11:18,090 --> 00:11:18,740
Pretty cool.

171
00:11:18,780 --> 00:11:22,190
So let's go ahead and head over to the next video and let's start building that out.
