Build a Real-Time Android WhatsApp Clone With Jetpack Compose

In social messaging applications, the reliable and robust chat feature is the essential part. In this article, you will learn how to build your own real-time Android WhatsApp project with Jetpack Compose and Stream’s versatile Compose Chat SDK.

Also, you will learn the overall architecture, each layer, and theming that are used in WhatsApp-Clone-Compose project.

Before you dive in, we recommend cloning the WhatsApp-Clone-Compose on your local device with the command below and opening the project with your Android Studio.

git clone https://github.com/GetStream/WhatsApp-clone-compose.git

If you build WhatsApp Clone Compose on your device, the result will look like this:

App Architecture

WhatsApp Clone Compose follows Google’s app architecture guide, which is the recommended architecture for building a robust, high-quality app. In this post, you’ll learn how the app architecture was designed and how data flows between different layers.

As you see in the figure below, each layer has dedicated components that are very loosely coupled to each other. The arrow means the component has a dependency on the target component along the direction

Fundamentally, the architecture is composed of two layers: UI layer and a Data layer.

Each layer has different responsibilities, as defined below:

  • Each layer follows unidirectional event/data flow; The UI layer emits user events to the data layer, and the data layer exposes data as a stream to other layers.
  • The data layer is designed to work independently from other layers and must be pure, which means it doesn’t have any dependencies on the other layers.

With this loosely coupled architecture, you can increase the reusability of components and scalability of your app.

Now let’s see how each layer works.

UI Layer

WhatsApp Clone Compose is built with 100% Jetpack Compose to draw UI elements, and it configures screens by observing UI states, which comes from the ViewModel that holds UI states and restores data when configuration changes.

The ViewModel transforms the business data from the data layer into UI states, and UI elements configure screens following the UI states for success, loading, or error.

UI states represent the business data or exception following the single source-of-truth principle, so you can focus on how to draw UI elements depending on the UI states in the UI layer.

Data Layer

The data layer consists of repositories, which are exposed to other layers as public interfaces. Repositories include business logic that is mostly run on the background thread such as querying data from local databases and fetching remote data from the network.

By exposing the interface of repositories, other layers can observe the business data as a stream such as Kotlin’s Flow.

Repositories have multiple data sources such as the local database and the network. To guarantee the single source-of-truth principle from the multiple resources, repositories are implemented as an offline-first source of business logic as the figure above.

Modularization

WhatsApp Clone Compose is built with multi-module strategies to improve app development.

  • Reusability: Modularizing reusable code properly enable opportunities for code sharing and restrict code access from other modules.
  • Parallel Building: Each module can be run in parallel and it reduces the build time.
  • Decentralize Team Focusing: Each developer team can assign their dedicated module and they can focus on their own modules.

Modularization allows you to build your feature and write test code independently without any dependencies on other features. As a result, you can achieve loose coupling between modules and high cohesion, which means each module has clearly defined responsibilities for its own domain logic.

Theming with Jetpack Compose

WhatsApp Clone Compose has theming components in the core-designsystem module. The module includes reusable components that are used in UI feature modules, and theme definitions, such as Theme, Backgroundand typography.

In this post, you’ll learn how WhatsApp Clone Compose styles the background and theme for overall UI elements.

Background

Styling background is a primary part of application design. To make UI elements have a consistent background color, WhatsApp Clone Compose implemented its own background composable, which is called WhatsAppCloneBackground:

https://medium.com/media/1264835d5eb6009f619e4f3283774603/href

The BackgroundTheme includes two properties below:

  • Color: This property decides the background color of the Surface.
  • Tonal Elevation: The surface tonal elevation highlights the components depending on the elevation levels. Surfaces at elevation levels +1 to +5 are tinted via color overlays based on the primary color, such as app bars or menus. For more information, check out Material Design 3 Color system.

By wrapping UI elements with WhatsAppCloneBackground composable like the example below, descendants of the composable content will have the same background color.

https://medium.com/media/4cc7181617d809f13d1052dc99cb5c61/href

Theme

Theming is one of the most essential parts of the app design, and you can achieve this easily in Jetpack Compose.

WhatsApp Clone Compose uses Material Theme for styling the overall UI elements, and defining custom color schemes with lightColorSceme and darkColorSceme methods:

https://medium.com/media/988e8e0f43bc30e096dcc77ae343af8e/href

Also, WhatsApp Clone Compose defines its own theme, WhatsAppCloneComposeTheme like the below:

https://medium.com/media/94e48983fb71fe40156d6e58ad0251e8/href

Color themes apply to different color schemes and background colors depending on whether the user device is in dark mode or not.

By wrapping your UI elements according with WhatsAppCloneComposeTheme like the example below, you can see the result of the UI elements being colored to the defined color scheme.

https://medium.com/media/defd9957bab955c166d0e3d0279bf9cb/href

