Demo: Chatter
Also known as Y...
This demo has starter 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 this demo, we'll have you develop the networking logic for a basic social media app!
Let's walk through the code, shall we?
Starter Code
Expand each of the files below for a description of the starter code!
data package
Contains all code that deals with data directly (data layer)
modelcontains the data classes that will be used to represent the Post dataremotecontains the Post API Service interfacerepositorycontains thePostRepositorythat will be used to handle and expose Post data to the UI layer.
di package
Contains all code that deals with dependency injection
ChatterApplicationcontains 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 thePostScreenChatterHeadercontains the top part and search bar of the screenPostCardcontains the component that represents the Post data as a card
screenscontains thePostScreenthat displays the products as a list and uses a search bar to filter the resultsviewmodelscontains thePostViewModelthat handles the business logic ofPostScreen
Instructions:
2) Retrofit Instance
Create the Retrofit instance provider. For our base url, we will be using https://dummyjson.com/.
3) PostAPIService
We will be using the Get All Posts and Search Posts endpoints from here. Based on that information, do the following:
Create the relevant serializable data classes in
Post.ktto represent the JSON responses. To see the example JSON output, you can visit https://dummyjson.com/posts 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
PostApiService.ktCreate the API Service instance in
NetworkModuleusing the retrofit instance provider you made earlier and the interface you just defined.
4) PostRepository.kt
Inject the api service into the repository.
Implement the
getAllPostsmethod by using the api service and returning a Result<List<Post>>.Do the same for the
searchPostsmethod.
5) PostViewModel.kt
Inject the repository into the ViewModel.
Replace Any with the actual Post data class you defined earlier in the
PostUiState.Implement
loadPostswith 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
searchPosts.
6) PostScreen.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
PostContentcomposable usingLazyColumnand thePostCardcomposable.Collect the uiState and searchQuery StateFlows from the ViewModel.
Pass the states and the relevant handlers into the
PostScreenContentcomposable which you should add to thePostScreencomposable.
If you get any networking errors, make sure to debug with LogCat errors!
If you've solved the demo correctly, your solution should look something like this...
Last updated
Was this helpful?