1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-2236 Sharing extensions | Fix | Fix dropdown menu flickering (#913)

This commit is contained in:
Evgenii Kozlov 2024-02-23 15:59:34 +01:00 committed by uburoiubu
parent 751417a8b7
commit 86fb213ade
No known key found for this signature in database
GPG key ID: C8FB80E0A595FBB6
2 changed files with 58 additions and 5 deletions

View file

@ -44,6 +44,8 @@ import com.anytypeio.anytype.core_ui.views.ButtonSecondary
import com.anytypeio.anytype.core_ui.views.ButtonSize
import com.anytypeio.anytype.core_ui.views.Caption1Medium
import com.anytypeio.anytype.core_ui.views.TitleInter15
import com.anytypeio.anytype.core_utils.ui.MultipleEventCutter
import com.anytypeio.anytype.core_utils.ui.get
import com.anytypeio.anytype.presentation.sharing.AddToAnytypeViewModel.SpaceView
import com.anytypeio.anytype.presentation.spaces.SpaceIconView
@ -105,13 +107,20 @@ fun AddToAnytypeScreen(
}
Column(modifier = Modifier.fillMaxWidth()) {
val throttler = remember {
MultipleEventCutter.Companion.get(interval = DROPDOWN_MENU_VISIBILITY_WINDOW_INTERVAL)
}
Header()
DataSection(content)
Box(
modifier = Modifier
.fillMaxWidth()
.height(76.dp)
.noRippleClickable { isSaveAsMenuExpanded = !isSaveAsMenuExpanded }
.noRippleClickable {
throttler.processEvent {
isSaveAsMenuExpanded = !isSaveAsMenuExpanded
}
}
) {
Text(
text = stringResource(R.string.sharing_menu_save_as_section_name),
@ -139,7 +148,11 @@ fun AddToAnytypeScreen(
if (items.size > 1) {
DropdownMenu(
expanded = isSaveAsMenuExpanded,
onDismissRequest = { isSaveAsMenuExpanded = false },
onDismissRequest = {
throttler.processEvent {
isSaveAsMenuExpanded = false
}
},
modifier = Modifier.background(
color = colorResource(id = R.color.background_secondary)
)
@ -276,12 +289,17 @@ private fun CurrentSpaceSection(
onSelectSpaceClicked: (SpaceView) -> Unit
) {
var isSpaceSelectMenuExpanded by remember { mutableStateOf(false) }
val throttler = remember {
MultipleEventCutter.Companion.get(interval = DROPDOWN_MENU_VISIBILITY_WINDOW_INTERVAL)
}
Box(
modifier = Modifier
.fillMaxWidth()
.height(76.dp)
.noRippleClickable {
isSpaceSelectMenuExpanded = true
throttler.processEvent {
isSpaceSelectMenuExpanded = true
}
}
) {
Text(
@ -316,7 +334,11 @@ private fun CurrentSpaceSection(
)
DropdownMenu(
expanded = isSpaceSelectMenuExpanded,
onDismissRequest = { isSpaceSelectMenuExpanded = false },
onDismissRequest = {
throttler.processEvent {
isSpaceSelectMenuExpanded = false
}
},
modifier = Modifier.background(
color = colorResource(id = R.color.background_secondary)
)
@ -440,4 +462,6 @@ sealed class SharingData {
override val data: String
get() = uri
}
}
}
const val DROPDOWN_MENU_VISIBILITY_WINDOW_INTERVAL = 150L

View file

@ -38,4 +38,33 @@ fun View.setOnThrottleClickListener(
}
}
)
}
/**
* Can be used for throttling clicks in compose code
*/
interface MultipleEventCutter {
fun processEvent(event: () -> Unit)
companion object
}
fun MultipleEventCutter.Companion.get(
interval: Long = OnThrottleClickListener.DEFAULT_INTERVAL
): MultipleEventCutter = DefaultMultipleEventCutter(interval)
private class DefaultMultipleEventCutter(
private val interval: Long
) : MultipleEventCutter {
private val now: Long
get() = System.currentTimeMillis()
private var lastEventTimeMs: Long = 0
override fun processEvent(event: () -> Unit) {
if (now - lastEventTimeMs >= interval) {
event.invoke()
}
lastEventTimeMs = now
}
}