Skip to content

Commit

Permalink
Scroll to list first item on list update
Browse files Browse the repository at this point in the history
  • Loading branch information
abdrasulov committed Jun 13, 2024
1 parent 17f2a52 commit cd876ce
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ 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.items
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -78,6 +77,7 @@ import io.horizontalsystems.bankwallet.ui.compose.components.subhead1_lucian
import io.horizontalsystems.bankwallet.ui.compose.components.subhead1_remus
import io.horizontalsystems.bankwallet.ui.compose.components.subhead2_grey
import io.horizontalsystems.bankwallet.ui.compose.components.title3_leah
import io.horizontalsystems.bankwallet.ui.compose.hsRememberLazyListState
import io.horizontalsystems.core.helpers.DateHelper
import io.horizontalsystems.core.helpers.HudHelper
import io.horizontalsystems.marketkit.models.EtfPoint
Expand Down Expand Up @@ -139,14 +139,11 @@ fun EtfPage(
}

ViewState.Success -> {
val listState = rememberSaveable(
val listState = hsRememberLazyListState(
2,
uiState.sortBy,
uiState.timeDuration,
saver = LazyListState.Saver
) {
LazyListState()
}

uiState.timeDuration
)
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -45,6 +43,7 @@ import io.horizontalsystems.bankwallet.ui.compose.components.HeaderSorting
import io.horizontalsystems.bankwallet.ui.compose.components.ListErrorView
import io.horizontalsystems.bankwallet.ui.compose.components.MarketCoinClear
import io.horizontalsystems.bankwallet.ui.compose.components.MenuItem
import io.horizontalsystems.bankwallet.ui.compose.hsRememberLazyListState

class MetricsPageFragment : BaseComposeFragment() {

Expand Down Expand Up @@ -110,13 +109,7 @@ class MetricsPageFragment : BaseComposeFragment() {
}

ViewState.Success -> {
val listState = rememberSaveable(
uiState.sortDescending,
saver = LazyListState.Saver
) {
LazyListState()
}

val listState = hsRememberLazyListState(2, uiState.sortDescending)
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ 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.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -59,6 +57,7 @@ import io.horizontalsystems.bankwallet.ui.compose.components.MarketCoinFirstRow
import io.horizontalsystems.bankwallet.ui.compose.components.MarketCoinSecondRow
import io.horizontalsystems.bankwallet.ui.compose.components.MenuItem
import io.horizontalsystems.bankwallet.ui.compose.components.SectionItemBorderedRowUniversalClear
import io.horizontalsystems.bankwallet.ui.compose.hsRememberLazyListState
import io.horizontalsystems.core.helpers.HudHelper

class TvlFragment : BaseComposeFragment() {
Expand Down Expand Up @@ -128,13 +127,11 @@ class TvlFragment : BaseComposeFragment() {
}

ViewState.Success -> {
val listState = rememberSaveable(
val listState = hsRememberLazyListState(
2,
tvlData?.chainSelect?.selected,
tvlData?.sortDescending,
saver = LazyListState.Saver
) {
LazyListState()
}
tvlData?.sortDescending
)

LazyColumn(
state = listState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.horizontalsystems.bankwallet.ui.compose

import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.runtime.*
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow

@Composable
fun LazyListState.OnBottomReached(
Expand Down Expand Up @@ -29,3 +34,15 @@ fun LazyListState.OnBottomReached(
.collect { if (it) onLoadMore() }
}
}

@Composable
fun hsRememberLazyListState(i: Int, vararg keys: Any?): LazyListState {
val listState = rememberLazyListState()
LaunchedEffect(keys = keys) {
if (listState.firstVisibleItemIndex >= i) {
listState.scrollToItem(i)
}
}

return listState
}

0 comments on commit cd876ce

Please sign in to comment.