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.runtime.saveable.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.foundation.background
21 import androidx.compose.foundation.clickable
22 import androidx.compose.foundation.layout.Arrangement
23 import androidx.compose.foundation.layout.Box
24 import androidx.compose.foundation.layout.Column
25 import androidx.compose.foundation.layout.Row
26 import androidx.compose.foundation.layout.fillMaxSize
27 import androidx.compose.foundation.layout.padding
28 import androidx.compose.foundation.shape.RoundedCornerShape
29 import androidx.compose.material.Text
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.getValue
32 import androidx.compose.runtime.mutableStateOf
33 import androidx.compose.runtime.saveable.rememberSaveable
34 import androidx.compose.runtime.saveable.rememberSaveableStateHolder
35 import androidx.compose.runtime.setValue
36 import androidx.compose.ui.Modifier
37 import androidx.compose.ui.graphics.Color
38 import androidx.compose.ui.unit.dp
39 
40 @Sampled
41 @Composable
42 fun SimpleNavigationWithSaveableStateSample() {
43     @Composable
44     fun <T : Any> Navigation(
45         currentScreen: T,
46         modifier: Modifier = Modifier,
47         content: @Composable (T) -> Unit
48     ) {
49         // create SaveableStateHolder.
50         val saveableStateHolder = rememberSaveableStateHolder()
51         Box(modifier) {
52             // Wrap the content representing the `currentScreen` inside `SaveableStateProvider`.
53             // Here you can also add a screen switch animation like Crossfade where during the
54             // animation multiple screens will be displayed at the same time.
55             saveableStateHolder.SaveableStateProvider(currentScreen) { content(currentScreen) }
56         }
57     }
58 
59     Column {
60         var screen by rememberSaveable { mutableStateOf("screen1") }
61         Row(horizontalArrangement = Arrangement.SpaceEvenly) {
62             Button(onClick = { screen = "screen1" }) { Text("Go to screen1") }
63             Button(onClick = { screen = "screen2" }) { Text("Go to screen2") }
64         }
65         Navigation(screen, Modifier.fillMaxSize()) { currentScreen ->
66             if (currentScreen == "screen1") {
67                 Screen1()
68             } else {
69                 Screen2()
70             }
71         }
72     }
73 }
74 
75 @Composable
Screen1null76 fun Screen1() {
77     var counter by rememberSaveable { mutableStateOf(0) }
78     Button(onClick = { counter++ }) { Text("Counter=$counter on Screen1") }
79 }
80 
81 @Composable
Screen2null82 fun Screen2() {
83     Text("Screen2")
84 }
85 
86 @Composable
Buttonnull87 fun Button(modifier: Modifier = Modifier, onClick: () -> Unit, content: @Composable () -> Unit) {
88     Box(
89         modifier
90             .clickable(onClick = onClick)
91             .background(Color(0xFF6200EE), RoundedCornerShape(4.dp))
92             .padding(horizontal = 16.dp, vertical = 8.dp)
93     ) {
94         content()
95     }
96 }
97