A5: Bananazon
I'm going bananas!!!
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)
modelcontains the data classes that will be used to represent the Product dataremotecontains the Product API Service interfacerepositorycontains theProductRepositorythat will be used to handle and expose Product data to the UI layer.
di package
Contains all code that deals with dependency injection
BananzonApplicationcontains the Hilt Application Class for the appNetworkModulecontains the providers for the various parts of the retrofit layer like the OkHttpClient
ui package
Contains all code in the UI Layer including ViewModels
componentscontains a couple Composables that will be used in theProductScreenHeadercontains the top part and search bar of the screenProductCardcontains the component that represents the Product data as a card
screenscontains theProductScreenthat displays the products as a list and uses a search bar to filter the resultsviewmodelscontains theProductViewModelthat handles the business logic ofProductScreen
Requirements:
This assignment is not split into features, but rather more so as steps towards implementing Retrofit correctly.
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.ktto 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.ktCreate the API Service instance in
NetworkModuleusing 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
getAllProductsmethod by using the api service and returning a Result<List<Product>>.Do the same for the
searchProductsmethod.
5) ProductViewModel.kt
Inject the repository into the ViewModel.
Replace Any with the actual Product data class you defined earlier in the
ProductUiState.Implement
loadProductswith 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
ProductsContentcomposable usingLazyColumnand theProductCardcomposable.Collect the uiState and searchQuery StateFlows from the ViewModel.
Pass the states and the relevant handlers into the
ProductsScreenContentcomposable which you should add to theProductsScreencomposable.
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...
When you're done, export your project through Android Studio and upload your ZIP to CMS! Congrats on finishing A5!

Last updated
Was this helpful?