Skip to content

Commit

Permalink
Scroll to selected country
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Dec 15, 2023
1 parent d337e05 commit 8c1a5b2
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.compose.screen

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.animateScrollBy
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand All @@ -11,6 +12,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
Expand Down Expand Up @@ -161,6 +163,18 @@ fun SelectLocationScreen(
}
Spacer(modifier = Modifier.height(height = Dimens.verticalSpace))
val lazyListState = rememberLazyListState()
if (uiState is SelectLocationUiState.ShowData && uiState.selectedRelay != null) {
LaunchedEffect(uiState.selectedRelay) {
val index =
uiState.countries.indexOfFirst {
it.location.location.country ==
uiState.selectedRelay.location.location.country
}

lazyListState.scrollToItem(index)
lazyListState.animateScrollAndCentralizeItem(index)
}
}
LazyColumn(
modifier =
Modifier.fillMaxSize()
Expand Down Expand Up @@ -239,3 +253,14 @@ fun SelectLocationScreen(
}
}
}

suspend fun LazyListState.animateScrollAndCentralizeItem(index: Int) {
val itemInfo = this.layoutInfo.visibleItemsInfo.firstOrNull { it.index == index }
if (itemInfo != null) {
val center = layoutInfo.viewportEndOffset / 2
val childCenter = itemInfo.offset + itemInfo.size / 2
animateScrollBy((childCenter - center).toFloat())
} else {
animateScrollToItem(index)
}
}

0 comments on commit 8c1a5b2

Please sign in to comment.