Skip to content

Commit

Permalink
Save scroll offset of bottom navigation screens (#324)
Browse files Browse the repository at this point in the history
* Save scroll offset of bottom navigation screens

* Save scroll offset of folder detail
  • Loading branch information
alexandregpereira authored Aug 18, 2024
1 parent fb0f1a4 commit d0fe059
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import br.alexandregpereira.hunter.domain.folder.model.MonsterPreviewFolder
import br.alexandregpereira.hunter.folder.detail.di.FolderDetailStateRecoveryQualifier
import br.alexandregpereira.hunter.folder.detail.ui.FolderDetailScreen
import br.alexandregpereira.hunter.ui.compendium.monster.ColorState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterCardState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterImageState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterTypeState
import br.alexandregpereira.hunter.ui.compose.StateRecoveryLaunchedEffect
import org.koin.compose.koinInject
import org.koin.core.qualifier.named
Expand All @@ -38,10 +44,35 @@ fun FolderDetailFeature(
val state by viewModel.state.collectAsState()

FolderDetailScreen(
state = state,
isOpen = state.isOpen,
folderName = state.folderName,
monsters = remember(state.monsters) { state.monsters.asState() },
initialFirstVisibleItemIndex = remember { state.firstVisibleItemIndex },
initialFirstVisibleItemScrollOffset = remember { state.firstVisibleItemScrollOffset },
contentPadding = contentPadding,
onItemCLick = viewModel::onItemClick,
onItemLongCLick = viewModel::onItemLongClick,
onClose = viewModel::onClose,
onScrollChanges = viewModel::onScrollChanges,
)
}

private fun List<MonsterPreviewFolder>.asState(): List<MonsterCardState> = map {
it.run {
MonsterCardState(
index = index,
name = name,
imageState = MonsterImageState(
url = imageUrl,
type = MonsterTypeState.valueOf(type.name),
backgroundColor = ColorState(
light = backgroundColorLight,
dark = backgroundColorDark
),
challengeRating = challengeRating,
isHorizontal = isHorizontalImage,
contentDescription = name
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,68 +17,68 @@
package br.alexandregpereira.hunter.folder.detail.ui

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.dp
import br.alexandregpereira.hunter.domain.folder.model.MonsterPreviewFolder
import br.alexandregpereira.hunter.folder.detail.FolderDetailState
import br.alexandregpereira.hunter.ui.compendium.CompendiumItemState.Item
import br.alexandregpereira.hunter.ui.compendium.CompendiumItemState.Title
import br.alexandregpereira.hunter.ui.compendium.monster.ColorState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterCardState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterCompendium
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterImageState
import br.alexandregpereira.hunter.ui.compendium.monster.MonsterTypeState
import br.alexandregpereira.hunter.ui.compose.AppFullScreen
import br.alexandregpereira.hunter.ui.compose.plus

@Composable
internal fun FolderDetailScreen(
state: FolderDetailState,
isOpen: Boolean,
folderName: String,
monsters: List<MonsterCardState>,
initialFirstVisibleItemIndex: Int = 0,
initialFirstVisibleItemScrollOffset: Int = 0,
contentPadding: PaddingValues = PaddingValues(),
onItemCLick: (index: String) -> Unit = {},
onItemLongCLick: (index: String) -> Unit = {},
onClose: () -> Unit = {}
onClose: () -> Unit = {},
onScrollChanges: (Int, Int) -> Unit = { _, _ -> },
) = AppFullScreen(
isOpen = state.isOpen,
isOpen = isOpen,
contentPaddingValues = contentPadding,
level = 0,
onClose = onClose
) {
val monsters = remember(state.monsters) { state.monsters.asState() }
val items = listOf(
Title(
value = state.folderName,
value = folderName,
isHeader = true
),
) + monsters.map {
Item(value = it)
}
val listState: LazyGridState = rememberLazyGridState(
initialFirstVisibleItemIndex = initialFirstVisibleItemIndex,
initialFirstVisibleItemScrollOffset = initialFirstVisibleItemScrollOffset
)
OnScrollChanges(
getScrollItemScrollValues = {
listState.firstVisibleItemIndex to listState.firstVisibleItemScrollOffset
},
onScrollChanges = onScrollChanges,
)
MonsterCompendium(
items = items,
listState = listState,
animateItems = true,
contentPadding = contentPadding + PaddingValues(top = 24.dp),
onItemCLick = onItemCLick,
onItemLongCLick = onItemLongCLick
)
}

private fun List<MonsterPreviewFolder>.asState(): List<MonsterCardState> = map {
it.run {
MonsterCardState(
index = index,
name = name,
imageState = MonsterImageState(
url = imageUrl,
type = MonsterTypeState.valueOf(type.name),
backgroundColor = ColorState(
light = backgroundColorLight,
dark = backgroundColorDark
),
challengeRating = challengeRating,
isHorizontal = isHorizontalImage,
contentDescription = name
)
)
}
@Composable
private fun OnScrollChanges(
getScrollItemScrollValues: () -> Pair<Int, Int>,
onScrollChanges: (Int, Int) -> Unit
) {
val (firstVisibleItemIndex, firstVisibleItemScrollOffset) = getScrollItemScrollValues()
onScrollChanges(firstVisibleItemIndex, firstVisibleItemScrollOffset)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ import br.alexandregpereira.hunter.domain.folder.model.MonsterPreviewFolder
data class FolderDetailState(
val isOpen: Boolean = false,
val folderName: String = "",
val monsters: List<MonsterPreviewFolder> = emptyList()
val monsters: List<MonsterPreviewFolder> = emptyList(),
val firstVisibleItemIndex: Int = 0,
val firstVisibleItemScrollOffset: Int = 0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class FolderDetailStateHolder internal constructor(
folderDetailEventManager.dispatchResult(OnVisibilityChanges(isShowing = false))
}

fun onScrollChanges(firstVisibleItemIndex: Int, firstVisibleItemScrollOffset: Int) {
setState {
copy(
firstVisibleItemIndex = firstVisibleItemIndex,
firstVisibleItemScrollOffset = firstVisibleItemScrollOffset
)
}
}

private fun loadMonsters(folderName: String) {
getMonstersByFolder(folderName)
.flowOn(dispatcher)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ fun FolderListFeature(
onItemSelectionClose = viewModel::onItemSelectionClose,
onItemSelectionDeleteClick = viewModel::onItemSelectionDeleteClick,
onItemSelectionAddToPreviewClick = viewModel::onItemSelectionAddToPreviewClick,
onScrollChanges = viewModel::onScrollChanges,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
Expand All @@ -41,12 +42,24 @@ internal fun FolderCardGrid(
folders: List<FolderCardState>,
title: String,
modifier: Modifier = Modifier,
initialFirstVisibleItemIndex: Int = 0,
initialFirstVisibleItemScrollOffset: Int = 0,
contentPadding: PaddingValues = PaddingValues(),
onCLick: (String) -> Unit = {},
onLongCLick: (String) -> Unit = {},
onScrollChanges: (Int, Int) -> Unit = { _, _ -> },
) {
val lazyListState = rememberLazyGridState(
initialFirstVisibleItemIndex = initialFirstVisibleItemIndex,
initialFirstVisibleItemScrollOffset = initialFirstVisibleItemScrollOffset,
)
val getFirstVisibleItemValues = {
lazyListState.firstVisibleItemIndex to lazyListState.firstVisibleItemScrollOffset
}
OnScrollChanges(getFirstVisibleItemValues, onScrollChanges)
LazyVerticalGrid(
modifier = modifier,
state = lazyListState,
columns = GridCells.Adaptive(minSize = 320.dp),
verticalArrangement = Arrangement.spacedBy(24.dp),
horizontalArrangement = Arrangement.spacedBy(32.dp),
Expand Down Expand Up @@ -85,6 +98,18 @@ internal fun FolderCardGrid(
}
}

@Composable
private fun OnScrollChanges(
getFirstVisibleItemValues: () -> Pair<Int, Int>,
onScrollChanges: (Int, Int) -> Unit
) {
val (firstVisibleItemIndex, firstVisibleItemScrollOffset) = getFirstVisibleItemValues()
onScrollChanges(
firstVisibleItemIndex,
firstVisibleItemScrollOffset
)
}

@Preview
@Composable
private fun FolderCardGridPreview() = Window {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import br.alexandregpereira.hunter.folder.list.FolderListState

Expand All @@ -32,14 +33,18 @@ internal fun FolderListScreen(
onItemSelectionClose: () -> Unit = {},
onItemSelectionDeleteClick: () -> Unit = {},
onItemSelectionAddToPreviewClick: () -> Unit = {},
onScrollChanges: (Int, Int) -> Unit = { _, _ -> },
) {
Box(Modifier.fillMaxSize()) {
FolderCardGrid(
folders = state.folders,
title = state.strings.title,
initialFirstVisibleItemIndex = remember { state.firstVisibleItemIndex },
initialFirstVisibleItemScrollOffset = remember { state.firstVisibleItemScrollOffset },
contentPadding = contentPadding,
onCLick = onCLick,
onLongCLick = onLongCLick
onLongCLick = onLongCLick,
onScrollChanges = onScrollChanges,
)

ItemSelection(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ data class FolderListState(
val strings: FolderListStrings = FolderListEmptyStrings(),
val isItemSelectionOpen: Boolean = false,
val itemSelectionCount: Int = 0,
val firstVisibleItemIndex: Int = 0,
val firstVisibleItemScrollOffset: Int = 0,
) {

internal val itemSelection: Set<String> = folders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ class FolderListStateHolder internal constructor(
.launchIn(scope)
}

fun onScrollChanges(firstVisibleItemIndex: Int, firstVisibleItemScrollOffset: Int) {
setState {
copy(
firstVisibleItemIndex = firstVisibleItemIndex,
firstVisibleItemScrollOffset = firstVisibleItemScrollOffset,
)
}
}

fun onItemSelect(folderName: String) {
analytics.trackItemSelect(folderName)
setState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package br.alexandregpereira.hunter.monster.compendium.ui

import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,39 @@ package br.alexandregpereira.hunter.search
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import br.alexandregpereira.hunter.search.ui.SearchScreen
import org.koin.compose.koinInject

@Composable
fun SearchScreenFeature(
contentPadding: PaddingValues = PaddingValues(),
) {
val viewModel: SearchStateHolder = koinInject()
val stateHolder: SearchStateHolder = koinInject()
val state by stateHolder.state.collectAsState()

val initialFirstVisibleItemIndex = remember { state.firstVisibleItemIndex }
val initialFirstVisibleItemScrollOffset = remember { state.firstVisibleItemScrollOffset }
val initialSearchKeysScrollOffset = remember { state.searchKeysScrollOffset }

SearchScreen(
state = viewModel.state.collectAsState().value,
searchValue = state.searchValue,
monsterRows = state.monsterRows,
searchLabel = state.searchLabel,
searchResults = state.searchResults,
isSearching = state.isSearching,
searchKeys = state.searchKeys,
initialFirstVisibleItemIndex = initialFirstVisibleItemIndex,
initialFirstVisibleItemScrollOffset = initialFirstVisibleItemScrollOffset,
initialSearchKeysScrollOffset = initialSearchKeysScrollOffset,
contentPaddingValues = contentPadding,
onSearchValueChange = viewModel::onSearchValueChange,
onCardClick = viewModel::onItemClick,
onCardLongClick = viewModel::onItemLongClick,
onSearchKeyClick = viewModel::onSearchKeyClick,
onAddClick = viewModel::onAddClick,
onSearchValueChange = stateHolder::onSearchValueChange,
onCardClick = stateHolder::onItemClick,
onCardLongClick = stateHolder::onItemLongClick,
onSearchKeyClick = stateHolder::onSearchKeyClick,
onAddClick = stateHolder::onAddClick,
onScrollChanges = stateHolder::onScrollChanges,
onSearchKeysScrollChanges = stateHolder::onSearchKeysScrollChanges,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,17 @@ internal class SearchStateHolder(
val monsterIndexes = state.value.monsterRows.map { it.index }
folderPreviewEventDispatcher.dispatchEvent(AddMonster(monsterIndexes))
}

fun onScrollChanges(firstVisibleItemIndex: Int, firstVisibleItemScrollOffset: Int) {
setState {
copy(
firstVisibleItemIndex = firstVisibleItemIndex,
firstVisibleItemScrollOffset = firstVisibleItemScrollOffset
)
}
}

fun onSearchKeysScrollChanges(scrollOffset: Int) {
setState { copy(searchKeysScrollOffset = scrollOffset) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ import br.alexandregpereira.hunter.ui.compose.AppButtonSize
@Composable
internal fun SearchKeyButtons(
searchKeys: List<SearchKeyState>,
initialScrollOffset: Int,
shouldShow: () -> Boolean,
modifier: Modifier = Modifier,
onClick: (Int) -> Unit,
onScrollChanges: (Int) -> Unit,
) {
val scrollState = rememberScrollState()
val scrollState = rememberScrollState(initial = initialScrollOffset)
onScrollChanges(
getScrollPosition = { scrollState.value },
onScrollChanges = onScrollChanges,
)
AnimatedVisibility(
visible = shouldShow(),
enter = fadeIn(),
Expand All @@ -41,6 +47,14 @@ internal fun SearchKeyButtons(
}
}

@Composable
private fun onScrollChanges(
getScrollPosition: () -> Int,
onScrollChanges: (Int) -> Unit,
) {
onScrollChanges(getScrollPosition())
}

@Composable
internal fun SearchKeyButton(
searchKey: SearchKeyState,
Expand Down
Loading

0 comments on commit d0fe059

Please sign in to comment.