Skip to content

Commit

Permalink
refs Ivy-Apps#2348: Remember expenses scroll list position (Ivy-Apps#…
Browse files Browse the repository at this point in the history
…2784)

* refs Ivy-Apps#2348: Remember expenses scroll list position

* Resolve comments
  • Loading branch information
MaratKhakim authored Oct 10, 2023
1 parent 06ed817 commit 31b4e12
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, KeyParams>()

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
}
1 change: 1 addition & 0 deletions screen-home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation(projects.ivyNavigation)
implementation(projects.tempLegacyCode)
implementation(projects.ivyData)
implementation(projects.ivyCommonUi)

implementation(projects.widgetAddTransaction)

Expand Down
5 changes: 3 additions & 2 deletions screen-home/src/main/java/com/ivy/home/HomeTab.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions screen-transactions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {
implementation(projects.ivyNavigation)
implementation(projects.tempLegacyCode)
implementation(projects.ivyData)
implementation(projects.ivyCommonUi)

testImplementation(projects.ivyTesting)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -274,7 +274,9 @@ private fun BoxWithConstraintsScope.UI(
)
}
) {
val listState = rememberLazyListState()
val listState = rememberScrollPositionListState(
key = "item_stats_lazy_column"
)
val density = LocalDensity.current

LazyColumn(
Expand Down

0 comments on commit 31b4e12

Please sign in to comment.