# Demo: Chatter

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.

{% file src="<https://195521982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LvOeSx5ZqjJA8sxykMu%2Fuploads%2FqfmBNCq7rOrm8MHSIWeL%2Fdemo5Starter.zip?alt=media&token=e1507601-68ef-4cc3-886c-f2e3a1789a91>" %}
Demo 5 Release Code!
{% endfile %}

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!

<details>

<summary><code>data</code> package</summary>

Contains all code that deals with data directly (data layer)

* `model` contains the data classes that will be used to represent the Post data
* `remote` contains the Post API Service interface
* `repository` contains the `PostRepository` that will be used to handle and expose Post data to the UI layer.

</details>

<details>

<summary><code>di</code> package</summary>

Contains all code that deals with dependency injection

* `ChatterApplication` contains the Hilt Application Class for the app
* `NetworkModule` contains the providers for the various parts of the retrofit layer like the OkHttpClient

</details>

<details>

<summary><code>ui</code> package</summary>

Contains all code in the UI Layer including ViewModels

* `components` contains a couple Composables that will be used in the `PostScreen`
  * `ChatterHeader` contains the top part and search bar of the screen
  * `PostCard` contains the component that represents the Post data as a card
* `screens` contains the `PostScreen` that displays the products as a list and uses a search bar to filter the results
* `viewmodels` contains the `PostViewModel` that handles the business logic of `PostScreen`

</details>

***

## Instructions:

<details>

<summary>1) OkHttpClient</summary>

Create the OkHttpClient provider in `NetworkModule`

</details>

<details>

<summary>2) Retrofit Instance</summary>

Create the Retrofit instance provider. For our base url, we will be using <https://dummyjson.com/>.   &#x20;

</details>

<details>

<summary>3) PostAPIService</summary>

We will be using the Get All Posts and Search Posts endpoints from [here](https://dummyjson.com/docs/posts#posts-all). Based on that information, do the following:

1. Create the relevant serializable data classes in `Post.kt` to represent the JSON responses. To see the example JSON output, you can visit <https://dummyjson.com/posts> for example.&#x20;
   1. It might be helpful to have AI do the heavy lifting for creating the data classes with serial name.
2. Define the methods that represent the API endpoints we are using with the correct Retrofit annotations in `PostApiService.kt`
3. Create the API Service instance in `NetworkModule` using the retrofit instance provider you made earlier and the interface you just defined.

</details>

<details>

<summary>4) PostRepository.kt</summary>

1. Inject the api service into the repository.
2. Implement the `getAllPosts` method by using the api service and returning a Result\<List\<Post>>.
3. Do the same for the `searchPosts` method.

</details>

<details>

<summary>5) PostViewModel.kt</summary>

1. Inject the repository into the ViewModel.
2. Replace Any with the actual Post data class you defined earlier in the `PostUiState`.
3. Implement `loadPosts` 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.
4. Do the same for `searchPosts`.

</details>

<details>

<summary>6) PostScreen.kt</summary>

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.

1. Display the list of products in the `PostContent` composable using `LazyColumn` and the `PostCard` composable.
2. Collect the uiState and searchQuery StateFlows from the ViewModel.&#x20;
3. Pass the states and the relevant handlers into the `PostScreenContent` composable which you should add to the `PostScreen` composable.

If you get any networking errors, make sure to debug with LogCat errors!

</details>

If you've solved the demo correctly, your solution should look something like this...

{% file src="<https://195521982-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LvOeSx5ZqjJA8sxykMu%2Fuploads%2FiMjecKCnvYPYCfd115lT%2FScreenshot%202025-11-12%20at%208.25.13%E2%80%AFPM.png?alt=media&token=0686c43b-5a2c-4afc-87e1-0fded480bb8f>" %}
