This assignment has a release code! Please download it below, extract it, and open it in Android Studio. Ensure it can compile and run fine on your emulator before starting this assignment.
For our fifth assignment, we'll have you develop the networking logic for a basic e-commerce app!
We've talked about money and happiness, and now, let us ponder — can money buy happiness? Perhaps not, but money sure can buy a lot of things — let's dive into the depths of materialism...
Let's walk through the code, shall we?
Release Code
Expand each of the files below for a description of the release code!
data package
Contains all code that deals with data directly (data layer)
model contains the data classes that will be used to represent the Product data
remote contains the Product API Service interface
repository contains the ProductRepository that will be used to handle and expose Product data to the UI layer.
di package
Contains all code that deals with dependency injection
BananzonApplication contains the Hilt Application Class for the app
NetworkModule contains the providers for the various parts of the retrofit layer like the OkHttpClient
ui package
Contains all code in the UI Layer including ViewModels
components contains a couple Composables that will be used in the ProductScreen
Header contains the top part and search bar of the screen
ProductCard contains the component that represents the Product data as a card
screens contains the ProductScreen that displays the products as a list and uses a search bar to filter the results
viewmodels contains the ProductViewModel that handles the business logic of ProductScreen
Requirements:
This assignment is not split into features, but rather more so as steps towards implementing Retrofit correctly.
1) OkHttpClient
Create the OkHttpClient provider in NetworkModule
2) Retrofit Instance
Create the Retrofit instance provider. For our base url, we will be using https://dummyjson.com/.
3) ProductAPIService
We will be using the Get All Products and Search Products endpoints from here. Based on that information, do the following:
Create the relevant serializable data classes in Product.kt to represent the JSON responses. To see the example JSON output, you can visit https://dummyjson.com/products for example.
It might be helpful to have AI do the heavy lifting for creating the data classes with serial name.
Define the methods that represent the API endpoints we are using with the correct Retrofit annotations in ProductApiService.kt
Create the API Service instance in NetworkModule using the retrofit instance provider you made earlier and the interface you just defined.
4) ProductRepository.kt
Inject the api service into the repository.
Implement the getAllProducts method by using the api service and returning a Result<List<Product>>.
Do the same for the searchProducts method.
5) ProductViewModel.kt
Inject the repository into the ViewModel.
Replace Any with the actual Product data class you defined earlier in the ProductUiState.
Implement loadProducts with the repository functions. Remember to handle the loading and error states accordingly when the function is called, on success of the repository function result, and on failure of the repository function result.
Do the same for searchProducts.
6) ProductScreen.kt
Alright, time to tie it all together! In this part, you'll connect your screen to the viewmodel and test the final networking logic to get your app actually showing the proper response from the backend.
Display the list of products in the ProductsContent composable using LazyColumn and the ProductCard composable.
Collect the uiState and searchQuery StateFlows from the ViewModel.
Pass the states and the relevant handlers into the ProductsScreenContent composable which you should add to the ProductsScreen composable.
If you get any networking errors, make sure to debug with LogCat errors!
If you've solved A5 correctly, your solution should look something like this...