Skip to content

Commit

Permalink
Merge branch 'scroll-to-selected-relay-in-select-location-screen-droi…
Browse files Browse the repository at this point in the history
…d-564'
  • Loading branch information
Pururun committed Dec 19, 2023
2 parents a5e9e96 + 0924085 commit dce93ba
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 dce93ba

Please sign in to comment.