Skip to content

Commit

Permalink
Scroll to start of folder preview when monster size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandregpereira committed Aug 16, 2024
1 parent c372330 commit 7dea1d9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 androidx.compose.ui.Modifier
import br.alexandregpereira.hunter.folder.preview.ui.FolderPreviewScreen
import org.koin.compose.koinInject
Expand All @@ -34,6 +35,7 @@ fun FolderPreviewFeature(

FolderPreviewScreen(
state = state,
actionHandler = remember(stateHolder) { stateHolder },
contentPadding = contentPadding,
onClick = stateHolder::onItemClick,
onLongClick = stateHolder::onItemLongClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
Expand All @@ -43,6 +45,7 @@ fun FolderPreview(
monsters: List<MonsterFolderPreview>,
saveButtonText: String,
modifier: Modifier = Modifier,
lazyListState: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(),
onClick: (index: String) -> Unit = {},
onLongClick: (index: String) -> Unit = {},
Expand All @@ -53,6 +56,7 @@ fun FolderPreview(
Row {
LazyRow(
modifier = Modifier.fillMaxWidth(0.7f),
state = lazyListState,
horizontalArrangement = Arrangement.spacedBy(16.dp),
contentPadding = PaddingValues(horizontal = 16.dp)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import br.alexandregpereira.hunter.folder.preview.FolderPreviewAction
import br.alexandregpereira.hunter.folder.preview.FolderPreviewState
import br.alexandregpereira.hunter.state.ActionHandler
import kotlinx.coroutines.flow.collectLatest

@Composable
internal fun FolderPreviewScreen(
state: FolderPreviewState,
actionHandler: ActionHandler<FolderPreviewAction>,
contentPadding: PaddingValues = PaddingValues(),
modifier: Modifier = Modifier,
onClick: (index: String) -> Unit = {},
Expand All @@ -43,8 +49,21 @@ internal fun FolderPreviewScreen(
exit = fadeOut(animationSpec = spring()),
modifier = modifier
) {
val lazyListState = rememberLazyListState()

LaunchedEffect(lazyListState, actionHandler) {
actionHandler.action.collectLatest { event ->
when (event) {
is FolderPreviewAction.ScrollToStart -> {
lazyListState.animateScrollToItem(0)
}
}
}
}

FolderPreview(
monsters = state.monsters,
lazyListState = lazyListState,
saveButtonText = state.strings.save,
contentPadding = contentPadding,
onClick = onClick,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package br.alexandregpereira.hunter.folder.preview

sealed class FolderPreviewAction {
data object ScrollToStart : FolderPreviewAction()
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import br.alexandregpereira.hunter.localization.AppLocalization
import br.alexandregpereira.hunter.monster.event.MonsterEvent.OnVisibilityChanges.Show
import br.alexandregpereira.hunter.monster.event.MonsterEventDispatcher
import br.alexandregpereira.hunter.monster.event.collectOnMonsterCompendiumChanges
import br.alexandregpereira.hunter.state.MutableActionHandler
import br.alexandregpereira.hunter.state.UiModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
Expand All @@ -49,7 +51,8 @@ class FolderPreviewStateHolder internal constructor(
private val dispatcher: CoroutineDispatcher,
private val analytics: FolderPreviewAnalytics,
private val appLocalization: AppLocalization,
) : UiModel<FolderPreviewState>(FolderPreviewState()) {
) : UiModel<FolderPreviewState>(FolderPreviewState()),
MutableActionHandler<FolderPreviewAction> by MutableActionHandler() {

init {
observeEvents()
Expand Down Expand Up @@ -113,7 +116,12 @@ class FolderPreviewStateHolder internal constructor(
addMonsterToFolderPreview(index)
.flowOn(dispatcher)
.onEach { monsters ->
val previousMonsterListSize = state.value.monsters.size
setState { changeMonsters(monsters) }
if (monsters.size > previousMonsterListSize) {
delay(300)
sendAction(FolderPreviewAction.ScrollToStart)
}
}
.launchIn(scope)
}
Expand Down

0 comments on commit 7dea1d9

Please sign in to comment.