Skip to content

Commit

Permalink
(All) Let use-case setups invoke recomposition #9
Browse files Browse the repository at this point in the history
Also removes the previous standard behavior that a use-case is reset when it's done.
  • Loading branch information
maxkeppeler committed Dec 6, 2022
1 parent 3d15518 commit f0fc3f3
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import com.maxkeppeker.sheets.core.models.base.rememberSheetState
import com.maxkeppeler.sheets.calendar.CalendarView
Expand All @@ -40,7 +41,19 @@ fun BottomSheetSample(
) {
val coroutine = rememberCoroutineScope()
val hideBottomSheet = { coroutine.launch { state.animateTo(ModalBottomSheetValue.Hidden) } }
val dialogSheetState = rememberSheetState(visible = true, onCloseRequest = { hideBottomSheet() })
val dialogSheetState =
rememberSheetState(visible = true, onCloseRequest = { hideBottomSheet(); })

LaunchedEffect(state.currentValue) {
when (state.currentValue) {
ModalBottomSheetValue.Hidden,
ModalBottomSheetValue.Expanded -> {
dialogSheetState.invokeReset() // Manually reset internal state if required
}
ModalBottomSheetValue.HalfExpanded -> Unit

}
}

ModalBottomSheetLayout(
content = screenContent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ internal fun rememberCalendarState(
selection: CalendarSelection,
config: CalendarConfig,
): CalendarState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = CalendarState.Saver(selection, config),
init = { CalendarState(selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ internal fun rememberClockState(
selection: ClockSelection,
config: ClockConfig,
): ClockState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = ClockState.Saver(context, selection, config),
init = { ClockState(context, selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ internal fun rememberColorState(
selection: ColorSelection,
config: ColorConfig,
): ColorState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = ColorState.Saver(context, selection, config),
init = { ColorState(context, selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fun StateHandler(
sheetState: SheetState,
baseState: BaseTypeState,
) {
DisposableEffect(sheetState.visible, sheetState.reset) {
if (!sheetState.visible || sheetState.reset) {
DisposableEffect(sheetState.reset) {
if (sheetState.reset) {
baseState.reset()
sheetState.clearReset()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class SheetState(
*/
fun hide() {
visible = false
reset = true
onDismissRequest?.invoke(this)
onCloseRequest?.invoke(this)
}
Expand All @@ -63,13 +62,15 @@ class SheetState(
reset = false
}

private fun invokeReset() {
/**
* Reset the current state data.
*/
fun invokeReset() {
reset = true
}

// Closed
internal fun dismiss() {
invokeReset()
if (!embedded) visible = false
onDismissRequest?.invoke(this)
onCloseRequest?.invoke(this)
Expand All @@ -85,9 +86,7 @@ class SheetState(
The parent container (Dialog, PopUp, BottomSheet)
can be hidden with the use-case view.
*/
invokeReset()
if (!embedded) visible = false

onFinishedRequest?.invoke(this)
onCloseRequest?.invoke(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ internal fun rememberDateTimeState(
selection: DateTimeSelection,
config: DateTimeConfig,
): DateTimeState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = DateTimeState.Saver(selection, config),
init = { DateTimeState(selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ internal fun rememberDurationState(
selection: DurationSelection,
config: DurationConfig,
): DurationState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = DurationState.Saver(selection, config),
init = { DurationState(selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ internal fun rememberEmojiState(
selection: EmojiSelection,
config: EmojiConfig
): EmojiState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = EmojiState.Saver(selection, config),
init = { EmojiState(selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ internal class InputState(
internal fun rememberInputState(
selection: InputSelection,
): InputState = rememberSaveable(
inputs = arrayOf(selection),
saver = InputState.Saver(selection),
init = { InputState(selection) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ internal fun rememberListState(
selection: ListSelection,
config: ListConfig
): ListState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = ListState.Saver(selection, config),
init = { ListState(selection, config) }
)
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ internal fun rememberOptionState(
selection: OptionSelection,
config: OptionConfig
): OptionState = rememberSaveable(
inputs = arrayOf(selection, config),
saver = OptionState.Saver(selection, config),
init = { OptionState(selection, config) }
)

0 comments on commit f0fc3f3

Please sign in to comment.