diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 62b493ab79..0000000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.gitignore b/.gitignore index 1f58edecb3..2a71d3359c 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ captures/ # Comment next line if keeping position of elements in Navigation Editor is relevant for you .idea/navEditor.xml .idea/codeStyles +.DS_Store # Keystore files # Uncomment the following lines if you do not want to check your keystore files in. diff --git a/ivy-common-ui/src/main/java/com/ivy/common/ui/RememberScrollPosition.kt b/ivy-common-ui/src/main/java/com/ivy/common/ui/RememberScrollPosition.kt new file mode 100644 index 0000000000..66c8dfdd7c --- /dev/null +++ b/ivy-common-ui/src/main/java/com/ivy/common/ui/RememberScrollPosition.kt @@ -0,0 +1,51 @@ +package com.ivy.common.ui + +import androidx.compose.foundation.lazy.LazyListState +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.saveable.rememberSaveable + +/** + * Static field, contains all scroll values + */ +private val saveMap = mutableMapOf() + +private data class KeyParams( + val params: String = "", + val index: Int, + val scrollOffset: Int +) + +/** + * Save scroll state on all time. + * @param key value for comparing screen + * @param params arguments for find different between equals screen + * @param initialFirstVisibleItemIndex see [LazyListState.firstVisibleItemIndex] + * @param initialFirstVisibleItemScrollOffset see [LazyListState.firstVisibleItemScrollOffset] + */ +@Composable +fun rememberScrollPositionListState( + key: String, + params: String = "", + initialFirstVisibleItemIndex: Int = 0, + initialFirstVisibleItemScrollOffset: Int = 0 +): LazyListState { + val scrollState = rememberSaveable(saver = LazyListState.Saver) { + var savedValue = saveMap[key] + if (savedValue?.params != params) savedValue = null + val savedIndex = savedValue?.index ?: initialFirstVisibleItemIndex + val savedOffset = savedValue?.scrollOffset ?: initialFirstVisibleItemScrollOffset + LazyListState( + savedIndex, + savedOffset + ) + } + DisposableEffect(Unit) { + onDispose { + val lastIndex = scrollState.firstVisibleItemIndex + val lastOffset = scrollState.firstVisibleItemScrollOffset + saveMap[key] = KeyParams(params, lastIndex, lastOffset) + } + } + return scrollState +} diff --git a/screen-home/build.gradle.kts b/screen-home/build.gradle.kts index 8598c0c4e8..6fbd39402d 100644 --- a/screen-home/build.gradle.kts +++ b/screen-home/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(projects.ivyNavigation) implementation(projects.tempLegacyCode) implementation(projects.ivyData) + implementation(projects.ivyCommonUi) implementation(projects.widgetAddTransaction) diff --git a/screen-home/src/main/java/com/ivy/home/HomeTab.kt b/screen-home/src/main/java/com/ivy/home/HomeTab.kt index 337b657e83..2e82bfd95a 100644 --- a/screen-home/src/main/java/com/ivy/home/HomeTab.kt +++ b/screen-home/src/main/java/com/ivy/home/HomeTab.kt @@ -10,7 +10,6 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.systemBarsPadding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListState -import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -29,6 +28,7 @@ import com.ivy.base.legacy.Transaction import com.ivy.base.legacy.TransactionHistoryItem import com.ivy.base.legacy.Theme import com.ivy.base.legacy.stringRes +import com.ivy.common.ui.rememberScrollPositionListState import com.ivy.frp.forward import com.ivy.frp.then2 import com.ivy.home.Constants.SWIPE_HORIZONTAL_THRESHOLD @@ -112,7 +112,8 @@ private fun BoxWithConstraintsScope.UI( } ) ) { - val listState = rememberLazyListState( + val listState = rememberScrollPositionListState( + key = "home_lazy_column", initialFirstVisibleItemIndex = ivyContext.transactionsListState ?.firstVisibleItemIndex ?: 0, initialFirstVisibleItemScrollOffset = ivyContext.transactionsListState diff --git a/screen-transactions/build.gradle.kts b/screen-transactions/build.gradle.kts index a66e096f1d..350948e32f 100644 --- a/screen-transactions/build.gradle.kts +++ b/screen-transactions/build.gradle.kts @@ -14,6 +14,7 @@ dependencies { implementation(projects.ivyNavigation) implementation(projects.tempLegacyCode) implementation(projects.ivyData) + implementation(projects.ivyCommonUi) testImplementation(projects.ivyTesting) } \ No newline at end of file diff --git a/screen-transactions/src/main/java/com/ivy/transactions/TransactionsScreen.kt b/screen-transactions/src/main/java/com/ivy/transactions/TransactionsScreen.kt index 213c134a03..0b4520ab3a 100644 --- a/screen-transactions/src/main/java/com/ivy/transactions/TransactionsScreen.kt +++ b/screen-transactions/src/main/java/com/ivy/transactions/TransactionsScreen.kt @@ -14,7 +14,6 @@ import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListScope -import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect @@ -40,6 +39,7 @@ import com.ivy.base.legacy.TransactionHistoryItem import com.ivy.base.legacy.Theme import com.ivy.base.model.TransactionType import com.ivy.base.legacy.stringRes +import com.ivy.common.ui.rememberScrollPositionListState import com.ivy.design.l0_system.UI import com.ivy.design.l0_system.style import com.ivy.legacy.Constants @@ -274,7 +274,9 @@ private fun BoxWithConstraintsScope.UI( ) } ) { - val listState = rememberLazyListState() + val listState = rememberScrollPositionListState( + key = "item_stats_lazy_column" + ) val density = LocalDensity.current LazyColumn(