Skill'ler SDK entegrasyonu, flow ve paywall oluşturma, yayın öncesi denetim, placement taşıma ve daha fazlasını kapsar. Görevi kendi cümlelerinizle anlatın, ajanınız doğru skill'i seçsin.
Kullanıcının ilerlemesini bir Lottie animasyonu olarak göstermek için bu tarifi takip edin — örneğin, günlük serisini göstermek için dolan bir halka. Uygulamanız animasyonu bir özel element içinde yerel bir Lottie görünümü olarak render eder.
Başlamadan önce
Uygulamanızı Adapty React Native SDK 4.4 veya sonrasına güncelleyin. Daha eski SDK sürümleri, her özel öğe için yedek içeriği gösterir.
Uygulamanızın yerel kodunu özel öğeler için, React Native kılavuzunda açıklandığı gibi ayarlayın.
iOS’ta, https://github.com/airbnb/lottie-spm.git adresinden Swift Package Manager ile Xcode’daki uygulama hedefine Lottie ekleyin. Uygulamanız lottie-react-native kullanıyorsa bu adımı atlayın: o paket Lottie’yi zaten uygulamanıza ekler.
Android’de, uygulama modülünüzde Jetpack Compose’u kurun. Bu tarifte animasyon görünümü bir composable’dır. androidx.compose.foundation:foundation ve com.airbnb.android:lottie-compose kaynaklarından Gradle ile uygulamanıza Compose Foundation ve Jetpack Compose için Lottie ekleyin.
.json formatında bir Lottie animasyonu hazırlayın ya da örnek streak_ring.json dosyasını indirin. Uygulama animasyonu baştan oynatır ve ilerlemeyle orantılı olarak durdurur; örneğin %50 için tam ortasında durur. Tam ilerlemeyi göstermek için animasyonun şu özelliklere sahip olması gerekir:
İlk karede hiç ilerleme olmamalı, son karede ise tam ilerleme gösterilmeli.
Sabit bir hız, yumuşatma (easing) olmadan. Animasyon yavaş başlayıp hızlanırsa, yarı yolda gerçek ilerlemenin yarısından daha azını gösterir.
1. Ekrana özel öğe ekleyin
Flow & Paywall Builder’da, bir flow ekranına özel element ekleyin ve animasyon için yapılandırın:
Type: lottie_animation.
ID: streak_ring. Bu tarifde ID, aynı zamanda animasyon dosyasının adıdır.
Height: Varsayılan Fixed yüksekliği koruyun. Animasyon, özel elementin boyutuna göre ölçeklenir.
Özel elementin içine, halkanın hareketsiz bir görüntüsünü içeren bir Image ekleyin. Kullanıcılar, uygulamanızın animasyonu oynatamayan sürümlerinde bunu yedek içerik olarak görür.
2. Animasyon dosyasını uygulamanıza ekleyin
Yerel kodunuz, animasyon dosyasını JavaScript kodunuzun asset’lerinden değil, iOS ve Android projelerinizden okur:
iOS: Dosyayı Xcode’da uygulama hedefinize ekleyin.
Android: Dosyayı android/app/src/main/assets dizinine, yani Android uygulama modülünüzün assets klasörüne koyun. Dosyayı bir alt klasöre değil, bu klasörün kök dizinine yerleştirin.
.json uzantısı olmadan dosya adı, streak_ring için streak_ring.json gibi özel öğenin kimliğiyle eşleşmelidir.
3. Animasyon görünümünü oluşturun
Streak için bir model ve animasyonu oynatacak bir görünüm oluşturun. Uygulamanız streak değiştiğinde modeli günceller. Görünüm modeli okur, böylece ekran açıldığında halka mevcut streaki gösterir:
// 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)) }}
.playing(.fromProgress(0, toProgress: progress, loopMode: .playOnce)) halkayı bir kez, boştan progress değerine kadar doldurur.
// 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), )}
clipSpec = LottieClipSpec.Progress(0f, progress) halkayı baştan progress değerine kadar doldurur.
4. Animasyonu özel elemente bağla
Uygulama genelindeki resolver’ınıza streak modeli için bir özellik ekleyin. Ardından lottie_animation Type ve streak_ring ID’si için animasyon görünümünü döndürün. Vurgulanan satırlar, eklenecek veya değiştirilecek kısımları gösterir. Uygulamanızda henüz uygulama genelinde bir resolver yoksa kodun tamamını kopyalayabilirsiniz:
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 case’ini diğer lottie_animation case’lerinden önce yerleştirin; çünkü Swift yalnızca eşleşen ilk case’i çalıştırır. Uygulama paketinde söz konusu ID için dosya yoksa, çözümleyici nil döndürür ve kullanıcılar yedek içeriği görür.
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 }}
assets klasöründe ilgili ID için dosya yoksa, resolver null döndürür ve kullanıcılar yedek içeriği görür. Resolver’ınız zaten her animasyon dosyası için bir view döndürüyorsa, ID streak_ring olmadığında null yerine o view’ı döndürün. Bu sayede kullanıcılar diğer lottie_animation özel öğelerinizin animasyonlarını görmeye devam eder.
Streak modeliyle resolver’ı oluşturun ve uygulama başlatılırken uygulama genelinde geçerli resolver olarak ayarlayın:
In AppDelegate.swift, add a property for the streak model, and set the resolver in the application(_:didFinishLaunchingWithOptions:) method. The highlighted lines show what to add. Keep the rest of the AppDelegate class as it is:
@mainclass AppDelegate: UIResponder, UIApplicationDelegate { // the streak model that your app updates let streakStore = StreakStore() func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil ) -> Bool { AdaptyUI.setCustomElementsResolver(CustomElementsResolver(streakStore: streakStore)) // the rest of your launch code return true }}
In MainApplication.kt, add a property for the streak model, and set the resolver in the onCreate method. The highlighted lines show what to add. Keep the rest of the MainApplication class as it is:
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 }}
5. JavaScript kodunuzdan streak’i gönderin
JavaScript kodunuz streak’i tutar ve native görünümler bunu streak modelinden okur. Modeli güncellemek için streak’i Turbo Native Module aracılığıyla native kodunuza gönderin. Modülün bir JavaScript spec’i ve her platform için bir implementasyonu vardır.
Projenizin kök dizinindeki bir specs klasöründe modülün spec’ini oluşturun:
To generate the native code of the spec, add codegenConfig to package.json. com.example.app kısmını package.json dosyasında ve modülün Kotlin implementasyonunda Android uygulamanızın paket adıyla değiştirin. package.json dosyasında codegenConfig zaten varsa, yalnızca modulesProvider nesnesine NativeStreakStore ekleyin:
#import "RCTNativeStreakStore.h"@implementation RCTNativeStreakStore+ (NSString *)moduleName { return @"NativeStreakStore";}- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: (const facebook::react::ObjCTurboModule::InitParams &)params { return std::make_shared<facebook::react::NativeStreakStoreSpecJSI>(params);}- (void)setStreak:(double)days { // the AppDelegate updates the streak model when it receives this notification [[NSNotificationCenter defaultCenter] postNotificationName:@"StreakDidChange" object:nil userInfo:@{@"days": @((NSInteger)days)}];}@end
AppDelegate sınıfınızda NotificationCenter’ı gözlemleyerek her yeni streak ile streak modelini güncelleyin. application(_:didFinishLaunchingWithOptions:) metodunda, AdaptyUI.setCustomElementsResolver çağrısından sonra gözlemciyi ekleyin:
// 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 }}
Spesifikasyonun native kodunu üretmek için ios klasöründe pod install komutunu çalıştırın.
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, ) ) }}
MainApplication.kt dosyasında, NativeStreakStorePackage’ı paket listesine ekleyin:
JavaScript kodunuzda, uygulamanın her açılışında ve streak her değiştiğinde streak’i gönderin:
// call at app launch, and each time the streak changesexport function sendStreak(days: number) { NativeStreakStore.setStreak(days);}
6. Cihazda test etme
Flow & Paywall Builder, uygulama kodunuzu çalıştıramadığından canvas yalnızca yedek içeriği gösterir. Animasyonu önizlemek için:
Flow’u yayınlayın. Önbelleğe alınmış sürüm yerine en son yayınlanan sürümü görmek için cihazı bir test cihazı olarak kaydedin.
Resolver’ı içeren bir uygulama derlemesi çalıştırın. Streak’i 0 güne ayarlayın, ekranı açın ve halkının boş kaldığını doğrulayın. 4 ve 7 gün ile tekrarlayın ve halkının her değere kadar dolduğunu doğrulayın.
Yedek içeriği test etmek için animasyon dosyasını derlemeden kaldırın ve ekranı tekrar açın.
Yedek görsel yerine animasyonun neden gösterilmediğini öğrenmek için React Native kılavuzunun Cihazda test etme bölümündeki talimatları izleyin.