// the streak that your app keeps up to dateclass StreakStore { val currentStreak = MutableStateFlow(0) // days in a row}@Composablefun StreakRing(animationFile: String, streakStore: StreakStore) { val composition by rememberLottieComposition(LottieCompositionSpec.Asset(animationFile)) val streak by streakStore.currentStreak.collectAsState() // 7 days fill the ring: from 0 for an empty ring to 1 for a full one val progress = minOf(streak / 7f, 1f) LottieAnimation( composition = composition, modifier = Modifier.fillMaxSize(), // scale the animation to fit the custom element clipSpec = LottieClipSpec.Progress(0f, progress), )}
// the streak that your app keeps up to datepublic class StreakStore { public final MutableLiveData<Integer> currentStreak = new MutableLiveData<>(0); // days in a row}public class StreakRingView extends LottieAnimationView { private final StreakStore streakStore; private final Observer<Integer> streakObserver = streak -> { // 7 days fill the ring: from 0 for an empty ring to 1 for a full one float progress = Math.min(streak / 7f, 1f); setMinAndMaxProgress(0f, progress); playAnimation(); }; public StreakRingView(Context context, String animationFile, StreakStore streakStore) { super(context); this.streakStore = streakStore; setAnimation(animationFile); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); streakStore.currentStreak.observeForever(streakObserver); } @Override protected void onDetachedFromWindow() { streakStore.currentStreak.removeObserver(streakObserver); super.onDetachedFromWindow(); }}
class CustomElementsResolver( context: Context, private val streakStore: StreakStore,) : AdaptyCustomElementsResolver { // the animation files in the assets folder, listed once private val animationFiles: Set<String> = try { context.assets.list("").orEmpty().toSet() } catch (e: IOException) { emptySet() // no list of files: every Lottie element shows the fallback } override fun resolve(type: String, id: String): AdaptyCustomElementContent? = when (type) { "lottie_animation" -> { // the ID names the animation file, such as streak_ring.json val file = "$id.json" when { file !in animationFiles -> null // no file for this ID: show the fallback id == "streak_ring" -> AdaptyCustomElementContent.compose { StreakRing(file, streakStore) } // the other animations of your resolver else -> null } } // the other Types of your resolver else -> null }}
public class CustomElementsResolver implements AdaptyCustomElementsResolver { private final StreakStore streakStore; // the animation files in the assets folder, listed once private final Set<String> animationFiles = new HashSet<>(); public CustomElementsResolver(Context context, StreakStore streakStore) { this.streakStore = streakStore; try { String[] files = context.getAssets().list(""); if (files != null) { animationFiles.addAll(Arrays.asList(files)); } } catch (IOException e) { // no list of files: every Lottie element shows the fallback } } @Override public AdaptyCustomElementContent resolve(String type, String id) { if (type.equals("lottie_animation")) { // the ID names the animation file, such as streak_ring.json String file = id + ".json"; if (!animationFiles.contains(file)) { return null; // no file for this ID: show the fallback } if (id.equals("streak_ring")) { return AdaptyCustomElementContent.view( (context, elementContext) -> new StreakRingView(context, file, streakStore) ); } // the other animations of your resolver return null; } // the other Types of your resolver return null; }}
class App : Application() { // the streak model that your app updates val streakStore = StreakStore() // keep this instance: a resolver for one flow can reuse it // lazy: the context isn't attached yet while App is constructed val customElementsResolver by lazy { CustomElementsResolver(this, streakStore) } override fun onCreate() { super.onCreate() AdaptyUI.setCustomElementsResolver(customElementsResolver) }}
public class App extends Application { // the streak model that your app updates private final StreakStore streakStore = new StreakStore(); // keep this instance: a resolver for one flow can reuse it private CustomElementsResolver customElementsResolver; @Override public void onCreate() { super.onCreate(); customElementsResolver = new CustomElementsResolver(this, streakStore); AdaptyUI.setCustomElementsResolver(customElementsResolver); } public StreakStore getStreakStore() { return streakStore; } public CustomElementsResolver getCustomElementsResolver() { return customElementsResolver; }}