xref: /aosp_15_r20/external/lottie/sample-compose/src/main/java/com/airbnb/lottie/sample/compose/Route.kt (revision bb5273fecd5c61b9ace70f9ff4fcd88f0e12e3f7)
1 package com.airbnb.lottie.sample.compose
2 
3 import android.util.Base64
4 import androidx.navigation.NamedNavArgument
5 import androidx.navigation.NavController
6 import androidx.navigation.navArgument
7 
navigatenull8 fun NavController.navigate(route: Route) = navigate(route.route)
9 
10 sealed class Route(val route: String, val args: List<NamedNavArgument> = emptyList()) {
11     object Showcase : Route("showcase")
12 
13     object Preview : Route("preview")
14 
15     object LottieFiles : Route("lottiefiles")
16 
17     object Examples : Route("examples")
18 
19     object BasicUsageExamples : Route("basic usage examples")
20 
21     object AnimatableUsageExamples : Route("LottieAnimatable examples")
22 
23     object TransitionsExamples : Route("transitions examples")
24 
25     object ViewPagerExample : Route("view pager example")
26 
27     object NetworkExamples : Route("network examples")
28 
29     object ImagesExamples : Route("image examples")
30 
31     object TextExamples : Route("text examples")
32 
33     object DynamicPropertiesExamples : Route("dynamic properties examples")
34 
35     object ContentScaleExamples : Route("ContentScale examples")
36 
37     object CachingExamples : Route("Caching examples")
38 
39     object Player : Route(
40         "player",
41         listOf(
42             navArgument("url") {
43                 androidx.navigation.NavType.StringType
44                 nullable = true
45             },
46             navArgument("file") {
47                 androidx.navigation.NavType.StringType
48                 nullable = true
49             },
50             navArgument("asset") {
51                 androidx.navigation.NavType.StringType
52                 nullable = true
53             },
54             navArgument("backgroundColor") {
55                 androidx.navigation.NavType.StringType
56                 nullable = true
57             },
58         )
59     ) {
60         val fullRoute = "$route?url={url}&file={file}&asset={asset}&backgroundColor={backgroundColor}"
61 
62         fun forUrl(url: String, backgroundColor: String? = null) = when (backgroundColor) {
63             null -> "${route}?url=${url.toBase64()}"
64             else -> "${route}?url=${url.toBase64()}&backgroundColor=${backgroundColor.toBase64()}"
65         }
66 
67         fun forFile(file: String) = "${route}?file=${file.toBase64()}"
68 
69         fun forAsset(asset: String) = "${route}?asset=${asset.toBase64()}"
70     }
71 }
72 
toBase64null73 private fun String.toBase64() = Base64.encodeToString(toByteArray(), Base64.NO_PADDING or Base64.NO_WRAP or Base64.URL_SAFE)