1 /*
2  * Copyright 2022 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.wear.compose.material.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.animation.core.animateFloatAsState
21 import androidx.compose.foundation.layout.Box
22 import androidx.compose.foundation.layout.fillMaxSize
23 import androidx.compose.foundation.layout.padding
24 import androidx.compose.runtime.Composable
25 import androidx.compose.runtime.getValue
26 import androidx.compose.runtime.mutableStateOf
27 import androidx.compose.runtime.remember
28 import androidx.compose.runtime.setValue
29 import androidx.compose.ui.Alignment
30 import androidx.compose.ui.Modifier
31 import androidx.compose.ui.unit.dp
32 import androidx.wear.compose.material.HorizontalPageIndicator
33 import androidx.wear.compose.material.Icon
34 import androidx.wear.compose.material.InlineSlider
35 import androidx.wear.compose.material.InlineSliderDefaults
36 import androidx.wear.compose.material.PageIndicatorState
37 
38 @Sampled
39 @Composable
HorizontalPageIndicatorSamplenull40 fun HorizontalPageIndicatorSample() {
41     val maxPages = 9
42     var selectedPage by remember { mutableStateOf(0) }
43     var finalValue by remember { mutableStateOf(0) }
44 
45     val animatedSelectedPage by
46         animateFloatAsState(
47             targetValue = selectedPage.toFloat(),
48         ) {
49             finalValue = it.toInt()
50         }
51 
52     val pageIndicatorState: PageIndicatorState = remember {
53         object : PageIndicatorState {
54             override val pageOffset: Float
55                 get() = animatedSelectedPage - finalValue
56 
57             override val selectedPage: Int
58                 get() = finalValue
59 
60             override val pageCount: Int
61                 get() = maxPages
62         }
63     }
64 
65     Box(modifier = Modifier.fillMaxSize().padding(6.dp)) {
66         InlineSlider(
67             modifier = Modifier.align(Alignment.Center),
68             value = selectedPage,
69             increaseIcon = { Icon(InlineSliderDefaults.Increase, "Increase") },
70             decreaseIcon = { Icon(InlineSliderDefaults.Decrease, "Decrease") },
71             valueProgression = 0 until maxPages,
72             onValueChange = { selectedPage = it }
73         )
74         HorizontalPageIndicator(pageIndicatorState = pageIndicatorState)
75     }
76 }
77