Show user progress with a Lottie animation

Follow this recipe to display the user’s progress as a Lottie animation — for example, a ring that fills up to indicate their daily streak. Your app renders the animation as a native Lottie view in a custom element.

Before you start

  • Update your app to Adapty React Native SDK 4.4 or later. Earlier SDK versions show the fallback content for every custom element.
  • Set up your app to fetch and display flows.
  • Set up the native code of your app for custom elements, as in the React Native guide.
  • On iOS, add Lottie to your app target in Xcode with Swift Package Manager, from https://github.com/airbnb/lottie-spm.git. If your app uses lottie-react-native, skip this step: that package already adds Lottie to your app.
  • On Android, set up Jetpack Compose in your app module. The animation view in this recipe is a composable. Add Compose Foundation and Lottie for Jetpack Compose to your app with Gradle, from androidx.compose.foundation:foundation and com.airbnb.android:lottie-compose.

Prepare a Lottie animation in .json format, or download the sample streak_ring.json. The app plays the animation from the start and stops it in proportion to the progress, such as halfway through for 50%. To show the exact progress, the animation needs:

  • No progress on its first frame, and full progress on its last frame.
  • A steady speed, with no easing. If the animation starts slowly and then speeds up, it shows less than half of the progress halfway through.

1. Add the custom element to the screen

In the Flow & Paywall Builder, add a custom element to a flow screen, and set it up for the animation:

  • Type: lottie_animation.
  • ID: streak_ring. In this recipe, the ID is also the name of the animation file.
  • Height: Keep the default Fixed height. The animation scales to fit the custom element.

Inside the custom element, add an Image with a still picture of the ring. Users see it as the fallback when their version of your app doesn’t play the animation.

2. Add the animation file to your app

Your native code reads the animation file from your iOS and Android projects, not from the assets of your JavaScript code:

  • iOS: Add the file to your app target in Xcode.
  • Android: Put the file in android/app/src/main/assets, the assets folder of your Android app module. Keep it at the root of this folder, not in a subfolder.

The file name without .json must match the ID of the custom element, such as streak_ring.json for streak_ring.

3. Create the animation view

Create a model for the streak, and a view that plays the animation. Your app updates the model when the streak changes. The view reads the model, so the ring shows the current streak when the screen opens:

4. Match the animation to the custom element

In your app-wide resolver, add a property for the streak model. Then return the animation view for the lottie_animation Type and the streak_ring ID. The highlighted lines show what to add or change. If your app has no app-wide resolver yet, copy the whole code:

Create the resolver with the streak model, and set it as the app-wide resolver at app launch:

5. Send the streak from your JavaScript code

Your JavaScript code keeps the streak, and the native views read it from the streak model. To update the model, send the streak to your native code through a Turbo Native Module. The module has a JavaScript spec and an implementation for each platform.

In a specs folder at the root of your project, create the spec of the module:

import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
  setStreak(days: number): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('NativeStreakStore');

To generate the native code of the spec, add codegenConfig to package.json. Replace com.example.app with the package name of your Android app, in package.json and in the Kotlin implementation of the module. If package.json already has codegenConfig, only add NativeStreakStore to the modulesProvider object:

"codegenConfig": {
  "name": "AppSpecs",
  "type": "modules",
  "jsSrcsDir": "specs",
  "android": {
    "javaPackageName": "com.example.app.specs"
  },
  "ios": {
    "modulesProvider": {
      "NativeStreakStore": "RCTNativeStreakStore"
    }
  }
}

Implement the module for each platform:

In your JavaScript code, send the streak at app launch, and each time the streak changes:

import NativeStreakStore from './specs/NativeStreakStore';

// call at app launch, and each time the streak changes
export function sendStreak(days: number) {
  NativeStreakStore.setStreak(days);
}

6. Test on a device

The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. To preview the animation:

  1. Publish the flow. To see the latest published version instead of a cached one, register the device as a test device.
  2. Run a build of your app that includes the resolver. Set the streak to 0 days, open the screen, and verify that the ring stays empty. Repeat with 4 and 7 days, and verify that the ring fills up to each value.

To test the fallback, remove the animation file from the build, and open the screen again.

To find out why the fallback appears instead of the animation, follow the instructions in the Test on a device section of the React Native guide.