|
| 1 | +package net.mullvad.mullvadvpn.compose.component |
| 2 | + |
| 3 | +import androidx.compose.foundation.ExperimentalFoundationApi |
| 4 | +import androidx.compose.foundation.Image |
| 5 | +import androidx.compose.foundation.background |
| 6 | +import androidx.compose.foundation.layout.Arrangement |
| 7 | +import androidx.compose.foundation.layout.Box |
| 8 | +import androidx.compose.foundation.layout.Column |
| 9 | +import androidx.compose.foundation.layout.Row |
| 10 | +import androidx.compose.foundation.layout.fillMaxSize |
| 11 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 12 | +import androidx.compose.foundation.layout.padding |
| 13 | +import androidx.compose.foundation.layout.size |
| 14 | +import androidx.compose.foundation.layout.wrapContentHeight |
| 15 | +import androidx.compose.foundation.pager.HorizontalPager |
| 16 | +import androidx.compose.foundation.pager.rememberPagerState |
| 17 | +import androidx.compose.foundation.shape.CircleShape |
| 18 | +import androidx.compose.material3.Icon |
| 19 | +import androidx.compose.material3.IconButton |
| 20 | +import androidx.compose.material3.MaterialTheme |
| 21 | +import androidx.compose.material3.Text |
| 22 | +import androidx.compose.runtime.Composable |
| 23 | +import androidx.compose.runtime.rememberCoroutineScope |
| 24 | +import androidx.compose.ui.Alignment |
| 25 | +import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.draw.alpha |
| 27 | +import androidx.compose.ui.draw.clip |
| 28 | +import androidx.compose.ui.draw.rotate |
| 29 | +import androidx.compose.ui.graphics.Color |
| 30 | +import androidx.compose.ui.res.painterResource |
| 31 | +import androidx.compose.ui.res.stringResource |
| 32 | +import androidx.compose.ui.text.SpanStyle |
| 33 | +import androidx.compose.ui.text.font.FontWeight |
| 34 | +import androidx.compose.ui.tooling.preview.Preview |
| 35 | +import androidx.constraintlayout.compose.ConstraintLayout |
| 36 | +import androidx.core.text.HtmlCompat |
| 37 | +import kotlinx.coroutines.launch |
| 38 | +import net.mullvad.mullvadvpn.R |
| 39 | +import net.mullvad.mullvadvpn.compose.extensions.toAnnotatedString |
| 40 | +import net.mullvad.mullvadvpn.lib.theme.AppTheme |
| 41 | +import net.mullvad.mullvadvpn.lib.theme.Dimens |
| 42 | +import net.mullvad.mullvadvpn.lib.theme.color.AlphaDescription |
| 43 | +import net.mullvad.mullvadvpn.lib.theme.color.AlphaInvisible |
| 44 | +import net.mullvad.mullvadvpn.lib.theme.color.AlphaVisible |
| 45 | + |
| 46 | +@Preview |
| 47 | +@Composable |
| 48 | +private fun PreviewAutoConnectCarousel() { |
| 49 | + AppTheme { AutoConnectCarousel() } |
| 50 | +} |
| 51 | + |
| 52 | +private enum class PAGES(val topText: Int, val image: Int, val bottomText: Int) { |
| 53 | + FIRST( |
| 54 | + R.string.auto_connect_carousel_first_slide_top_text, |
| 55 | + R.drawable.carousel_slide_1_cogwheel, |
| 56 | + R.string.auto_connect_carousel_first_slide_bottom_text |
| 57 | + ), |
| 58 | + SECOND( |
| 59 | + R.string.auto_connect_carousel_second_slide_top_text, |
| 60 | + R.drawable.carousel_slide_2_always_on, |
| 61 | + R.string.auto_connect_carousel_second_slide_bottom_text |
| 62 | + ), |
| 63 | + THIRD( |
| 64 | + R.string.auto_connect_carousel_third_slide_top_text, |
| 65 | + R.drawable.carousel_slide_3_block_connections, |
| 66 | + R.string.auto_connect_carousel_third_slide_bottom_text |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +@OptIn(ExperimentalFoundationApi::class) |
| 71 | +@Composable |
| 72 | +fun AutoConnectCarousel() { |
| 73 | + val pagerState = rememberPagerState(pageCount = { PAGES.entries.size }) |
| 74 | + val scope = rememberCoroutineScope() |
| 75 | + ConstraintLayout( |
| 76 | + modifier = Modifier.fillMaxSize(), |
| 77 | + ) { |
| 78 | + val (pager, backButtonRef, nextButtonRef, pageIndicatorRef) = createRefs() |
| 79 | + HorizontalPager( |
| 80 | + state = pagerState, |
| 81 | + beyondBoundsPageCount = 2, |
| 82 | + modifier = |
| 83 | + Modifier.constrainAs(pager) { |
| 84 | + top.linkTo(parent.top) |
| 85 | + start.linkTo(backButtonRef.end) |
| 86 | + end.linkTo(nextButtonRef.start) |
| 87 | + bottom.linkTo(parent.bottom) |
| 88 | + } |
| 89 | + ) { page -> |
| 90 | + Column( |
| 91 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 92 | + modifier = Modifier.fillMaxWidth() |
| 93 | + ) { |
| 94 | + Text( |
| 95 | + modifier = Modifier.padding(horizontal = Dimens.largePadding), |
| 96 | + style = MaterialTheme.typography.labelMedium, |
| 97 | + color = MaterialTheme.colorScheme.onSecondary, |
| 98 | + text = |
| 99 | + HtmlCompat.fromHtml( |
| 100 | + stringResource(id = PAGES.entries[page].topText), |
| 101 | + HtmlCompat.FROM_HTML_MODE_COMPACT |
| 102 | + ) |
| 103 | + .toAnnotatedString( |
| 104 | + boldSpanStyle = |
| 105 | + SpanStyle( |
| 106 | + fontWeight = FontWeight.ExtraBold, |
| 107 | + color = MaterialTheme.colorScheme.onPrimary |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) |
| 111 | + Image( |
| 112 | + modifier = |
| 113 | + Modifier.padding(top = Dimens.topPadding, bottom = Dimens.bottomPadding), |
| 114 | + painter = painterResource(id = PAGES.entries[page].image), |
| 115 | + contentDescription = null, |
| 116 | + ) |
| 117 | + Text( |
| 118 | + modifier = Modifier.padding(horizontal = Dimens.largePadding), |
| 119 | + style = MaterialTheme.typography.labelMedium, |
| 120 | + color = MaterialTheme.colorScheme.onSecondary, |
| 121 | + text = |
| 122 | + HtmlCompat.fromHtml( |
| 123 | + stringResource(id = PAGES.entries[page].bottomText), |
| 124 | + HtmlCompat.FROM_HTML_MODE_COMPACT |
| 125 | + ) |
| 126 | + .toAnnotatedString( |
| 127 | + boldSpanStyle = |
| 128 | + SpanStyle( |
| 129 | + fontWeight = FontWeight.ExtraBold, |
| 130 | + color = MaterialTheme.colorScheme.onPrimary |
| 131 | + ) |
| 132 | + ) |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + IconButton( |
| 138 | + modifier = |
| 139 | + Modifier.constrainAs(backButtonRef) { |
| 140 | + top.linkTo(parent.top) |
| 141 | + start.linkTo(parent.start) |
| 142 | + bottom.linkTo(parent.bottom) |
| 143 | + } |
| 144 | + .alpha(if (pagerState.currentPage != 0) AlphaVisible else AlphaInvisible), |
| 145 | + onClick = { |
| 146 | + scope.launch { pagerState.animateScrollToPage(pagerState.currentPage - 1) } |
| 147 | + }, |
| 148 | + enabled = pagerState.currentPage != 0 |
| 149 | + ) { |
| 150 | + Icon( |
| 151 | + painter = painterResource(id = R.drawable.icon_chevron), |
| 152 | + contentDescription = null, |
| 153 | + tint = Color.Unspecified, |
| 154 | + modifier = Modifier.rotate(180f).alpha(AlphaDescription) |
| 155 | + ) |
| 156 | + } |
| 157 | + |
| 158 | + IconButton( |
| 159 | + modifier = |
| 160 | + Modifier.constrainAs(nextButtonRef) { |
| 161 | + top.linkTo(parent.top) |
| 162 | + end.linkTo(parent.end) |
| 163 | + bottom.linkTo(parent.bottom) |
| 164 | + } |
| 165 | + .alpha( |
| 166 | + if (pagerState.currentPage != pagerState.pageCount - 1) AlphaVisible |
| 167 | + else AlphaInvisible |
| 168 | + ), |
| 169 | + onClick = { |
| 170 | + scope.launch { pagerState.animateScrollToPage(pagerState.currentPage + 1) } |
| 171 | + }, |
| 172 | + enabled = pagerState.currentPage != pagerState.pageCount - 1 |
| 173 | + ) { |
| 174 | + Icon( |
| 175 | + painter = painterResource(id = R.drawable.icon_chevron), |
| 176 | + contentDescription = null, |
| 177 | + tint = Color.Unspecified, |
| 178 | + modifier = Modifier.alpha(AlphaDescription) |
| 179 | + ) |
| 180 | + } |
| 181 | + Row( |
| 182 | + Modifier.wrapContentHeight() |
| 183 | + .fillMaxWidth() |
| 184 | + .padding(top = Dimens.topPadding) |
| 185 | + .constrainAs(pageIndicatorRef) { |
| 186 | + top.linkTo(pager.bottom) |
| 187 | + end.linkTo(parent.end) |
| 188 | + start.linkTo(parent.start) |
| 189 | + }, |
| 190 | + horizontalArrangement = Arrangement.Center, |
| 191 | + verticalAlignment = Alignment.Bottom |
| 192 | + ) { |
| 193 | + repeat(pagerState.pageCount) { iteration -> |
| 194 | + val color = |
| 195 | + if (pagerState.currentPage == iteration) MaterialTheme.colorScheme.onPrimary |
| 196 | + else MaterialTheme.colorScheme.primary |
| 197 | + Box( |
| 198 | + modifier = |
| 199 | + Modifier.padding(Dimens.indicatorPadding) |
| 200 | + .clip(CircleShape) |
| 201 | + .background(color) |
| 202 | + .size(Dimens.indicatorSize) |
| 203 | + ) |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments