Skip to content

Commit 382afe2

Browse files
committed
Made a workable solution for scroll to position in list
1 parent cc7a4cf commit 382afe2

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import com.ramcosta.composedestinations.navigation.DestinationsNavigator
4949
import com.ramcosta.composedestinations.result.NavResult
5050
import com.ramcosta.composedestinations.result.ResultRecipient
5151
import com.ramcosta.composedestinations.spec.DestinationSpec
52+
import kotlinx.coroutines.flow.collect
5253
import kotlinx.coroutines.launch
5354
import net.mullvad.mullvadvpn.R
5455
import net.mullvad.mullvadvpn.compose.cell.FilterCell
@@ -79,6 +80,7 @@ import net.mullvad.mullvadvpn.compose.test.SELECT_LOCATION_CUSTOM_LIST_HEADER_TE
7980
import net.mullvad.mullvadvpn.compose.test.SELECT_LOCATION_LOCATION_BOTTOM_SHEET_TEST_TAG
8081
import net.mullvad.mullvadvpn.compose.textfield.SearchTextField
8182
import net.mullvad.mullvadvpn.compose.transitions.SelectLocationTransition
83+
import net.mullvad.mullvadvpn.compose.util.RunOnKeyChange
8284
import net.mullvad.mullvadvpn.lib.theme.AppTheme
8385
import net.mullvad.mullvadvpn.lib.theme.Dimens
8486
import net.mullvad.mullvadvpn.lib.theme.color.AlphaInactive
@@ -276,8 +278,9 @@ fun SelectLocationScreen(
276278
}
277279
Spacer(modifier = Modifier.height(height = Dimens.verticalSpace))
278280
val lazyListState = rememberLazyListState()
279-
val selectedItemCode = (state as? SelectLocationUiState.Content)?.selectedItem?.code
280-
LaunchedEffect(selectedItemCode) {
281+
val selectedItemCode =
282+
(state as? SelectLocationUiState.Content)?.selectedItem?.code ?: ""
283+
RunOnKeyChange(key = selectedItemCode) {
281284
val index = state.indexOfSelectedRelayItem()
282285

283286
if (index >= 0) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.mullvad.mullvadvpn.compose.util
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.rememberCoroutineScope
5+
import androidx.compose.runtime.saveable.rememberSaveable
6+
import kotlinx.coroutines.CoroutineScope
7+
import kotlinx.coroutines.launch
8+
9+
@Composable
10+
fun RunOnKeyChange(key: Any, block: suspend CoroutineScope.() -> Unit) {
11+
val scope = rememberCoroutineScope()
12+
rememberSaveable(key) {
13+
scope.launch {
14+
block()
15+
}
16+
key
17+
}
18+
}

0 commit comments

Comments
 (0)