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 7, 2023
1 parent 61921f9 commit f4e3a28
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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 @@ -14,6 +15,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.MaterialTheme
import androidx.compose.material3.Text
Expand Down Expand Up @@ -163,6 +165,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.focusRequester(listFocus)
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 f4e3a28

Please sign in to comment.