Welcome back.
So at the end of this video, we are going to be done with refactoring our application, at least to
a point where we can see our articles again because at this point think we couldn't because we have
a bunch of errors.
So let's finalize that and then we will be done with the first part of our refactoring.
So now let's go over to our main activity file.
This year, we will need to initialize the view model because our main activity, that's where everything
is inside of it, right?
So the news app is inside of it, right in here, and the news app contains all of the content.
So it contains to state the controllers and the main screen.
So basically, this is our main screen where everything is going on.
OK, so that's our main activity.
And in here, we can set up our view model.
So private will view model, which will be by view model.
And more models, plural, where it's going to be our main view model.
So like this and we need to import view models.
So it's this here what we need.
Main view model here it's activity view models.
That's where we get this view models from.
Which we are using in order to initialize our main view model that we created.
So we will lift up its declaration up until the news app by creating it as a parameter in the main screen
and new setup, and then provide as an argument where required.
So what we're basically doing is we're setting it up at the very top level and then we're pushing it
through to the point where it is required, where it's going to be then consumed and used.
Because our view model contains our repository, it contains whether we are loading something, it allows
us to get the top articles and so forth.
So we create this in the main activity, then we are passing it down to our main screen, which then
passes it to the news manager and so forth, right?
So let's go ahead and do that.
So instead of on create.
Before we set the content, let's use our review model.
Get top articles.
So we're going to now load the articles already and then we're going to pass our review model to our
news app.
So Pastor, my model like this to the news app.
And.
We need to hear just pass a view model.
Like so right with a capital M..
So you muddle.
Like this?
Well, it's important for this to work.
This error will disappear once we have taken care of the news app because our news app will require
the main view model inside of it.
So here the declaration.
So we're now passing the View model to our news app, and the news app can then pass it to the main
screen.
So here Main View model, which will be our mini view model.
So this one here, we're passing it in there.
So now the main screen gets the view model because it needed it right and it can pass it to the navigation.
The navigation gets it and can pass it down to the bottom navigation down here and display it.
So at this point, let me see.
Yeah, it passes that down here, and that's where it can then be consumed inside of the composable,
where we have the bottom menu screen and we're going to use it there.
But before we can do that, let's get back to our main view model, because here we will move the center
and getter for the article by category, from the Muse manager to the.
Category two, our main view model.
OK, so then news manager so far has taken care of that.
Let me see this one here.
And I'm going to cut the get article by category this one here.
I'm going to cut it from here and put it over to my main view model.
And then, well, we have our get, but we don't need to get it to weather, remember anymore.
So.
So here it will look slightly different.
So we get article by category where we are not using a mutable state flow, so mutable state flow.
Top news response.
We get the article by category of type state flow with top news response and then this will not be a
composable get.
And this will not be a remember, but it will just be the get article by category.
So like this?
OK, so that's the well, how we can get to the article by category now.
So we're not using a mutable item anymore, but the music musical state flow.
So it's an mutable state of mutable state flow.
No.
All right, and then we need to create or have the method get articles by category, so we have the
variable now let's create the function next.
So get articles by category.
Like this cat, the gory cat to gory, which will be of type string and then here we can set is loading
value to true because when we are getting the articles, we should be loading.
And once we're done loading the articles, we will set that value to be false.
So he falls, but in between, we need to do the same thing that we did here where we launch.
And this one is going to be get articles by category value will be repository, get articles by category
and he will need to pass the category like this.
Now we will get this error because it says get articles by category.
Does this type mismatch?
So let's look at it here.
There were call calling or using our manager that is passed to the repository to call the get articles
by category method.
And you see that this method is still is using the non, let's say, non-call routine approach, which
was all of this stuff with the call and quite complex things that we're doing.
So we want to do the same thing as we have done in our get articles, right?
So but then we're getting them by category.
So let's make a quick change here to our news manager to you.
Also, just use this very short hand code that we had there so we can get rid of all that other stuff.
And now we can directly assign the same thing that we've done here.
So let me just copy that and put it in here like this.
So we net well, we get the.
The top news responds with context with the dispatchers, and then it will not be the service, not
get articles, but get articles by category where we now need to pass the category instead of the country.
So category like this OK category?
Now, if you want to use the term with context key, what would need to make this a suspend function,
which it is because now we're using a call routine, right?
So you see the code gets a lot briefer here, don't need to have these callback thingies and so forth.
So that's really well, that's the twenty twenty two approach, so to speak, to working with things
that should run in the background.
So we use retrofit, which is very useful, but now we are going to, um, well, export death and do
everything using call routine, so to speak.
So it's still going to use calls, but it's going to be suspend function calls, so here you can see
the arrow here with the Green Line in between or going through it.
So now back in our main view model, now the error should disappear.
But we're going to have a smarter and better for the articles by while we get articles by category,
but we also need to have a.
Well, have the getters and setters to get them selected categories.
So whatever we have selected, so private fall selected, we actually used the same thing.
So it's going to be the same thing as we've done here, but only that this will be the survey collected.
Category.
So select category and this one will be selected category.
Everything else will hear it will be the article category.
The mutable state flow will be of type article category as well.
So here we need to pass the article category, but this will be a Nullarbor or it could be another Nullarbor.
And we are going to set the mutable state flow personal to it.
OK, so this will give him the state flow of article category Nullarbor.
And then we get the selected category.
OK, now this is the type.
So when at the signing, we're signing at the right hand side, so.
So what we're doing here is we're creating this new variable, which will be of type mutable state flow,
specifically of type article category Nullarbor.
And we're assigning null to the new tuple state law at this point.
And then this is just.
Setter and here we have to get her.
And now we need to have a function which will be called once we selected a well, once selected category
has changed.
So we need to pass in the category of type string.
And then we're going to set the new category.
To come from get articles by article singular by category where we passed the category and think I called
this one.
Articles by category yes.
Where 90 percent category.
And then the selected category will be set, so its value will be overwritten with the new category
that we're getting.
This obviously only will work if they are of the same type, so here the selected category is of article
category.
Let me check some mutable state flow.
Now and then, this is a state flow of time article category, and here we get the article by category.
And therefore, we need to go over to our article category Enum, So article category, you know this
one here, let me check.
So the article category enum gives us an article category.
And this would also be the function that I'm using, so I'm using the wrong function here.
Get Article Missy.
So in our article category, we had this.
Good article category, so this method is that the one that I want to use get article category.
All right.
So that's what we need to pass because we wanted to get the Category Enum value based on that.
OK.
All right, and now in the categories themselves.
So let's go over two categories.
Katie, and this file, you will find that we get an error here because the newsman, as you get article
category, doesn't seem to work anymore, so we need to replace the newsman as you hear at the top with
our new model.
Because as I said, we're going to cover everything via our view model, so view model, which will
be the main view model.
All right.
This one.
And then we can collect the selected category from our view model instead of getting it from our news
manager.
So here I'm going to replace news manager with the new model.
And the same goes here.
So replacing it with our view model.
All right, and now we can go over to our news app where you will see that we're going to get errors
because here our composable webpage displays two categories.
The news manager doesn't seem to work anymore, obviously, because now we're not going to use the news
manager anymore.
We're using the view model here instead.
So view model, which will be the.
View model and see and like this.
You model.
And what do we get that view model from, we should get it here from our bottom navigation here at the
top, where instead of walnut instead, but on top of passing our news manager, we're passing our main
view model here.
And it's well, let's just call it view model.
OK, so here we can then use the view model and now accordingly, we're not going to use our news manager
to get the articles by category and neither to get the unselected category changed, but instead we're
going to use our new model to take care of all of that.
So view model, good article by category what we're going to, for example, pass.
Well, actually, it's this one here.
So here inside of categories.
We're going to use our on selected category changed.
With.
Whatever was selected.
So with it.
OK, so would the string that this pass to the categories, so here and then we're going to also get
articles by that category.
So view model thought good articles by category it.
So here we are changing the selected option in our news app.
So let's say we change from business to sports, for example, then this method needs to be called to
now know which category we need to load again because the category now will have to be changed and now
we need to load the news again.
But then here I said that we should uncomment or comment out this business thing you hear where we get
articles by category.
So now we can uncomment it, but past of human or use our view model here to get articles by category.
And then also use your model here.
On selected category change with business.
OK, so now let me see if I have any errors in my application still.
Here are bottom navigation needs to get the news well, needs to get the view model as well.
So this navigation view model that would get your top, the main one wanted to pass it directly to our
bottom navigation, which as you see here, needs it now and then.
That's pretty much it.
So let's run it, and let's see, verification still runs.
But the thing is that you see that we needed to basically set our our main view model, set up the call
routines and then just send everything over correctly.
Oh yes.
So this year in the search bar.
Let me see the search bar doesn't seem to be happy.
But only the preview itself, which is good.
So he had a news manager needs to get the API retrofit service.
So let's run it again.
Yeah, so we needed to set up all of this.
Um, well, there's a walk around ish thing.
And if we test it now, well, in the categories, it seems to work, the sources are being loaded.
But our top news itself is not being displayed correctly yet, so let's check what's wrong there.
So that should be this should be in our news app.
Let's go over to our news app.
Here.
Well, we're at the bottom navigation.
Where we have to be more precise, the top news, so here the top news.
Needs to worry, needs to news manager.
And I found out where it is, because the thing is, like in our news app, we said the articles right,
with a mutable list of top news article, but then we are never actually collecting the top articles.
Directly and assigning them.
So here we are, getting the articles from our news response from the news manager, but what I actually
need to do is I need to create a new article on VB. here, so I'm going to call this one up articles
which will be using our view model thought new response dot.
And here we need to use something called collect as state.
Well, that will do is it will collect values from the state flow and represents its latest value via
state.
So we need to get the state from it.
So basically, we collect as state, then we get the value from that state and that value contains articles
and these articles.
We're going to set as top articles and then instead of loading the articles directly from our news manager,
we're going to get them now from our view model, which are stored inside of articles.
OK, so at this point, if we run our application again, we should see our top articles as well.
Yeah, there we are.
So we see the top articles and we see the categories you see here.
Well, now let me check.
So now we need to make a change to the, uh, the other articles as well.
So here.
We're adding all articles.
Then we.
Past the data to our NEF host.
And, well, it seems like I have to get rid of this top news article here and then let's see once again.
So we add all articles, which will be our top articles.
If there, if it's not them, that we're going to get them, we're going to just get a list of.
So let me see.
And then we go to categories and there we are two sources.
And there we are.
All right.
So our news app works now with our new.
Architecture, where we're using a view model, well, we still have our models and where we have our
views, which are our screens, components and so forth.
And by the way, I also realized one less thing that I would like to change in this video still, and
that is inside of our news app once we are in the bottom navigation composable here detail index.
So once we get into the detail page, we have this check.
Will we're checking the query right?
So we're going to take care of the query later on at this point.
We're not done with it yet, but in the alt's block, we are also adding all articles from our news
manager and we are running into problems here with our news manager because now we're using our view
model, so we should use our view model instead.
And this will then fix a bug where, well, if you don't do the view model there and you click on one
of the projects or one of the details here, you will not get the details screen.
It will crash.
OK, and that's fixing it.
So adding this article from the view model will fix it.
All right.
So that's it for this video.
See you in the next one.