1 00:00:01,410 --> 00:00:01,940 Come back. 2 00:00:02,340 --> 00:00:08,460 So in this video and maybe also in the second one and the third one, so in the next couple of videos, 3 00:00:08,460 --> 00:00:12,570 we are going to refactor our application. 4 00:00:12,570 --> 00:00:16,800 So we really need to make a couple of changes, as you have seen in the presentation. 5 00:00:16,800 --> 00:00:22,330 So we're going to use call routines, for example, instead of calling retrofit directing. 6 00:00:22,560 --> 00:00:26,280 So let's just get started and rebuild our application step by step. 7 00:00:26,670 --> 00:00:28,410 This will be quite tricky. 8 00:00:28,410 --> 00:00:29,950 And um. 9 00:00:30,510 --> 00:00:33,900 Well, the idea behind it is that you on one hand, will learn movement. 10 00:00:33,900 --> 00:00:39,270 On the other hand, you will learn how to refactor your own application. 11 00:00:39,270 --> 00:00:45,660 So if you will have built something already and now you want to refactor it so that it will use a certain 12 00:00:45,660 --> 00:00:51,630 architecture in this case and VM, then it will be really helpful to do as you will see. 13 00:00:52,260 --> 00:00:54,570 So here's the difference. 14 00:00:54,750 --> 00:01:01,470 If we want to use a suspend function, a call routine here instead of retrofit, what we need to do 15 00:01:01,470 --> 00:01:03,840 is we need to add the suspend keyword. 16 00:01:04,349 --> 00:01:09,930 Now this function will be a call routine function, which is a function that can run in the background. 17 00:01:10,290 --> 00:01:16,380 But then what we need to return is not going to be a call, but just the top response itself. 18 00:01:16,890 --> 00:01:18,990 So here we don't need to return to call. 19 00:01:19,350 --> 00:01:25,110 And the same thing goes for dysfunction here, so get articles by category. 20 00:01:25,380 --> 00:01:29,010 Let's make that a suspend function as well. 21 00:01:29,670 --> 00:01:33,420 We're going to look at the others later on, but for now, let's just add those two. 22 00:01:34,690 --> 00:01:35,050 Then. 23 00:01:37,150 --> 00:01:45,880 Let's jump over to our news manager and in our news manager, we need to create news while a service 24 00:01:45,880 --> 00:01:47,710 as a parameter to the news manager. 25 00:01:48,130 --> 00:01:53,920 So here private vol service, which will be the news service. 26 00:01:54,070 --> 00:02:01,120 So now we're passing the news service to our news manager as a service and then the function that we 27 00:02:01,120 --> 00:02:01,690 had there. 28 00:02:01,700 --> 00:02:02,770 Get articles. 29 00:02:03,130 --> 00:02:03,700 This one? 30 00:02:04,900 --> 00:02:08,080 You see, now doesn't work as we had it before. 31 00:02:08,470 --> 00:02:15,430 So instead of running all of this stuff that we had in the past, we're going to really simplify this. 32 00:02:18,030 --> 00:02:26,070 And we're going to use an AOL dispatcher now, so let me get rid of this entire part. 33 00:02:27,200 --> 00:02:34,040 So we get a service now, so we don't need to use the retrofit service anymore as we did, and we get 34 00:02:34,040 --> 00:02:34,990 rid of this as well. 35 00:02:35,030 --> 00:02:43,490 So the articles function will now need to get the country, which will be of type string, and it will 36 00:02:43,490 --> 00:02:50,300 only return a top news response where we are going to build that with context. 37 00:02:50,330 --> 00:02:53,030 So here with context. 38 00:02:54,970 --> 00:03:03,820 Like soul and we this pass a dispatcher, so an old dispatcher, i o standing for input output. 39 00:03:04,390 --> 00:03:11,320 So dispatchers start i o and then inside of it, we are going to then. 40 00:03:13,160 --> 00:03:22,760 Use our service, so service that get top articles, and here we needed to pass in the country, which 41 00:03:22,760 --> 00:03:24,320 will be this country here. 42 00:03:28,880 --> 00:03:34,610 In order for this to work, we need to make this a suspend function also, only then can we use with 43 00:03:34,610 --> 00:03:35,270 context. 44 00:03:35,720 --> 00:03:39,770 And you see, we cannot use get articles like we did in the in it any more. 45 00:03:39,770 --> 00:03:43,910 We don't need to do that anymore, so we can get rid of that as well. 46 00:03:46,770 --> 00:03:54,360 Now, as I said in the presentation, we will need to create a repository where we can then pass our 47 00:03:54,360 --> 00:03:56,580 manager, slash our news manager. 48 00:03:58,720 --> 00:04:06,430 Which I'm going to put into a new folder slash directory or package, which I'm going to call data. 49 00:04:07,530 --> 00:04:13,560 And into data, I should actually put our models as well, because if you look at it, that's where 50 00:04:14,100 --> 00:04:14,880 it would fit. 51 00:04:15,300 --> 00:04:17,930 So we could refactor it and put it in there. 52 00:04:17,970 --> 00:04:24,000 But for now, I'm just going to create my repository in here, which will be a Scotland file class, 53 00:04:24,000 --> 00:04:28,530 a class to be more precise, and I'm going to call this one repository. 54 00:04:29,900 --> 00:04:36,110 Like so so this repository will need a manager, which will be our newsman that year. 55 00:04:36,620 --> 00:04:39,830 OK, so I will be required and use manager. 56 00:04:39,830 --> 00:04:42,050 We need to import news manager for this to work. 57 00:04:42,380 --> 00:04:48,050 And then inside of this repository, we can now get the articles so we can just go ahead and get articles, 58 00:04:48,050 --> 00:04:48,800 for example. 59 00:04:49,460 --> 00:04:56,600 And in order to get the articles, we're just going to what we're going to assign the manager to get 60 00:04:56,600 --> 00:05:01,100 articles methods directly in here for the US articles. 61 00:05:01,760 --> 00:05:01,970 OK. 62 00:05:02,040 --> 00:05:02,510 Like so. 63 00:05:03,020 --> 00:05:13,880 And then accordingly, we will also have a suspend function for to get articles by category. 64 00:05:15,810 --> 00:05:16,920 All right, so here. 65 00:05:17,280 --> 00:05:23,340 Well, if we want to get it by category, we need to pass in the category, which will be of type string. 66 00:05:23,940 --> 00:05:32,250 And this will be why we're going to assign manager dogs, get articles by category with the category 67 00:05:33,060 --> 00:05:36,610 and here like this with the category? 68 00:05:36,630 --> 00:05:38,250 Let me see where we get that from. 69 00:05:39,060 --> 00:05:40,970 I did write it incorrectly here. 70 00:05:41,000 --> 00:05:42,210 I call it category. 71 00:05:42,660 --> 00:05:50,670 OK, so with the category that we get here, we're going to assign it to our articles, my by category. 72 00:05:53,490 --> 00:06:01,950 So what we have here is a single expression function, and what it does is it just returns what's to 73 00:06:01,950 --> 00:06:03,690 the right of the equal sign. 74 00:06:03,930 --> 00:06:08,610 So it's just going to return, whatever, to get articles method is going to do and it returns a top 75 00:06:08,610 --> 00:06:09,540 news response. 76 00:06:10,290 --> 00:06:13,230 All right now, we are going to require a new. 77 00:06:15,220 --> 00:06:16,480 Class as well here. 78 00:06:16,690 --> 00:06:18,850 And I'm going to put that in here. 79 00:06:18,880 --> 00:06:23,260 New class and this one will be called main app. 80 00:06:23,470 --> 00:06:23,890 All right. 81 00:06:24,220 --> 00:06:28,150 And this class will return an application. 82 00:06:28,390 --> 00:06:33,430 So this will be our actual application class main app. 83 00:06:33,790 --> 00:06:40,390 And here we need to have the brackets as well as import application. 84 00:06:40,960 --> 00:06:43,720 So what should happen in this main application? 85 00:06:43,900 --> 00:06:46,870 Well, the main application is going to handle stuff. 86 00:06:48,330 --> 00:06:54,270 So in this application class, we're going to initialize the newsman that your first as the first well 87 00:06:54,270 --> 00:07:00,060 as the repository needs it when we initialize the repository, providing the manager as an argument 88 00:07:00,360 --> 00:07:07,020 would use by lazy to ensure they are only initialized at the point of use and not directly when the 89 00:07:07,020 --> 00:07:08,070 main app is created. 90 00:07:08,370 --> 00:07:20,610 So private vault manager by lazy, which will then create a news manager where we are passing the API 91 00:07:20,940 --> 00:07:22,750 DOD retrofit service. 92 00:07:23,670 --> 00:07:26,730 So let's import news manager here for us to be able to use it. 93 00:07:27,060 --> 00:07:29,880 And then we need to have the repository as well. 94 00:07:30,060 --> 00:07:36,030 So repository, which will be created by lazy as well here. 95 00:07:36,780 --> 00:07:43,290 So only once it is required, so repository, which is the class that we created earlier and we're going 96 00:07:43,290 --> 00:07:45,420 to pass our manager as the manager. 97 00:07:45,660 --> 00:07:47,540 So let's import repository. 98 00:07:47,550 --> 00:07:48,690 So it's this one, right? 99 00:07:49,020 --> 00:07:56,190 Where do you recall that we needed to assign or pass a manager to our repositories so that we can then 100 00:07:56,190 --> 00:08:02,850 use it in order to call the Get Articles method, as well as the Get article by category method? 101 00:08:03,270 --> 00:08:09,690 And then we can make suspend functions from them so that they will be run in the background using call 102 00:08:09,690 --> 00:08:15,000 routines, because suspend is the key word that we're assigning to a function to make it a call routine 103 00:08:15,000 --> 00:08:15,540 function. 104 00:08:16,020 --> 00:08:21,270 So a function that will not run on the main Fred, but will run in the background and will notify us 105 00:08:21,270 --> 00:08:24,120 once it's done executing whatever it needs to execute. 106 00:08:26,970 --> 00:08:34,169 Now that we have our main app, we need to assign our main app as the app in our Android Manifest because, 107 00:08:34,740 --> 00:08:37,809 well, in here currently. 108 00:08:38,549 --> 00:08:39,090 Check. 109 00:08:40,500 --> 00:08:47,330 We don't have an app while we have the application here at the top and the app name is not defined, 110 00:08:47,340 --> 00:08:49,920 so here it's just name. 111 00:08:50,250 --> 00:08:52,980 And you were just going to use our domain app. 112 00:08:52,980 --> 00:08:57,660 So how you do it is used as Android name property and you assigned domain app to it. 113 00:08:58,350 --> 00:09:00,050 OK, which is our main app. 114 00:09:00,060 --> 00:09:06,570 So this will be the application that will take care of all the entire episodes because will be our app. 115 00:09:06,960 --> 00:09:07,400 All right. 116 00:09:07,410 --> 00:09:13,410 And now we can actually create our VM, which means our view model. 117 00:09:15,990 --> 00:09:20,400 Well, I'm going to put into my U.I folder, so here. 118 00:09:21,400 --> 00:09:28,090 And you I am going to create a new class, which I'm going to call main view model. 119 00:09:29,020 --> 00:09:29,400 All right. 120 00:09:29,410 --> 00:09:32,490 And this class main view model needs to have an application. 121 00:09:32,530 --> 00:09:38,800 So we need to pass our application to it and it will return Android View model. 122 00:09:39,610 --> 00:09:46,450 And this one needs to have the application that we are getting earlier or that we're passing as well. 123 00:09:47,080 --> 00:09:50,760 So now we need to get the repository from the main app in here. 124 00:09:50,770 --> 00:09:58,550 So let's get that by creating a new variable where we're going to pass or store it repos the toll free, 125 00:09:59,140 --> 00:10:02,770 which we are going to get from our application main app. 126 00:10:03,580 --> 00:10:04,720 So here, main app. 127 00:10:05,970 --> 00:10:10,590 And there it was our repository. 128 00:10:11,010 --> 00:10:15,660 And let's import May repository for rape like this. 129 00:10:16,170 --> 00:10:20,610 So this allows us to get our repository on our main map or men app here. 130 00:10:21,210 --> 00:10:28,940 And then we need to create a smarter and better for our news response, which is a mutable state flow. 131 00:10:28,950 --> 00:10:35,730 So here private vol news response, which will be immutable. 132 00:10:37,590 --> 00:10:42,090 State flow with a top news response. 133 00:10:43,850 --> 00:10:46,640 All right, so let's import a mutable state flow here. 134 00:10:46,940 --> 00:10:47,690 This class. 135 00:10:48,910 --> 00:10:49,420 Like that? 136 00:10:50,320 --> 00:10:53,620 And now we can create the news of response. 137 00:10:55,300 --> 00:10:55,990 Which will. 138 00:10:57,410 --> 00:11:00,920 The F-Type's state flow with the top news response. 139 00:11:02,140 --> 00:11:04,840 And let's import state flow as well. 140 00:11:06,340 --> 00:11:11,500 And then finally, get or have the getter, which will give us the news response. 141 00:11:14,560 --> 00:11:18,370 So for this view model class, we will focus on the top news first. 142 00:11:18,670 --> 00:11:21,430 We will use to Android View model instead of the view model. 143 00:11:21,670 --> 00:11:27,280 This is because we need access to the application class, which comes with the Android View model will 144 00:11:27,280 --> 00:11:33,160 get the repository from the main app using the application context, which is this get application here 145 00:11:33,910 --> 00:11:43,180 and create a certain and getter for the type mutable state flow and state flow as a holder for the articles 146 00:11:43,180 --> 00:11:43,850 response. 147 00:11:43,870 --> 00:11:46,300 So it holds our top news response. 148 00:11:46,840 --> 00:11:53,800 So here, private Vul on underscore score is loading will be immutable. 149 00:11:55,300 --> 00:11:56,020 State law. 150 00:11:57,470 --> 00:11:59,660 Well, I'm going to pass through to it. 151 00:12:00,740 --> 00:12:05,690 So is loading is going to be through and then let's create the. 152 00:12:07,510 --> 00:12:14,380 Public state flow here to one that we can access state flow Boolean, which will. 153 00:12:15,790 --> 00:12:18,250 Just be our is loading. 154 00:12:19,810 --> 00:12:22,540 And here is loading like this. 155 00:12:23,440 --> 00:12:27,070 So let me check, is bullion equals just loading. 156 00:12:27,110 --> 00:12:29,080 Okay, there needs to be an empty space in between. 157 00:12:30,010 --> 00:12:37,930 So this will be the variable that checks the loading states and then we create a method to launch to 158 00:12:37,930 --> 00:12:38,650 get articles. 159 00:12:38,650 --> 00:12:42,640 Method so fun gets top articles. 160 00:12:45,280 --> 00:12:51,360 Which is checking the loading state where they will set the value of it to true. 161 00:12:51,520 --> 00:12:54,790 So when we are getting the top articles, we are no. 162 00:12:54,970 --> 00:13:02,320 While we know that we are loading something, then we launch our view model scope. 163 00:13:04,180 --> 00:13:05,920 To launch a call routine. 164 00:13:06,040 --> 00:13:16,900 So here we're going to use our dispatchers, which is in Scotland call routines, thoughts i o and we 165 00:13:16,900 --> 00:13:23,500 are setting the news response value to be repository thoughts, good articles. 166 00:13:25,180 --> 00:13:34,960 So let's import the new model scope and let's import launch, OK, and then once we have the articles 167 00:13:35,500 --> 00:13:41,080 we're setting, there is loading state again to be not loading. 168 00:13:41,260 --> 00:13:42,430 So two false. 169 00:13:45,140 --> 00:13:46,220 So what is going on? 170 00:13:46,700 --> 00:13:49,490 So we have this main view model, which. 171 00:13:50,550 --> 00:13:58,080 Is the combination or the connection between the view and the model, so we're using it as a layer in 172 00:13:58,080 --> 00:13:59,220 between, so to speak. 173 00:13:59,460 --> 00:14:03,480 And now when we want to get the top articles, we're going to use a cold routine. 174 00:14:03,660 --> 00:14:06,900 So when we want to use a cold routine, which is our jet articles method. 175 00:14:07,260 --> 00:14:13,470 So this is a cold routine, and whenever you want to work with cold routines, you need to surround 176 00:14:13,470 --> 00:14:13,860 them. 177 00:14:15,620 --> 00:14:21,770 With a view model scope, so you can only launch them inside of the view model scope, you need to say 178 00:14:21,770 --> 00:14:26,600 on which Fred, you want to run them, so to speak, right of which kind of a threat is not really a 179 00:14:26,600 --> 00:14:27,680 threat, but something similar. 180 00:14:27,980 --> 00:14:31,220 And then you can run it and we're running it here. 181 00:14:31,220 --> 00:14:38,390 We're saying, OK, so the news response state, which is state flow here with whatever the articles 182 00:14:38,390 --> 00:14:38,720 are. 183 00:14:39,650 --> 00:14:43,370 And we're also setting this is loading value accordingly. 184 00:14:46,800 --> 00:14:55,590 So we're going to use this in order to display the loading an indicator so the progress indicator or 185 00:14:55,590 --> 00:14:57,090 progress bar, so to speak. 186 00:14:58,130 --> 00:15:02,510 So now in our news app, Katie File. 187 00:15:05,170 --> 00:15:12,640 Within the navigation method here, we need to make changes because you see we get an error because 188 00:15:12,640 --> 00:15:14,560 the articles are used differently. 189 00:15:18,350 --> 00:15:23,000 So here we get articles by category. 190 00:15:23,540 --> 00:15:25,520 So for now, let's. 191 00:15:26,470 --> 00:15:27,460 Comment this out. 192 00:15:27,820 --> 00:15:29,980 So for now, I'm going to comment this out. 193 00:15:31,060 --> 00:15:33,520 I'm going to do it in a different way later on. 194 00:15:34,300 --> 00:15:40,300 And let's also fix the error that we have here where the news manager isn't too happy. 195 00:15:40,960 --> 00:15:45,310 So we need to instead of passing the news manager here. 196 00:15:45,340 --> 00:15:52,120 Well, actually, it's fine that we passed the news manager, but we need to pass the retrofit service. 197 00:15:52,120 --> 00:15:57,910 So I don't retrofit service because if you look at our news manager, you see now it needs a new service. 198 00:15:58,600 --> 00:16:03,130 So the news service itself is this interface. 199 00:16:03,820 --> 00:16:09,630 And if you want to use this interface, we need to see what's in the news app, Katie. 200 00:16:10,030 --> 00:16:18,910 So we need to use it by using our API object where we then used our retrofit service. 201 00:16:19,390 --> 00:16:22,330 This one here, which creates the new service itself. 202 00:16:23,580 --> 00:16:23,860 Okay. 203 00:16:23,860 --> 00:16:33,100 And then in our news app, we need to pass something else, so we will now need to also pass the view 204 00:16:33,100 --> 00:16:33,910 model itself. 205 00:16:34,270 --> 00:16:41,590 So view model of type main view model will need to be passed at this point as well. 206 00:16:41,860 --> 00:16:43,330 So we're passing all of these. 207 00:16:46,480 --> 00:16:51,550 Which now means that we also need to pass it inside of our main screen here. 208 00:16:52,690 --> 00:16:59,560 So the main screen here at the top when we want to use it will need the main view model as well. 209 00:17:00,580 --> 00:17:01,140 Like this? 210 00:17:02,250 --> 00:17:04,329 Let me put it into a separate line like that. 211 00:17:04,990 --> 00:17:14,560 And then we can pass that to our navigation composable here because at this point, it doesn't get it 212 00:17:14,560 --> 00:17:14,829 yet. 213 00:17:15,069 --> 00:17:25,000 So let's pass the view model as well here, which will be our view model, which is the MSI. 214 00:17:25,359 --> 00:17:26,920 It is the main view model. 215 00:17:26,920 --> 00:17:27,190 Yes. 216 00:17:28,250 --> 00:17:30,710 OK, so this main view model that we're passing to it. 217 00:17:34,600 --> 00:17:40,570 And I'd say, let's make a quick break at this point because we still have quite some steps ahead of 218 00:17:40,570 --> 00:17:47,950 us, as you see, the news app isn't too happy because the main screen also needs the news model and 219 00:17:47,950 --> 00:17:50,840 so forth, so we're going to take care of that in a bit. 220 00:17:51,190 --> 00:17:52,390 So see you in the next video.