All right, so in the previous section, we started improving the structure of the project and worked
on top new screen and two categories.
Now let's look at the source's screen.
So the screen here in particular.
And after that, we need to take care of our search screen as well.
So therefore, the first step will be to modify the update request methods in our new service cut file.
So let's go over to news service.
Katie.
So that's our interface, right?
This one here and we were using suspend functions in the last video.
So we changed those functions to be suspend functions.
Now we need to do the same thing for our other functions as well.
So here for resources, for example, as well as for the search query and so forth.
So let's just do that.
Let's go and call this one to suspend function, get articles by sources.
And here we need to still pass in the source.
We're going to still get a top response, but our top news response, but this will not be a call.
But we're just going to do like so.
The same goes for this one.
So we're not going to return a call because now we're going to use to suspend functions here, which
will be called routine functions.
And they, well, they will directly return a top news response as we want it.
Instead of using a call here.
And then we need to go to our news manager.
All right, so here, news manager, that's where we also had those functions get article by source.
For example, you see now it's not happy with it anymore because, well, we changed how it's going
to work.
So we're not going to use retrofit here anymore.
So this one will now be also is suspend function.
So get article by source and.
It will return a top news source now, so here.
Top news of news response, that's what it was.
So I'm going to basically do the same thing as we've done before here with context.
The dispatcher I owe and going to simplify this significantly.
So we don't need to do all of this on failure and queue on response and so forth.
We don't need to do any of it.
So now instead, we're going to call the get.
And she was articles by source says kind, and here, instead of passing the category itself.
We can get the source.
Name that value.
OK, so this source name, this variable here that we have in our news manager, which is a mutable
state which knows which.
New source.
We want to use OK, and this will be to get articles by source.
Now let's get searched articles as well, and therefore we need to make it to suspend function as well.
And let me actually do the same thing as we've done here.
It will return a top news response based on.
And here we need to make a couple of changes.
So this will be state still a query with context dispatchers.
I'll.
Like we did and then.
It will call them get searched articles or get articles.
I think I call it.
All right.
And here we're not going to search by source name, but here we need to search by whatever we entered.
And we get that from the query.
So whatever we entered in query, which is this query here, so we can just pass the query to get articles
because if you recall, query just needed a string, which will be our query.
So our search keywords, so to speak.
So we're just saying run it on the dispatcher's I own basically on this thread, and once it's done
doing this stuff, give me the return, which will be a top news response.
OK, so you were just saying in which threat?
We want to run it and this is what we want them to run.
OK.
And now actually, we need to make sure that in our repository Katy file, we prepared these methods
for access to the view model.
So he and repository Katie, we get articles by category.
We get articles like So.
So here we need to get articles by source as well as get articles by search.
So these will also be suspend functions here, suspend fun, get articles by source, and we need to
pass the source, which will be a string.
And here it will use our manager to get articles by source where we set the source to be the source.
So we set the source of this get articles by source to the source that we are passing to discard articles
by source here.
OK, so this will be an equal sign here.
And let me put that into the next line so it will be working flawlessly and see how that I call it actually
get article by source there.
I realize that we probably should also use the source here.
So let's just set the source as string and use that source then and not the source name value.
All right.
So get articles by source.
And now in our repository.
So over here, we can now set the source.
OK, so this will be the source that we passed to this good article by source, but we then can pass.
OK, and then at the same time, we can also set up our search function for of our search feature,
so to speak.
And I'm just going to call this get searched articles, and it will need to query which will be a string,
and it will pass that query to be the query.
And we're here going to get the searched articles, obviously.
So we're going to call this method here from our news manager in our repository.
We're preparing it, so to speak, for our view model via our repository.
OK, now we can go over to our main view model.
Here, and we need to prepare the holder for the source name with setters and getters.
So we had that four hour news response.
We had that for our articles by category selected articles and so forth.
Now we need to do the same thing with those letters and stuff for our source name.
So here, Val source name.
Will be a mutable state flow, which I'm going to set by default to and gadget, which is one source.
So there is the source code and gadget.
And then I'm going to have a private vol get article by source here, which will also be mutable state
flow, where I'm going to pass a top news response item or object, and then I'll get articles by source.
So article by source, which will return, aced or will be of typed state flow.
And specifically of type top news response, and we need to pass a or have a get here as well, which
will be on the score, get article by source and I have a typo here, so let me get rid of that typo,
then it should look a little better.
So now we have the source name and then we have the article by source variables.
Now we need to do the same thing for our query, so for the search feature, right?
So we have this we're setting up the source and then at the same time, we're setting up the search
feature here at the top.
So this is part of the source stuff.
And now we need to do the same thing for our search feature.
So here we need to have a query, which will be also a mutable state flow, and I'm going to set the
default value to be empty.
So the search will be empty by default.
And then we also need to have these getters and setters in the same fashion.
We just need to change the name.
So I'm going to call this one searched news response, and this one will be searching used response
without an underscore.
And we're going to get it with first searched news response so we could call it get search news response
to be more precise here.
And while the general idea will still be the same.
All right, now, we need to create a method to launch, get articles by source.
This one here and then set the response to its center.
So to a visitor here.
OK, so also in our main view model, let's go and create a function here.
We see we have created a on selected category or changed function before, but now we need and get article
by source or get articles by source, something like this.
And then first of all, I'm going to set the highest loading to true again or to value the true, to
be precise.
So this will make sure that our educator.
So our progress indicator will be displayed.
And then we set up the view model scope to launch.
So this will be the core routine where we just say, OK, well, want to run this on the dispatcher's?
I o this, but sure start i o thread.
And what do we want to launch?
Well, our get article by source.
The value going to set that to repository thought good articles by source.
And here we need to pass to source, which we get from our source name, but value.
So.
There's variable up here.
Which is going to be this miserable state flow.
So by default, it will be Engadget and then we are going to set it.
And obviously, we also need to make sure that we are overriding this value to false because then we
are done with the loading of the source or the article by source.
So now.
We also need to set up the Get Searched Articles fund, get searched articles.
Which I'm going to get by a query, which will be of type string.
And then we also need to do the same thing.
So we're going to set the value to wait and hear instead of setting the article by source, we're going
to set the search and reduce response to call our get searched articles with the query.
So not with the value here, but with the query that we're passing to the function.
So whenever we call this function, we're going to pass the query into it and it's going to take care
of all of this background stuff, like taking care of the call routine here, and it's going to set
the value accordingly.
And in between, while this thing happens because this is going to take a while, which can be half
a second or so, it's going to display the.
Progress dialogue again.
All right, now, we can go over to our sources, and if we look at our sources class here at the top
we were using.
While we're in the bottom menu screen, I wanted to go to the sources close, so sources Katie.
Here you will find that we were using a news manager, but now we want to always use our new model instead,
right?
So let's go ahead and use our view model instead of our news manager in here or pass it accordingly.
Obviously, we will need to make a couple of changes here, so we need to now not use the news manager
anymore, but our view model here.
So if you model that source name and we're going to collect the state, so because we cannot just use
the value we need to collect as a state and then we can get the value from it.
And we're going to set that as the source.
OK, and then accordingly, further down, we need to make sure that we are not using our new manager
name value here, but our view model, that source name value.
And then in our items here in the careful missy.
So here when we're setting the title, yeah, we did that.
OK, that's good.
Then we need to go further down here in our sources, and that's in the scaffold still.
But here in the IT padding values.
So when we're getting the padding values here, we're going to use our view model instead.
So here, view model, don't get articles by source and I think a called it article by source.
And then the articles are going to come from the view model as well.
So you see, we're really replacing our manager or our what was it news manager all the time with our
new view model here.
But again, we're collecting it as a state.
So here collect as state, that value is what we need to do.
OK, and in the search bar itself.
So search bar and the search bar.
Katy File, that should be it here.
We also need to make a change.
So our news manager that we used before will need to be replaced with a view model again.
So you model and once we do that, we should get a couple of arrows here.
So one this year at the bottom where we are checking the query and then we're calling the news managers
get searched articles.
Instead, we're going to use to view models, get search articles and view model that gets searched
articles and at the bottom here in our.
Point while here we just need to pass our review model object, we could just pass an empty view model
object and import its.
For now, in our top news file, we get an error, right, because here when we're using the search
bar, we're passing a news manager, right?
But instead, what we need to do is we need to pass our view model here as well.
So instead of using the use manager, we're going to entirely get rid of it and replace it with our
main view model, main or view model as I'm going to call it, and pass that instead.
So now let's pass that to our.
Search bar, and we couldn't be more specific and say the view model should be the view model if you
want to really make sure that you are using the named parameters correctly.
And then if search text here is not empty, then we want to use our view model that search news, response
value and the value itself is going to get what we're going to get that through collecting it as a state
again.
So collect as a state which collects the values from the state flow and represents its value by US state.
Okay.
So before it was a state, but we are using a flow state, right?
So we need to collect the flows state as a state.
So that's why we're using this collect as a state, which we have done before, right?
So that will be our top news.
Now we need to go over to our news app.
And then we should see some errors as well.
So here at the very bottom, you will find some errors because our composable thought that the top news
clause would get a news manager and we overrode that right.
So here we need to use our view model instead.
So you model will be of type main view model.
And actually, let me see the graph builder already uses that, so let's not screw you, create a new
view model, but actually passed the one that we are getting.
All right, that should be fine now.
So we're passing the new model that we're passing to the bottom navigation to be used for our top news
screen, as well as for our source of screen.
So here, let's not pass a.
Thingy, a news manager, but the view model, because if we look at our sources, you see it needs
a view model now.
All right, and now we need to go over to our news app and make a couple of changes here, so the first
one will be in our index, let here.
I'm still checking if news a query value.
So here what we need to do is we need to get the value from the query state.
So here query state.
So what will this query state be?
So the query state will be a variable that I want to use, and this variable will be existing inside
of our navigation, specifically in the nav hose to me, see, instead of this nav host here.
So I'm going to create this.
Think it should be in the curly brackets?
Yes.
So up here before I used in button navigation, I want to create a variable that will hold the query
state.
So the query state will just be the state of whatever is the current query.
So whatever we're currently searching for and this will be a mutable state off and it's going to get
it from our view model.
So the default value will be you model that query the value.
Came at to import midtable, stayed off here for this to work, so now it can use to query state here
and we're going to get the career state value and we're going to check if it's not empty.
So this is one way of checking if it's not empty.
The other way would be to check if it's not an empty string.
So I would like to check if it's not an empty string because it could be empty, but this could not
be empty, but still an empty string or so.
So I'm really making sure that it's not just an empty string.
And if that's the case, then we're clearing the articles and then here, instead of using the news
manager that search and use response, we need to use our new model here as well.
All right.
So at this point?
Let me see here at the bottom in top news, I also need to make sure that I am passing the view model.
Yes, I think we set that up, right?
So here we're passing the wall here.
I don't want to pass in the news.
Manage a query.
But the query, which I'm going to set up inside of my bottom navigation, so we're not using the news
manager anymore, is you see instead what I want to use is going to be the query, which will be a mutable
state.
Of type string.
OK, and we need to import mutable state here and use that query now for our top news, because if we
look at our top news, it needs a query of two type mutable state that can then use to set that as the
search text and check the search text and add all of the searches that we have.
Now let me see here in my news app.
I need you obviously need to make a change to this news manager because I'm not using the news manager
at this point anymore, but instead I want to pass the query here.
So the query will be our query state that we just set up here.
So this query state and I'm passing it in here and then I can use it and it will be passed down to our
composable where they need it.
And here for our bottom navigation or didn't have graph builder.
And then one final thing that I only realized after testing it is that in some of our sources, we are
setting the view model source name value, but we are not getting the articles by source.
So here what I should do is I should use view model dot, get article by source and then I should be
good.
So let's run our application and see if it's going to work so that we are.
Let's click on this dog.
OK?
That seems to work.
Let's click on.
This one seems to work.
So let's search for what this one looks a little more positive.
So let's search for Apple.
So let's look at Apple News specifically.
I'm going to search for Apple and then Apple quietly.
This continues.
All right, then.
Pentagon set up a year office.
That's nice.
Apple, fixed mobile and so forth.
So now we have a bunch of Apple news.
Let's look at the sources.
So weird and gadget.
Now let's change that to talkSPORT Sport, and you could see it displays the different news.
Politico doesn't seem to have very nice news.
Business Insider source still works flawlessly.
And then a problem that we have with our categories.
So if we look at our categories, they're not changing, right?
And that has to do with us just using state and nuts or a state flow and not using the state.
So here in sort of our categories, when we have a category tab selected, we are using our View Model
Select category.
But here we are, just calling the value or getting the value without before that collecting it as a
state.
So now we need to do that here, collecting that as a state as well as the article content itself here.
We also need to collect it as state.
All right.
And then check if we need to collect it a state anywhere else.
Actually, I think that should be that should be good.
So let's test this again and now let's see if our categories are going to work as intended as well.
So we're going to go over to categories.
And yes, it seems like it's working.
All right.
So, you know, our application is back at working flawlessly.