forked from Ivy-Apps/ivy-wallet
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs Ivy-Apps#2348: Remember expenses scroll list position (Ivy-Apps#…
…2784) * refs Ivy-Apps#2348: Remember expenses scroll list position * Resolve comments
- Loading branch information
1 parent
06ed817
commit 31b4e12
Showing
7 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ivy-common-ui/src/main/java/com/ivy/common/ui/RememberScrollPosition.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters