1 00:00:00,840 --> 00:00:01,680 Welcome back. 2 00:00:01,859 --> 00:00:07,410 And the last video we created our API and the news service and all of those classes and so forth. 3 00:00:07,440 --> 00:00:12,150 Now we need to take care of the actual news managers. 4 00:00:12,150 --> 00:00:17,640 So the thing is, it's quite a complex process that we have to take care of, right? 5 00:00:17,640 --> 00:00:22,920 We need to create the classes first, like top news and top news article and so forth. 6 00:00:23,130 --> 00:00:29,430 Then we need to create the retro fit interface with the different requests, for example, a get, request, 7 00:00:29,430 --> 00:00:34,140 post, request, put, request and so forth, which is our interface new service. 8 00:00:34,260 --> 00:00:40,170 Then we need to set up the API object with the URL and basically where we want to send all of this and 9 00:00:40,170 --> 00:00:43,680 then we can use all of that in another. 10 00:00:44,850 --> 00:00:50,700 File, which will be or another class, which will be called news manager. 11 00:00:51,120 --> 00:00:57,270 OK, so this class will take care of putting everything together and here we will get the response, 12 00:00:57,270 --> 00:00:58,020 so to speak. 13 00:00:59,900 --> 00:01:07,040 So we need to separate the logic from the UI, and we prepared all of the logic now, now we can take 14 00:01:07,040 --> 00:01:07,970 care of the UI. 15 00:01:09,530 --> 00:01:15,770 So first of all, the private variable, which I'm going to call news response so underscore and use 16 00:01:15,770 --> 00:01:20,030 response and underscore just means that this should only be usable from this class itself. 17 00:01:20,450 --> 00:01:23,270 And this will be a mutable state of. 18 00:01:26,300 --> 00:01:31,790 Like so state of our top news response. 19 00:01:33,690 --> 00:01:35,520 OK, so top news response. 20 00:01:35,910 --> 00:01:41,080 You might recall it's this class, so that's the top level class which contains all of the data. 21 00:01:41,700 --> 00:01:45,510 So there's going to be our top news response, but we need to create an object of it. 22 00:01:45,510 --> 00:01:47,610 So we need to add the brackets here. 23 00:01:48,150 --> 00:01:54,660 And then I'm going to get my news response that I can then access from other classes as well and set 24 00:01:54,660 --> 00:01:58,950 from there, which will be a state of top news response. 25 00:01:59,730 --> 00:02:04,110 And whenever you do that, first of all, you need to import state here. 26 00:02:05,400 --> 00:02:09,000 So let's import the state from composed runtime. 27 00:02:10,940 --> 00:02:15,930 And then you need to use the composable where this is going to go. 28 00:02:15,950 --> 00:02:23,780 So at composable get and here we need to use the remember key word again because we are going to remember, 29 00:02:23,780 --> 00:02:24,870 don't use response. 30 00:02:24,890 --> 00:02:27,230 And let's import remember as well. 31 00:02:30,550 --> 00:02:35,380 So this basically just means, hey, we're getting the news report or we have this news response, if 32 00:02:35,380 --> 00:02:40,150 we want to make it available to other classes, we need to make it a state and it will just update our 33 00:02:40,150 --> 00:02:43,190 user interface based on the data. 34 00:02:43,210 --> 00:02:49,660 So that's really the cool thing about all of the state stuff where the state allows us not to have to 35 00:02:49,660 --> 00:02:53,590 let the user interface know manually that something has changed or the data has changed. 36 00:02:53,830 --> 00:02:59,440 But the data itself, when it changes, will update the user interface, or it will let the user interface 37 00:02:59,440 --> 00:03:00,670 know that it has to update. 38 00:03:01,750 --> 00:03:05,890 So now that we have the news response. 39 00:03:06,340 --> 00:03:08,740 Let's go ahead and get the articles. 40 00:03:08,920 --> 00:03:15,820 So I'm going to create a function that will get my get me the articles articles like so. 41 00:03:16,810 --> 00:03:20,290 So here we will need to use our service that we have set up. 42 00:03:20,290 --> 00:03:26,410 So service, which will be API Dot Retrofit Service API, is this class that we created. 43 00:03:26,740 --> 00:03:32,410 So now we're using this retrofit service, which is our new service, which we'll just do this get method. 44 00:03:33,070 --> 00:03:37,060 And here we can now get top articles. 45 00:03:38,270 --> 00:03:42,350 And it's calling this method the setup article's method. 46 00:03:42,830 --> 00:03:46,940 So now we need to pass in the country as well as the API key. 47 00:03:48,170 --> 00:03:49,700 All right, so let's do that. 48 00:03:50,420 --> 00:03:53,480 Let's get the country, which will be, for example, the US. 49 00:03:53,690 --> 00:04:02,270 And let's get the key and therefore you need to go over to your registration and take this API key. 50 00:04:02,630 --> 00:04:09,200 Now you could, of course, obviously also store it somewhere in a variable that you use in the singleton. 51 00:04:09,200 --> 00:04:11,240 So it's going to be accessible from everywhere. 52 00:04:11,720 --> 00:04:13,370 That would be fine as well. 53 00:04:13,400 --> 00:04:24,290 So here we could actually create a private vol concert or concert vol, which would be the API key. 54 00:04:25,710 --> 00:04:30,630 And let's call this one API key API key. 55 00:04:32,360 --> 00:04:33,800 This should also be constable. 56 00:04:35,710 --> 00:04:38,950 OK, so now let's use this API key here. 57 00:04:39,610 --> 00:04:43,030 So this is inside of API. 58 00:04:45,860 --> 00:04:48,890 API that API. 59 00:04:49,790 --> 00:04:51,230 On the score key. 60 00:04:52,970 --> 00:04:55,850 And I see that I shouldn't make it private and it should make it public. 61 00:04:57,300 --> 00:04:59,400 OK, now I can't use the API key. 62 00:05:00,000 --> 00:05:00,360 All right. 63 00:05:00,480 --> 00:05:08,490 So this will give me the service and now I have to and queue to service so and queue, which will asynchronously 64 00:05:08,490 --> 00:05:12,960 sent the request and notify callback of its response or if an error occurred. 65 00:05:13,380 --> 00:05:18,630 So in queue, which just means we're not going to do it instantly. 66 00:05:18,630 --> 00:05:24,150 But as soon as our application is ready and it's going to do it asynchronously and it's just going to 67 00:05:24,150 --> 00:05:28,110 give us the callback or the the response, so to speak, once it's ready. 68 00:05:28,530 --> 00:05:35,400 So when you're passing the callback, which we can do like, so we're just creating a new callback of 69 00:05:35,400 --> 00:05:37,080 type top news response. 70 00:05:37,090 --> 00:05:38,670 So it will give us this callback. 71 00:05:38,670 --> 00:05:41,460 We'll return a top news response. 72 00:05:41,880 --> 00:05:46,650 And then whenever you have a callback, you need to have the two different response types. 73 00:05:47,040 --> 00:05:57,540 So you need to have the override fun on response, which will require a call of top news response in 74 00:05:57,540 --> 00:05:58,170 our case. 75 00:05:58,530 --> 00:05:59,880 So we need to import call. 76 00:06:00,810 --> 00:06:04,500 And that retrofit version here we see fit. 77 00:06:04,530 --> 00:06:06,390 We used to write colbeck as well. 78 00:06:07,830 --> 00:06:10,530 So here, callback and used the wrong callback. 79 00:06:11,340 --> 00:06:14,970 That's the problem, so let's import the retrofit callback. 80 00:06:15,480 --> 00:06:20,280 And actually, once that is done, let me get rid of this and hover over it, and it will tell me to 81 00:06:20,280 --> 00:06:21,120 implement members. 82 00:06:21,120 --> 00:06:22,500 So this will make it a lot easier. 83 00:06:22,500 --> 00:06:25,260 I will not have to manually type all of that code in. 84 00:06:25,620 --> 00:06:30,630 There are two different callback responses that you will get, so you get the response and the on failure. 85 00:06:30,930 --> 00:06:37,980 So we're just going to say, OK, if we get a positive response here, we can use the data and do something 86 00:06:37,980 --> 00:06:38,280 with it. 87 00:06:38,550 --> 00:06:43,320 And if we get a failure, then we can just print it to the screen, saying, Hey, there was an error 88 00:06:43,320 --> 00:06:44,220 or something like that. 89 00:06:44,790 --> 00:06:47,430 So what I'm going to do in the error case is the first thing. 90 00:06:47,430 --> 00:06:56,580 I'm just going to log an entry to my log cat and I'm just going to see error and I'm going to get this 91 00:06:56,580 --> 00:06:59,820 throw bill, which will give me the actual error. 92 00:07:00,160 --> 00:07:02,310 OK, so t the prince deck tricks. 93 00:07:03,270 --> 00:07:05,370 So now what do we want to do with the response? 94 00:07:05,640 --> 00:07:08,100 If it worked or if we got the articles? 95 00:07:08,730 --> 00:07:14,640 Well, for now, I'm just going to check that the response was successful so we can get a response that 96 00:07:14,640 --> 00:07:15,900 says it's successful. 97 00:07:15,900 --> 00:07:20,610 We can get a response that it says we can get different kinds of responses, right? 98 00:07:21,090 --> 00:07:27,210 So I'm going to check if is successful, which is a Boolean, so it can be the true or false. 99 00:07:27,750 --> 00:07:33,660 And if that's the case, then I'm going to use my news response and set the value of the news response 100 00:07:33,660 --> 00:07:35,460 to the be the body of the response. 101 00:07:36,300 --> 00:07:39,990 So this will just give me the content of the response itself. 102 00:07:39,990 --> 00:07:43,260 But the body is another bill, so we need to force unwrap it. 103 00:07:43,890 --> 00:07:47,430 But we will only force on it because we know that the response was successful. 104 00:07:47,640 --> 00:07:50,610 So there will be a body, which is why it's fine to voice and rep it. 105 00:07:50,940 --> 00:07:55,130 Of course, you could also surround that within if, let's say, statement. 106 00:07:55,650 --> 00:08:00,990 So then if the response was not successful, we're just going to log the error. 107 00:08:02,770 --> 00:08:03,270 Like so. 108 00:08:03,810 --> 00:08:07,190 But then if it was successful, I would also like to see the response. 109 00:08:07,190 --> 00:08:14,790 So I would like to see what was the response body so we can just log the news as news response, dog 110 00:08:14,800 --> 00:08:15,300 value. 111 00:08:15,600 --> 00:08:19,710 So now whenever we get the news, we will displayed in our lock at for now. 112 00:08:19,800 --> 00:08:23,360 Later on, we're going to change that and displayed in our user interface. 113 00:08:25,560 --> 00:08:29,820 Now we have this beautiful method that will get us all of the articles, but we're never using it, 114 00:08:30,180 --> 00:08:31,440 so the rest is in it. 115 00:08:31,950 --> 00:08:37,350 Method the initialize the method, which will be call once the news manager will be initialized. 116 00:08:37,409 --> 00:08:44,100 So once an object of it will be created, that's where we will use to get articles method. 117 00:08:46,750 --> 00:08:55,450 OK, so now that we have that, let's initialize our news manager in the navigation composable so every 118 00:08:55,450 --> 00:08:59,080 composable can have access to it without it being initialized twice. 119 00:08:59,530 --> 00:08:59,980 All right. 120 00:08:59,980 --> 00:09:05,230 So now let's initialize our news manager in the navigation composable inside of our news app. 121 00:09:05,470 --> 00:09:11,890 So our news app has this navigation composable that we had created with the nav controller and the scroll 122 00:09:11,890 --> 00:09:12,280 state. 123 00:09:12,610 --> 00:09:15,190 But I'm going to add the news manager here as well. 124 00:09:15,190 --> 00:09:22,150 So we need to pass and use manager object whenever we want to use our navigation in the future. 125 00:09:23,310 --> 00:09:26,320 So how do we use our navigation in here? 126 00:09:26,340 --> 00:09:33,810 So here I'm just going to create an article's object which will just use my news by nature news responds 127 00:09:33,810 --> 00:09:35,490 the value, the articles. 128 00:09:36,060 --> 00:09:39,900 And now you might wonder what the hell is going on here, Dennis. 129 00:09:40,320 --> 00:09:42,240 This is like way over my head. 130 00:09:42,660 --> 00:09:43,980 Let me go over it in a second. 131 00:09:44,730 --> 00:09:48,180 So I just want to display the articles here as well. 132 00:09:48,630 --> 00:09:52,110 So let me add the log in here that's imported. 133 00:09:52,500 --> 00:10:00,240 So what's going on is we know that the news response will be of Typekit, while a state news, top news 134 00:10:00,240 --> 00:10:06,480 response and top news response has this variable called articles and the articles is what we're getting. 135 00:10:06,630 --> 00:10:15,870 So here, which is saying news response has spelled value, which will be fully added here as the value. 136 00:10:17,980 --> 00:10:23,800 Which will be the body of the response, so the response that we saw earlier here, let me show you 137 00:10:23,800 --> 00:10:24,370 once again. 138 00:10:24,730 --> 00:10:26,230 So this is the response rate. 139 00:10:26,500 --> 00:10:27,550 Whatever we. 140 00:10:28,530 --> 00:10:31,140 Call this is the example response that we might get. 141 00:10:31,980 --> 00:10:39,960 So we're just saying, give me that response and the body of it, so the code of it that the JASON code. 142 00:10:40,890 --> 00:10:47,730 And now we've taken this response and this response has articles in it. 143 00:10:47,910 --> 00:10:49,470 And I want to have those articles. 144 00:10:50,700 --> 00:10:56,380 OK, so now if we look at this, our navigation here at the top was saying that we want to have a news 145 00:10:56,400 --> 00:10:56,940 manager. 146 00:10:57,450 --> 00:11:03,060 But what I'm going to do is I'm just going to create a news manager object straight away so that I have 147 00:11:03,060 --> 00:11:07,650 a default value, which now means that this error that I had here at the top where I had to set the 148 00:11:08,190 --> 00:11:13,870 news manager will go away because if I don't set that, you will see that it will ask me for a news 149 00:11:13,890 --> 00:11:15,120 manager at the top there. 150 00:11:15,390 --> 00:11:15,890 But you know what? 151 00:11:15,900 --> 00:11:22,350 I'm just going to create an object of the news managed directly in the navigation method declaration. 152 00:11:25,060 --> 00:11:27,490 So now if we try to run our application. 153 00:11:28,860 --> 00:11:33,370 And actually, I have an emulator open already, so I. 154 00:11:39,820 --> 00:11:43,150 So now, if we run our application, we get an error. 155 00:11:44,500 --> 00:11:49,720 So the problem is, and we get, uh, big error here. 156 00:11:49,960 --> 00:11:51,400 Well, it's just jumping on me. 157 00:11:52,240 --> 00:11:52,810 Look here. 158 00:11:52,990 --> 00:11:53,530 There we are. 159 00:11:54,040 --> 00:11:59,950 So it's trying to access the internet or application, but we don't have the rights to use the internet 160 00:11:59,950 --> 00:12:00,190 yet. 161 00:12:01,090 --> 00:12:04,510 So we need to make sure that we have the rights for it. 162 00:12:04,690 --> 00:12:06,910 Therefore, we need to go over to our manifest. 163 00:12:07,960 --> 00:12:10,180 And ad usage rights. 164 00:12:10,870 --> 00:12:12,760 So how to do that is the following. 165 00:12:13,060 --> 00:12:19,360 So you need to use this user's permission keyword and then you search for internet here and use this 166 00:12:19,360 --> 00:12:20,380 internet permission. 167 00:12:20,470 --> 00:12:23,590 Like so and to close it with those brackets here. 168 00:12:24,130 --> 00:12:25,900 So what this will give us is. 169 00:12:26,350 --> 00:12:31,480 Well, this will tell the application where Android specifically, that this application needs to use 170 00:12:31,480 --> 00:12:32,080 the internet. 171 00:12:32,860 --> 00:12:34,390 And if we run it again. 172 00:12:35,600 --> 00:12:44,600 Now our application will still crash and we get a different detail, so here we say it has a problem 173 00:12:44,600 --> 00:12:50,180 with the retrofit converter, so there is something wrong with the type of data that we're giving our 174 00:12:50,180 --> 00:12:50,870 application. 175 00:12:51,590 --> 00:12:57,380 So you can find it up here unable to create converter for class you tutorials and use a use response 176 00:12:57,380 --> 00:13:00,680 so it doesn't know how to convert the top news response. 177 00:13:01,220 --> 00:13:05,240 So in order to fix that, we will need something called Moshi. 178 00:13:05,720 --> 00:13:14,420 So there is this Moshi parser that converts JSON into something that is readable for all our applications. 179 00:13:14,930 --> 00:13:18,560 So let's go over to our Gradle file. 180 00:13:20,080 --> 00:13:27,280 Here and add to dependencies, so one for Moshi Scotland, which is this one here and one that converts. 181 00:13:28,260 --> 00:13:30,030 Moshi for Retrofit two. 182 00:13:30,870 --> 00:13:39,090 So once we have added those two in there, we can now go ahead and initialize Moshi and our API here 183 00:13:39,660 --> 00:13:41,340 and pass it to it as well. 184 00:13:41,760 --> 00:13:43,830 So let's go ahead and create a Moshi object. 185 00:13:44,130 --> 00:13:54,600 So here private while Moshi, which will be using the Moshi builder and let's import Moshi for that. 186 00:13:55,260 --> 00:14:04,830 And then we're going to add the Kotlin JSON adapter factory as well as build it. 187 00:14:05,670 --> 00:14:06,060 All right. 188 00:14:06,630 --> 00:14:12,600 So this will just convert JSON into something readable for our Scotland code for our project, and then 189 00:14:12,600 --> 00:14:16,020 we can use that to add in here as well. 190 00:14:16,200 --> 00:14:22,830 So here we add a converter factory, which is going to be our Moshi converter factory. 191 00:14:23,220 --> 00:14:30,300 So Moshi converter factory thought create. 192 00:14:31,630 --> 00:14:37,300 And factories, but factory that create with our Moshi object. 193 00:14:37,570 --> 00:14:39,130 So we need to import that as well. 194 00:14:40,620 --> 00:14:48,210 Like so and now our application should be willing to execute and run and load the news. 195 00:14:48,390 --> 00:14:52,590 So let's see if it's still going to crash, but it shouldn't at this point. 196 00:14:53,310 --> 00:14:53,820 All right. 197 00:14:53,820 --> 00:14:58,200 So now if you test it and then I had the weirdest book. 198 00:14:58,650 --> 00:15:03,240 So for some reason, my emulator doesn't update the time. 199 00:15:03,480 --> 00:15:10,140 So in your case, make sure that the date is really correct so that the date works for whatever date 200 00:15:10,140 --> 00:15:12,570 it is right now or whatever time it is right now for you. 201 00:15:12,960 --> 00:15:18,610 So even though here I have date and post there already. 202 00:15:18,630 --> 00:15:22,590 So system date here I said it automatically. 203 00:15:22,590 --> 00:15:26,700 But then you see just sits to is super weird time, which is not correct. 204 00:15:26,700 --> 00:15:30,960 So I had to set it manually do whatever time it is right now. 205 00:15:30,960 --> 00:15:39,360 So it's like twelve fifty two p.m. And then it works because the problem was that if the time on your 206 00:15:39,360 --> 00:15:46,230 phone is different to the time of the response and like the the data that you get from the news API, 207 00:15:46,530 --> 00:15:48,870 then this can cause problems with your application. 208 00:15:49,320 --> 00:15:54,900 So, yeah, basically, I ran the app after that because the code is fine, right? 209 00:15:55,230 --> 00:15:57,970 And and then, yeah, then we get the response. 210 00:15:57,990 --> 00:16:00,540 So here you see the response that we get. 211 00:16:00,900 --> 00:16:03,860 So this is where it starts. 212 00:16:05,270 --> 00:16:11,540 Missy, so top news article, this is where the object starts with the source, which is Ida Reuters 213 00:16:12,140 --> 00:16:15,380 and the name of it, this Reuters and we have the author, which is not given. 214 00:16:15,530 --> 00:16:22,100 We have the title, which is Manhattan D.A., will move to exonerate two men convicted of killing Malcolm 215 00:16:22,100 --> 00:16:22,490 X. 216 00:16:23,150 --> 00:16:25,220 Then the description is two men, blah blah blah. 217 00:16:25,280 --> 00:16:29,180 Then we have the image for it, like the. 218 00:16:30,710 --> 00:16:38,720 While the image JPEG, this one here, for example, so this is the image that is given as well as the 219 00:16:39,260 --> 00:16:40,130 published date. 220 00:16:41,660 --> 00:16:44,630 So you see when it was published and the actual content. 221 00:16:44,810 --> 00:16:47,570 So this is the bigger content. 222 00:16:48,110 --> 00:16:49,340 So yeah, that's pretty much it. 223 00:16:49,610 --> 00:16:57,590 So now we get the data and we just need to display the data because it's really like now just setting 224 00:16:57,590 --> 00:17:01,190 up what we have here as the data values. 225 00:17:02,320 --> 00:17:05,470 Like setting this value to be invisible in our application. 226 00:17:05,950 --> 00:17:07,900 So, yeah, that's what we're going to do in the next video. 227 00:17:07,930 --> 00:17:09,310 So thanks a lot for following.