Welcome back.
And the last video we created our API and the news service and all of those classes and so forth.
Now we need to take care of the actual news managers.
So the thing is, it's quite a complex process that we have to take care of, right?
We need to create the classes first, like top news and top news article and so forth.
Then we need to create the retro fit interface with the different requests, for example, a get, request,
post, request, put, request and so forth, which is our interface new service.
Then we need to set up the API object with the URL and basically where we want to send all of this and
then we can use all of that in another.
File, which will be or another class, which will be called news manager.
OK, so this class will take care of putting everything together and here we will get the response,
so to speak.
So we need to separate the logic from the UI, and we prepared all of the logic now, now we can take
care of the UI.
So first of all, the private variable, which I'm going to call news response so underscore and use
response and underscore just means that this should only be usable from this class itself.
And this will be a mutable state of.
Like so state of our top news response.
OK, so top news response.
You might recall it's this class, so that's the top level class which contains all of the data.
So there's going to be our top news response, but we need to create an object of it.
So we need to add the brackets here.
And then I'm going to get my news response that I can then access from other classes as well and set
from there, which will be a state of top news response.
And whenever you do that, first of all, you need to import state here.
So let's import the state from composed runtime.
And then you need to use the composable where this is going to go.
So at composable get and here we need to use the remember key word again because we are going to remember,
don't use response.
And let's import remember as well.
So this basically just means, hey, we're getting the news report or we have this news response, if
we want to make it available to other classes, we need to make it a state and it will just update our
user interface based on the data.
So that's really the cool thing about all of the state stuff where the state allows us not to have to
let the user interface know manually that something has changed or the data has changed.
But the data itself, when it changes, will update the user interface, or it will let the user interface
know that it has to update.
So now that we have the news response.
Let's go ahead and get the articles.
So I'm going to create a function that will get my get me the articles articles like so.
So here we will need to use our service that we have set up.
So service, which will be API Dot Retrofit Service API, is this class that we created.
So now we're using this retrofit service, which is our new service, which we'll just do this get method.
And here we can now get top articles.
And it's calling this method the setup article's method.
So now we need to pass in the country as well as the API key.
All right, so let's do that.
Let's get the country, which will be, for example, the US.
And let's get the key and therefore you need to go over to your registration and take this API key.
Now you could, of course, obviously also store it somewhere in a variable that you use in the singleton.
So it's going to be accessible from everywhere.
That would be fine as well.
So here we could actually create a private vol concert or concert vol, which would be the API key.
And let's call this one API key API key.
This should also be constable.
OK, so now let's use this API key here.
So this is inside of API.
API that API.
On the score key.
And I see that I shouldn't make it private and it should make it public.
OK, now I can't use the API key.
All right.
So this will give me the service and now I have to and queue to service so and queue, which will asynchronously
sent the request and notify callback of its response or if an error occurred.
So in queue, which just means we're not going to do it instantly.
But as soon as our application is ready and it's going to do it asynchronously and it's just going to
give us the callback or the the response, so to speak, once it's ready.
So when you're passing the callback, which we can do like, so we're just creating a new callback of
type top news response.
So it will give us this callback.
We'll return a top news response.
And then whenever you have a callback, you need to have the two different response types.
So you need to have the override fun on response, which will require a call of top news response in
our case.
So we need to import call.
And that retrofit version here we see fit.
We used to write colbeck as well.
So here, callback and used the wrong callback.
That's the problem, so let's import the retrofit callback.
And actually, once that is done, let me get rid of this and hover over it, and it will tell me to
implement members.
So this will make it a lot easier.
I will not have to manually type all of that code in.
There are two different callback responses that you will get, so you get the response and the on failure.
So we're just going to say, OK, if we get a positive response here, we can use the data and do something
with it.
And if we get a failure, then we can just print it to the screen, saying, Hey, there was an error
or something like that.
So what I'm going to do in the error case is the first thing.
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
throw bill, which will give me the actual error.
OK, so t the prince deck tricks.
So now what do we want to do with the response?
If it worked or if we got the articles?
Well, for now, I'm just going to check that the response was successful so we can get a response that
says it's successful.
We can get a response that it says we can get different kinds of responses, right?
So I'm going to check if is successful, which is a Boolean, so it can be the true or false.
And if that's the case, then I'm going to use my news response and set the value of the news response
to the be the body of the response.
So this will just give me the content of the response itself.
But the body is another bill, so we need to force unwrap it.
But we will only force on it because we know that the response was successful.
So there will be a body, which is why it's fine to voice and rep it.
Of course, you could also surround that within if, let's say, statement.
So then if the response was not successful, we're just going to log the error.
Like so.
But then if it was successful, I would also like to see the response.
So I would like to see what was the response body so we can just log the news as news response, dog
value.
So now whenever we get the news, we will displayed in our lock at for now.
Later on, we're going to change that and displayed in our user interface.
Now we have this beautiful method that will get us all of the articles, but we're never using it,
so the rest is in it.
Method the initialize the method, which will be call once the news manager will be initialized.
So once an object of it will be created, that's where we will use to get articles method.
OK, so now that we have that, let's initialize our news manager in the navigation composable so every
composable can have access to it without it being initialized twice.
All right.
So now let's initialize our news manager in the navigation composable inside of our news app.
So our news app has this navigation composable that we had created with the nav controller and the scroll
state.
But I'm going to add the news manager here as well.
So we need to pass and use manager object whenever we want to use our navigation in the future.
So how do we use our navigation in here?
So here I'm just going to create an article's object which will just use my news by nature news responds
the value, the articles.
And now you might wonder what the hell is going on here, Dennis.
This is like way over my head.
Let me go over it in a second.
So I just want to display the articles here as well.
So let me add the log in here that's imported.
So what's going on is we know that the news response will be of Typekit, while a state news, top news
response and top news response has this variable called articles and the articles is what we're getting.
So here, which is saying news response has spelled value, which will be fully added here as the value.
Which will be the body of the response, so the response that we saw earlier here, let me show you
once again.
So this is the response rate.
Whatever we.
Call this is the example response that we might get.
So we're just saying, give me that response and the body of it, so the code of it that the JASON code.
And now we've taken this response and this response has articles in it.
And I want to have those articles.
OK, so now if we look at this, our navigation here at the top was saying that we want to have a news
manager.
But what I'm going to do is I'm just going to create a news manager object straight away so that I have
a default value, which now means that this error that I had here at the top where I had to set the
news manager will go away because if I don't set that, you will see that it will ask me for a news
manager at the top there.
But you know what?
I'm just going to create an object of the news managed directly in the navigation method declaration.
So now if we try to run our application.
And actually, I have an emulator open already, so I.
So now, if we run our application, we get an error.
So the problem is, and we get, uh, big error here.
Well, it's just jumping on me.
Look here.
There we are.
So it's trying to access the internet or application, but we don't have the rights to use the internet
yet.
So we need to make sure that we have the rights for it.
Therefore, we need to go over to our manifest.
And ad usage rights.
So how to do that is the following.
So you need to use this user's permission keyword and then you search for internet here and use this
internet permission.
Like so and to close it with those brackets here.
So what this will give us is.
Well, this will tell the application where Android specifically, that this application needs to use
the internet.
And if we run it again.
Now our application will still crash and we get a different detail, so here we say it has a problem
with the retrofit converter, so there is something wrong with the type of data that we're giving our
application.
So you can find it up here unable to create converter for class you tutorials and use a use response
so it doesn't know how to convert the top news response.
So in order to fix that, we will need something called Moshi.
So there is this Moshi parser that converts JSON into something that is readable for all our applications.
So let's go over to our Gradle file.
Here and add to dependencies, so one for Moshi Scotland, which is this one here and one that converts.
Moshi for Retrofit two.
So once we have added those two in there, we can now go ahead and initialize Moshi and our API here
and pass it to it as well.
So let's go ahead and create a Moshi object.
So here private while Moshi, which will be using the Moshi builder and let's import Moshi for that.
And then we're going to add the Kotlin JSON adapter factory as well as build it.
All right.
So this will just convert JSON into something readable for our Scotland code for our project, and then
we can use that to add in here as well.
So here we add a converter factory, which is going to be our Moshi converter factory.
So Moshi converter factory thought create.
And factories, but factory that create with our Moshi object.
So we need to import that as well.
Like so and now our application should be willing to execute and run and load the news.
So let's see if it's still going to crash, but it shouldn't at this point.
All right.
So now if you test it and then I had the weirdest book.
So for some reason, my emulator doesn't update the time.
So in your case, make sure that the date is really correct so that the date works for whatever date
it is right now or whatever time it is right now for you.
So even though here I have date and post there already.
So system date here I said it automatically.
But then you see just sits to is super weird time, which is not correct.
So I had to set it manually do whatever time it is right now.
So it's like twelve fifty two p.m. And then it works because the problem was that if the time on your
phone is different to the time of the response and like the the data that you get from the news API,
then this can cause problems with your application.
So, yeah, basically, I ran the app after that because the code is fine, right?
And and then, yeah, then we get the response.
So here you see the response that we get.
So this is where it starts.
Missy, so top news article, this is where the object starts with the source, which is Ida Reuters
and the name of it, this Reuters and we have the author, which is not given.
We have the title, which is Manhattan D.A., will move to exonerate two men convicted of killing Malcolm
X.
Then the description is two men, blah blah blah.
Then we have the image for it, like the.
While the image JPEG, this one here, for example, so this is the image that is given as well as the
published date.
So you see when it was published and the actual content.
So this is the bigger content.
So yeah, that's pretty much it.
So now we get the data and we just need to display the data because it's really like now just setting
up what we have here as the data values.
Like setting this value to be invisible in our application.
So, yeah, that's what we're going to do in the next video.
So thanks a lot for following.