Come back.
So in this video and maybe also in the second one and the third one, so in the next couple of videos,
we are going to refactor our application.
So we really need to make a couple of changes, as you have seen in the presentation.
So we're going to use call routines, for example, instead of calling retrofit directing.
So let's just get started and rebuild our application step by step.
This will be quite tricky.
And um.
Well, the idea behind it is that you on one hand, will learn movement.
On the other hand, you will learn how to refactor your own application.
So if you will have built something already and now you want to refactor it so that it will use a certain
architecture in this case and VM, then it will be really helpful to do as you will see.
So here's the difference.
If we want to use a suspend function, a call routine here instead of retrofit, what we need to do
is we need to add the suspend keyword.
Now this function will be a call routine function, which is a function that can run in the background.
But then what we need to return is not going to be a call, but just the top response itself.
So here we don't need to return to call.
And the same thing goes for dysfunction here, so get articles by category.
Let's make that a suspend function as well.
We're going to look at the others later on, but for now, let's just add those two.
Then.
Let's jump over to our news manager and in our news manager, we need to create news while a service
as a parameter to the news manager.
So here private vol service, which will be the news service.
So now we're passing the news service to our news manager as a service and then the function that we
had there.
Get articles.
This one?
You see, now doesn't work as we had it before.
So instead of running all of this stuff that we had in the past, we're going to really simplify this.
And we're going to use an AOL dispatcher now, so let me get rid of this entire part.
So we get a service now, so we don't need to use the retrofit service anymore as we did, and we get
rid of this as well.
So the articles function will now need to get the country, which will be of type string, and it will
only return a top news response where we are going to build that with context.
So here with context.
Like soul and we this pass a dispatcher, so an old dispatcher, i o standing for input output.
So dispatchers start i o and then inside of it, we are going to then.
Use our service, so service that get top articles, and here we needed to pass in the country, which
will be this country here.
In order for this to work, we need to make this a suspend function also, only then can we use with
context.
And you see, we cannot use get articles like we did in the in it any more.
We don't need to do that anymore, so we can get rid of that as well.
Now, as I said in the presentation, we will need to create a repository where we can then pass our
manager, slash our news manager.
Which I'm going to put into a new folder slash directory or package, which I'm going to call data.
And into data, I should actually put our models as well, because if you look at it, that's where
it would fit.
So we could refactor it and put it in there.
But for now, I'm just going to create my repository in here, which will be a Scotland file class,
a class to be more precise, and I'm going to call this one repository.
Like so so this repository will need a manager, which will be our newsman that year.
OK, so I will be required and use manager.
We need to import news manager for this to work.
And then inside of this repository, we can now get the articles so we can just go ahead and get articles,
for example.
And in order to get the articles, we're just going to what we're going to assign the manager to get
articles methods directly in here for the US articles.
OK.
Like so.
And then accordingly, we will also have a suspend function for to get articles by category.
All right, so here.
Well, if we want to get it by category, we need to pass in the category, which will be of type string.
And this will be why we're going to assign manager dogs, get articles by category with the category
and here like this with the category?
Let me see where we get that from.
I did write it incorrectly here.
I call it category.
OK, so with the category that we get here, we're going to assign it to our articles, my by category.
So what we have here is a single expression function, and what it does is it just returns what's to
the right of the equal sign.
So it's just going to return, whatever, to get articles method is going to do and it returns a top
news response.
All right now, we are going to require a new.
Class as well here.
And I'm going to put that in here.
New class and this one will be called main app.
All right.
And this class will return an application.
So this will be our actual application class main app.
And here we need to have the brackets as well as import application.
So what should happen in this main application?
Well, the main application is going to handle stuff.
So in this application class, we're going to initialize the newsman that your first as the first well
as the repository needs it when we initialize the repository, providing the manager as an argument
would use by lazy to ensure they are only initialized at the point of use and not directly when the
main app is created.
So private vault manager by lazy, which will then create a news manager where we are passing the API
DOD retrofit service.
So let's import news manager here for us to be able to use it.
And then we need to have the repository as well.
So repository, which will be created by lazy as well here.
So only once it is required, so repository, which is the class that we created earlier and we're going
to pass our manager as the manager.
So let's import repository.
So it's this one, right?
Where do you recall that we needed to assign or pass a manager to our repositories so that we can then
use it in order to call the Get Articles method, as well as the Get article by category method?
And then we can make suspend functions from them so that they will be run in the background using call
routines, because suspend is the key word that we're assigning to a function to make it a call routine
function.
So a function that will not run on the main Fred, but will run in the background and will notify us
once it's done executing whatever it needs to execute.
Now that we have our main app, we need to assign our main app as the app in our Android Manifest because,
well, in here currently.
Check.
We don't have an app while we have the application here at the top and the app name is not defined,
so here it's just name.
And you were just going to use our domain app.
So how you do it is used as Android name property and you assigned domain app to it.
OK, which is our main app.
So this will be the application that will take care of all the entire episodes because will be our app.
All right.
And now we can actually create our VM, which means our view model.
Well, I'm going to put into my U.I folder, so here.
And you I am going to create a new class, which I'm going to call main view model.
All right.
And this class main view model needs to have an application.
So we need to pass our application to it and it will return Android View model.
And this one needs to have the application that we are getting earlier or that we're passing as well.
So now we need to get the repository from the main app in here.
So let's get that by creating a new variable where we're going to pass or store it repos the toll free,
which we are going to get from our application main app.
So here, main app.
And there it was our repository.
And let's import May repository for rape like this.
So this allows us to get our repository on our main map or men app here.
And then we need to create a smarter and better for our news response, which is a mutable state flow.
So here private vol news response, which will be immutable.
State flow with a top news response.
All right, so let's import a mutable state flow here.
This class.
Like that?
And now we can create the news of response.
Which will.
The F-Type's state flow with the top news response.
And let's import state flow as well.
And then finally, get or have the getter, which will give us the news response.
So for this view model class, we will focus on the top news first.
We will use to Android View model instead of the view model.
This is because we need access to the application class, which comes with the Android View model will
get the repository from the main app using the application context, which is this get application here
and create a certain and getter for the type mutable state flow and state flow as a holder for the articles
response.
So it holds our top news response.
So here, private Vul on underscore score is loading will be immutable.
State law.
Well, I'm going to pass through to it.
So is loading is going to be through and then let's create the.
Public state flow here to one that we can access state flow Boolean, which will.
Just be our is loading.
And here is loading like this.
So let me check, is bullion equals just loading.
Okay, there needs to be an empty space in between.
So this will be the variable that checks the loading states and then we create a method to launch to
get articles.
Method so fun gets top articles.
Which is checking the loading state where they will set the value of it to true.
So when we are getting the top articles, we are no.
While we know that we are loading something, then we launch our view model scope.
To launch a call routine.
So here we're going to use our dispatchers, which is in Scotland call routines, thoughts i o and we
are setting the news response value to be repository thoughts, good articles.
So let's import the new model scope and let's import launch, OK, and then once we have the articles
we're setting, there is loading state again to be not loading.
So two false.
So what is going on?
So we have this main view model, which.
Is the combination or the connection between the view and the model, so we're using it as a layer in
between, so to speak.
And now when we want to get the top articles, we're going to use a cold routine.
So when we want to use a cold routine, which is our jet articles method.
So this is a cold routine, and whenever you want to work with cold routines, you need to surround
them.
With a view model scope, so you can only launch them inside of the view model scope, you need to say
on which Fred, you want to run them, so to speak, right of which kind of a threat is not really a
threat, but something similar.
And then you can run it and we're running it here.
We're saying, OK, so the news response state, which is state flow here with whatever the articles
are.
And we're also setting this is loading value accordingly.
So we're going to use this in order to display the loading an indicator so the progress indicator or
progress bar, so to speak.
So now in our news app, Katie File.
Within the navigation method here, we need to make changes because you see we get an error because
the articles are used differently.
So here we get articles by category.
So for now, let's.
Comment this out.
So for now, I'm going to comment this out.
I'm going to do it in a different way later on.
And let's also fix the error that we have here where the news manager isn't too happy.
So we need to instead of passing the news manager here.
Well, actually, it's fine that we passed the news manager, but we need to pass the retrofit service.
So I don't retrofit service because if you look at our news manager, you see now it needs a new service.
So the news service itself is this interface.
And if you want to use this interface, we need to see what's in the news app, Katie.
So we need to use it by using our API object where we then used our retrofit service.
This one here, which creates the new service itself.
Okay.
And then in our news app, we need to pass something else, so we will now need to also pass the view
model itself.
So view model of type main view model will need to be passed at this point as well.
So we're passing all of these.
Which now means that we also need to pass it inside of our main screen here.
So the main screen here at the top when we want to use it will need the main view model as well.
Like this?
Let me put it into a separate line like that.
And then we can pass that to our navigation composable here because at this point, it doesn't get it
yet.
So let's pass the view model as well here, which will be our view model, which is the MSI.
It is the main view model.
Yes.
OK, so this main view model that we're passing to it.
And I'd say, let's make a quick break at this point because we still have quite some steps ahead of
us, as you see, the news app isn't too happy because the main screen also needs the news model and
so forth, so we're going to take care of that in a bit.
So see you in the next video.