// The streak that your app keeps up to date@MainActorfinal class StreakStore: ObservableObject { @Published var currentStreak = 0 // days in a row}struct StreakRingView: View { let animation: LottieAnimation @ObservedObject var streakStore: StreakStore // 7 days fill the ring: from 0 for an empty ring to 1 for a full one private var progress: AnimationProgressTime { min(AnimationProgressTime(streakStore.currentStreak) / 7, 1) } var body: some View { LottieView(animation: animation) .resizable() .playing(.fromProgress(0, toProgress: progress, loopMode: .playOnce)) }}
// 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), )}
struct CustomElementsResolver: AdaptyUICustomElementsResolver { let streakStore: StreakStore func view( forType type: String, id: String, context: AdaptyUICustomElementContext ) -> AnyView? { switch type { case "lottie_animation" where id == "streak_ring": guard let animation = LottieAnimation.named(id) else { return nil // no animation file: show the fallback } return AnyView(StreakRingView(animation: animation, streakStore: streakStore)) // the other cases of your resolver default: return nil } }}
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 }}
@main@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate { // the streak model that your app updates let streakStore = StreakStore() override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { AdaptyUI.setCustomElementsResolver(CustomElementsResolver(streakStore: streakStore)) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) }}
class App : Application() { // the streak model that your app updates val streakStore = StreakStore() override fun onCreate() { super.onCreate() AdaptyUI.setCustomElementsResolver(CustomElementsResolver(this, streakStore)) }}
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) let streakChannel = FlutterMethodChannel( name: "custom_elements/streak", binaryMessenger: engineBridge.applicationRegistrar.messenger() ) streakChannel.setMethodCallHandler { [streakStore] call, result in guard call.method == "setStreak", let days = call.arguments as? Int else { return result(FlutterMethodNotImplemented) } // the channel calls this handler on the main thread MainActor.assumeIsolated { streakStore.currentStreak = days } result(nil) }}
class MainActivity : FlutterFragmentActivity() { override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) val streakStore = (application as App).streakStore MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "custom_elements/streak") .setMethodCallHandler { call, result -> if (call.method == "setStreak") { streakStore.currentStreak.value = call.arguments as Int result.success(null) } else { result.notImplemented() } } }}
Dartコードで、アプリ起動時およびストリーク変更のたびにストリークを送信します:
// your iOS and Android code listens to this channelconst streakChannel = MethodChannel('custom_elements/streak');// call at app launch, and each time the streak changesFuture<void> sendStreak(int days) async { await streakChannel.invokeMethod('setStreak', days);}