1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-08 12:02:33 +01:00

Retain draggable state across onDrop changes

This commit is contained in:
Allan Wang 2023-06-22 03:06:19 -07:00
parent 7f33ae44f6
commit cdabd4e378
No known key found for this signature in database
GPG Key ID: C93E3F9C679D7A56
2 changed files with 12 additions and 3 deletions

View File

@ -63,7 +63,9 @@ class FrostApp : Application() {
)
}
MainScope().launch { setup() }
MainScope().launch {
// setup()
}
}
private suspend fun setup() {

View File

@ -18,6 +18,7 @@ package com.pitchedapps.frost.compose.draggable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
@ -35,7 +36,12 @@ fun interface OnDrop<T> {
/** Create draggable state, which will store and create all target states. */
@Composable
fun <T> rememberDraggableState(onDrop: OnDrop<T>): DraggableState<T> {
return remember(onDrop) { DraggableStateImpl(onDrop) }
// State must be remembered without keys, or else updates will clear draggable data
val state = remember { DraggableStateImpl(onDrop) }
LaunchedEffect(onDrop) { state.onDrop = onDrop }
return state
}
/**
@ -114,7 +120,7 @@ interface DropTargetState<T> {
var bounds: Rect
}
private class DraggableStateImpl<T>(private val onDrop: OnDrop<T>) : DraggableState<T> {
private class DraggableStateImpl<T>(var onDrop: OnDrop<T>) : DraggableState<T> {
override var windowPosition: Offset by mutableStateOf(Offset.Zero)
@ -241,6 +247,7 @@ private class DragTargetStateImpl<T>(
if (!isDragging) return
draggableState.activeDragTargets.remove(key)
draggableState.cleanUpDrag(this)
isDragging = false
}
}