Lecture 5 & HW 5: Persistent Storage, Networking, and JSON Parsing
Last updated
Last updated
Your goal for this week's assignment is to build off your HW3 weather app with actual weather data from an API. We will have two tabs, one for the screen with the temperature and weather-changing functionality, and one for the screen with actual weather data. You are going to use this API to retrieve randomly generated images: https://api-ninjas.com/api/weather
Notes on the API: We suggest you use the endpoint https://api-ninjas.com/api/weather?lat=<latitude>&lon=<longitude> to retrieve weather data as other endpoints require a premium membership. You should retrieve location data as you did in HW3 and use the latitude and longitude of the result in the above URL. For the API key please use: YQhN1nGNi9HGc7K79k6Sb07c2fdgI6nGJif8PyKN.
Your app should have two tabs, one for the Real Weather and one for the Adjustable Weather, accessible by bottom navigation
Before you start working on the new functionality, move your HW3 content from the main activity to the tab for Adjustable Weather. This is all you should do for this tab.
Requirements for Real Weather:
The real weather tab should have Text components that display “temperature”, “Feels like”, “Humidity”, “Minimum temperature”, “Maximum temperature”, “Sunrise”, and “Sunset.”
You should use Retrofit and Moshi to retrieve and parse the weather data into a class called Weather which should reflect the structure of the API endpoint.
You will need to use parameters in the network call. The following documentation shows how to do it in Java. You can change the syntax so it works for Kotlin. https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
Note that the sunrise and sunset times will be returned as Epoch times. You should convert this to a timestamp using Time(<sunrise>> * 1000)
There should be two buttons.
The first button should say “Request Location Permission” if permission isn’t granted. Clicking on this would launch a permission request. Otherwise, it should say “Update Weather,” and clicking on it would update the current data by retrieving it again from the endpoint.
When using fusedLocationProviderClient.lastLocation.addOnSuccessListener, you might encounter a warning suggesting that a permission check is required beforehand. This occurs because Android Studio doesn't recognize that permissions were already verified with Accompanist. Therefore, you can safely suppress this warning.
The second button should have the same functionality as the share button from HW3, but instead of sharing the weather condition and temperature, you should share the temperature and the temperature it feels like.