Make delegateSnapshotUpdate accept two lambda
This commit is contained in:
1 parent
733937ec74
commit
b0ab1f0f2d
5 files changed
+27
-44
No files matched your search
@@ -129,10 +129,10 @@ fun FabLayout(
|
||||
}
|
||||
val builder by rememberUpdatedState(fabBuilder)
|
||||
|
||||
val secondaryFab by delegateSnapshotUpdate {
|
||||
record { buildFab(builder) }
|
||||
transform { onEachLatest { state.collapse(MutatePriority.PreventUserInput) } }
|
||||
}
|
||||
val secondaryFab by delegateSnapshotUpdate(
|
||||
record = { buildFab(builder) },
|
||||
transform = { onEachLatest { state.collapse(MutatePriority.PreventUserInput) } },
|
||||
)
|
||||
|
||||
val density = LocalDensity.current
|
||||
val interval = remember(density) { with(density) { FabInterval.roundToPx() } }
|
||||
|
||||
@@ -633,15 +633,14 @@ fun AnimatedVisibilityScope.DownloadsScreen(navigator: DestinationsNavigator) =
|
||||
}
|
||||
}
|
||||
|
||||
val hideFab by delegateSnapshotUpdate {
|
||||
record { fabHidden }
|
||||
transform {
|
||||
// Bug: IDE failed to inference 'hide's type
|
||||
onEachLatest { hide: Boolean ->
|
||||
val hideFab by delegateSnapshotUpdate(
|
||||
record = { fabHidden },
|
||||
transform = {
|
||||
onEachLatest { hide ->
|
||||
if (!hide) delay(FAB_ANIMATE_TIME.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
FabLayout(
|
||||
hidden = hideFab && !selectMode,
|
||||
|
||||
@@ -331,15 +331,14 @@ fun AnimatedVisibilityScope.FavouritesScreen(navigator: DestinationsNavigator) =
|
||||
)
|
||||
}
|
||||
|
||||
val hideFab by delegateSnapshotUpdate {
|
||||
record { fabHidden }
|
||||
transform {
|
||||
// Bug: IDE failed to inference 'hide's type
|
||||
val hideFab by delegateSnapshotUpdate(
|
||||
record = { fabHidden },
|
||||
transform = {
|
||||
onEachLatest { hide: Boolean ->
|
||||
if (!hide) delay(FAB_ANIMATE_TIME.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
FabLayout(
|
||||
hidden = hideFab && !selectMode,
|
||||
|
||||
@@ -595,15 +595,14 @@ fun AnimatedVisibilityScope.GalleryListScreen(lub: ListUrlBuilder, navigator: De
|
||||
val invalidNum = stringResource(R.string.error_invalid_number)
|
||||
val outOfRange = stringResource(R.string.error_out_of_range)
|
||||
|
||||
val hideFab by delegateSnapshotUpdate {
|
||||
record { fabHidden }
|
||||
transform {
|
||||
// Bug: IDE failed to inference 'hide's type
|
||||
val hideFab by delegateSnapshotUpdate(
|
||||
record = { fabHidden },
|
||||
transform = {
|
||||
onEachLatest { hide: Boolean ->
|
||||
if (!hide) delay(FAB_ANIMATE_TIME.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
FabLayout(
|
||||
hidden = hideFab,
|
||||
|
||||
@@ -1,32 +1,18 @@
|
||||
package com.hippo.ehviewer.ui.tools
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.snapshotFlow
|
||||
import androidx.compose.runtime.snapshots.Snapshot
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface RecordSnapshotScope<T : Any> {
|
||||
fun record(block: () -> T)
|
||||
fun transform(block: Flow<T>.() -> Flow<T>)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun <T : Any> delegateSnapshotUpdate(dsl: RecordSnapshotScope<T>.() -> Unit): State<T> {
|
||||
val state = remember(dsl) {
|
||||
object : RecordSnapshotScope<T> {
|
||||
lateinit var flow: Flow<T>
|
||||
lateinit var initial: T
|
||||
override fun record(block: () -> T) {
|
||||
initial = Snapshot.withoutReadObservation(block)
|
||||
flow = snapshotFlow(block)
|
||||
}
|
||||
override fun transform(block: Flow<T>.() -> Flow<T>) {
|
||||
flow = block(flow)
|
||||
}
|
||||
}.apply(dsl)
|
||||
}
|
||||
return with(state) { flow.collectAsState(initial) }
|
||||
fun <T : Any> delegateSnapshotUpdate(
|
||||
record: () -> T,
|
||||
transform: Flow<T>.() -> Flow<T> = { this },
|
||||
) = remember(transform, record) {
|
||||
Snapshot.withoutReadObservation(record) to transform(snapshotFlow(record))
|
||||
}.let { (initial, flow) ->
|
||||
flow.collectAsState(initial)
|
||||
}
|
||||
Reference in new issue
Block a user