1 /*
<lambda>null2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.integration.demos.common
18
19 import android.app.Activity
20 import androidx.activity.ComponentActivity
21 import androidx.compose.runtime.Composable
22 import androidx.fragment.app.Fragment
23 import kotlin.reflect.KClass
24
25 /** Generic demo with a [title] that will be displayed in the list of demos. */
26 sealed class Demo(val title: String) {
27 override fun toString() = title
28 }
29
30 /**
31 * Demo that launches an [Activity] when selected.
32 *
33 * This should only be used for demos that need to customize the activity, the large majority of
34 * demos should just use [ComposableDemo] instead.
35 *
36 * @property activityClass the KClass (Foo::class) of the activity that will be launched when this
37 * demo is selected.
38 */
39 class ActivityDemo<T : ComponentActivity>(title: String, val activityClass: KClass<T>) :
40 Demo(title)
41
42 class FragmentDemo<T : Fragment>(title: String, val fragmentClass: KClass<T>) : Demo(title)
43
44 /** Demo that displays [Composable] [content] when selected. */
45 class ComposableDemo(title: String, val content: @Composable (onNavigateUp: () -> Unit) -> Unit) :
46 Demo(title)
47
48 /** A category of [Demo]s, that will display a list of [demos] when selected. */
49 class DemoCategory(title: String, val demos: List<Demo>) : Demo(title)
50
51 /** Flattened recursive DFS [List] of every demo in [this]. */
allDemosnull52 fun DemoCategory.allDemos(): List<Demo> {
53 val allDemos = mutableListOf<Demo>()
54 fun DemoCategory.addAllDemos() {
55 demos.forEach { demo ->
56 allDemos += demo
57 if (demo is DemoCategory) {
58 demo.addAllDemos()
59 }
60 }
61 }
62 addAllDemos()
63 return allDemos
64 }
65
66 /** Flattened recursive DFS [List] of every launchable demo in [this]. */
DemoCategorynull67 fun DemoCategory.allLaunchableDemos(): List<Demo> {
68 return allDemos().filter { it !is DemoCategory }
69 }
70