If you need to apply different colors for specific UI elements, you can just pull out and use the defined color value from the color scheme:

https://medium.com/media/f9bc35ebf7ddd90b9a43f1f5a3ce10fc/href

Build the above on your Android Studio, you can see the defined theme applied to the UI elements as shown in the image below:

Now, let’s build a real-time WhatsApp chat feature!

Getting Started With the Stream Chat SDK

WhatsApp Clone Compose built the chat feature with Stream’s versatile Compose Chat SDK. Stream Chat SDK offers performant chat solutions that have been used by billions of global end-users across thousands of different apps.

You can sign up for a free Stream chat trial plan quickly with your GitHub account. Small teams and individuals can also apply for a Maker Account that allows you to use Startup Plan for free.

Once you have an account, create a project in your Stream Dashboard.

After you create your project, note your API Key.

Lastly, We will disable authentication for convenience, so toggle the Disable Auth Checks button and click the Submit button on your project’s Overview page. Note that this should only be done in development environments.

Gradle Setup

Before implementing any chat features, import the Stream SDK for Jetpack Compose into your project. Add the below dependencies for your module + app level build.gradle file:

https://medium.com/media/de78aa01cc0dcd3708699e0fc5866d20/href

Stream SDK is an open-source project, so you can see all source codes, commit histories, and releases on GitHub.

Note: If you’re completely new to Stream Chat Android, you can also take a look at the Compose Chat Tutorial.

Initialize ChatClient With App Startup

ChatClient is the main entry point for all low-level chat operations, such as connecting and disconnecting users to the Stream server or sending and reacting to messages.

WhatsApp Clone Compose initializes the ChatClient with App Startup and connects a user at once for convenience. Note that we used a development token to connect a user as authentication is disabled.

https://medium.com/media/0d59800d61fdb8f775f5887f7a8a1dfc/href

As we use an initializer, we need to define it inside your AndroidManifest.xml file, and it will be launched when your app started.

https://medium.com/media/1eff62aa1ab096df625b677d884e5ead/href

Now, let’s build a channel list screen.

Build a Channel List Screen

Stream SDK provides high-level UI components that allow you to build chat screens without much effort. You can build your channel list screen easily with the ChannelsScreen composable function:

https://medium.com/media/493cd6a07311bf885846c675b9df2ad3/href

As you see in the example above, you should wrap the ChannelsScreen with ChatTheme, which styles chat UI components, such as primary color, typography, and shapes.

Also, navigate your screen or launch activities to display the message list screen with the onItemClick lambda parameter when users click the channel item.

Build the above on your Android Studio, you will see the result below:

Build a Message List Screen

You can build the message list screen easily with the MessagesScreen composable function like the below:

https://medium.com/media/9f4dd6c9486d769cfbe43b44e06b80f2/href

Next, you should build a TopAppBar that contains channel information, such as an image and name. To get the channel information, we need to fetch the data from the network using ChatClient like the below:

https://medium.com/media/af70aab82f3813c09f273eb94f6e7162/href

You can inject the ChatClient into your ViewModel with a dependency injection tool like Hilt or you can just get the instance with the ChatClient.instance() method.

After fetching the channel information via a network call, the WhatsAppMessagesViewModel transforms the result into UI states that are defined like the below:

https://medium.com/media/0f51ce9cc9b282a75f4d954cea1827c2/href

WhatsApp Clone Compose models each user interaction as events to handle them in ViewModel.

https://medium.com/media/84cd5e6aa70f464f59cff4bcc5f0664c/href

So you only need to exposehandleEvents method to your UI elements and reduce the API surfaces like the example below:

https://medium.com/media/781af6f0ab273778e0ccefca1720a5eb/href

Finally, you can draw your composable functions depending on the UI states that comes from the ViewModel, such as success, loading, and error:

https://medium.com/media/f26af727a5cf127b13cca64a82b54434/href

Note: To load a network image in the TopAppBar, WhatsApp Clone Compose uses Landscapist, which allows you to fetch and display network images with Glide, Coiland Fresco.

You will now see the result below:

Wrapping Up

In this tutorial, you explored the overall architecture of the WhatsApp Clone Compose project and how to build chat features with Stream’s versatile Compose Chat SDK.

To learn more about the Compose Chat SDK, try the Compose Chat Tutorial or check out the open-source projects below on GitHub:

  • Slack Clone Android
  • Stream Draw Android
  • Facebook Messenger Clone

You can find the author of this article on Twitter @github_skydoves or GitHub if you have any questions or feedback. If you’d like to stay up to date with Stream, follow us on Twitter @getstream_io for more great technical content.

As always, happy coding!

— Jaewoong

Originally Posted at GetStream.io/blog.


Build a Real-Time Android WhatsApp Clone With Jetpack Compose was originally published in ProAndroidDev on Medium, where people are continuing the conversation by highlighting and responding to this story.

Leave a Comment