Skip to content

Commit

Permalink
Use state instead of effects for collapsing home bottom sheet on back…
Browse files Browse the repository at this point in the history
… click
  • Loading branch information
msasikanth committed Aug 6, 2024
1 parent 1a697c7 commit 5610344
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
Expand Down Expand Up @@ -138,7 +136,6 @@ class HomePresenter(
}

internal val state = presenterInstance.state
internal val effects = presenterInstance.effects.asSharedFlow()

init {
lifecycle.doOnCreate {
Expand Down Expand Up @@ -181,8 +178,6 @@ class HomePresenter(
initialValue = HomeState.DEFAULT
)

val effects = MutableSharedFlow<HomeEffect>()

init {
dispatch(HomeEvent.Init)
}
Expand Down Expand Up @@ -267,7 +262,9 @@ class HomePresenter(
}

private fun backClicked() {
coroutineScope.launch { effects.emit(HomeEffect.MinimizeSheet) }
coroutineScope.launch {
_state.update { it.copy(feedsSheetState = SheetValue.PartiallyExpanded) }
}
}

private fun init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import app.cash.paging.compose.collectAsLazyPagingItems
import dev.sasikanth.rss.reader.components.CompactFloatingActionButton
import dev.sasikanth.rss.reader.components.LocalDynamicColorState
import dev.sasikanth.rss.reader.feeds.ui.FeedsBottomSheet
import dev.sasikanth.rss.reader.home.HomeEffect
import dev.sasikanth.rss.reader.home.HomeEvent
import dev.sasikanth.rss.reader.home.HomePresenter
import dev.sasikanth.rss.reader.platform.LocalLinkHandler
Expand All @@ -79,7 +78,6 @@ import dev.sasikanth.rss.reader.resources.icons.TwineIcons
import dev.sasikanth.rss.reader.resources.strings.LocalStrings
import dev.sasikanth.rss.reader.ui.AppTheme
import dev.sasikanth.rss.reader.utils.inverse
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

internal val BOTTOM_SHEET_PEEK_HEIGHT = 96.dp
Expand Down Expand Up @@ -113,23 +111,22 @@ internal fun HomeScreen(homePresenter: HomePresenter, modifier: Modifier = Modif

val linkHandler = LocalLinkHandler.current

LaunchedEffect(Unit) {
homePresenter.effects.collectLatest { effect ->
when (effect) {
HomeEffect.MinimizeSheet -> {
bottomSheetState.partialExpand()
}
}
}
}

val bottomSheetProgress by bottomSheetState.progressAsState()
val showScrollToTop by remember { derivedStateOf { listState.firstVisibleItemIndex > 0 } }

val sheetPeekHeight =
BOTTOM_SHEET_PEEK_HEIGHT +
WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()

LaunchedEffect(state.feedsSheetState) {
if (
state.feedsSheetState == SheetValue.PartiallyExpanded &&
bottomSheetState.currentValue == SheetValue.Expanded
) {
bottomSheetState.partialExpand()
}
}

BottomSheetScaffold(
modifier = modifier,
scaffoldState = bottomSheetScaffoldState,
Expand Down

0 comments on commit 5610344

Please sign in to comment.