// 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 } }}
streak_ring のケースを他の lottie_animation ケースより前に配置してください。Swift は最初に一致したケースのみを実行するためです。アプリバンドルに該当 ID のファイルが存在しない場合、リゾルバーは 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 }}
class MainApplication : Application(), ReactApplication { // the streak model that your app updates val streakStore = StreakStore() // the other members of your MainApplication class override fun onCreate() { super.onCreate() AdaptyUI.setCustomElementsResolver(CustomElementsResolver(this, streakStore)) // the rest of your onCreate code }}
// the streak that your JavaScript code sends through the native moduleNotificationCenter.default.addObserver( forName: Notification.Name("StreakDidChange"), object: nil, queue: .main) { [streakStore] notification in guard let days = notification.userInfo?["days"] as? Int else { return } MainActor.assumeIsolated { streakStore.currentStreak = days }}
スペックのネイティブコードを生成するには、ios フォルダで pod install を実行してください。
Create the module, and a package that registers it with React Native:
package com.example.appclass NativeStreakStoreModule(reactContext: ReactApplicationContext) : NativeStreakStoreSpec(reactContext) { override fun getName() = NAME override fun setStreak(days: Double) { val app = reactApplicationContext.applicationContext as MainApplication app.streakStore.currentStreak.value = days.toInt() } companion object { const val NAME = "NativeStreakStore" }}
class NativeStreakStorePackage : BaseReactPackage() { override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? = if (name == NativeStreakStoreModule.NAME) NativeStreakStoreModule(reactContext) else null override fun getReactModuleInfoProvider() = ReactModuleInfoProvider { mapOf( NativeStreakStoreModule.NAME to ReactModuleInfo( name = NativeStreakStoreModule.NAME, className = NativeStreakStoreModule.NAME, canOverrideExistingModule = false, needsEagerInit = false, isCxxModule = false, isTurboModule = true, ) ) }}