Merge pull request #1831 from FunkyMuse/fix/manage_blocked_numbers
fix: manage blocked numbers selection and add ability to show names
This commit is contained in:
commit
78243c1ea2
4 changed files with 70 additions and 21 deletions
|
@ -15,6 +15,7 @@ import androidx.compose.runtime.remember
|
|||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple
|
||||
import com.simplemobiletools.commons.compose.extensions.onEventValue
|
||||
|
@ -30,9 +31,12 @@ import java.io.FileOutputStream
|
|||
import java.io.OutputStream
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ManageBlockedNumbersActivity : BaseSimpleActivity() {
|
||||
|
||||
|
@ -282,11 +286,22 @@ class ManageBlockedNumbersActivity : BaseSimpleActivity() {
|
|||
private val application: Application
|
||||
) : AndroidViewModel(application) {
|
||||
|
||||
private val _blockedNumbers = MutableStateFlow(application.getBlockedNumbers().toImmutableList())
|
||||
|
||||
private val _blockedNumbers: MutableStateFlow<ImmutableList<BlockedNumber>?> = MutableStateFlow(null)
|
||||
val blockedNumbers = _blockedNumbers.asStateFlow()
|
||||
|
||||
init {
|
||||
updateBlockedNumbers()
|
||||
}
|
||||
|
||||
fun updateBlockedNumbers() {
|
||||
_blockedNumbers.update { application.getBlockedNumbers().toImmutableList() }
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
application.getBlockedNumbersWithContact { list ->
|
||||
_blockedNumbers.update { list.toImmutableList() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.simplemobiletools.commons.compose.extensions
|
|||
|
||||
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.runtime.MutableState
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.hapticfeedback.HapticFeedback
|
||||
|
@ -15,10 +15,11 @@ internal fun Modifier.listDragHandlerLongKey(
|
|||
selectedIds: MutableState<Set<Long>>,
|
||||
autoScrollSpeed: MutableState<Float>,
|
||||
autoScrollThreshold: Float,
|
||||
dragUpdate: (Boolean) -> Unit
|
||||
dragUpdate: (Boolean) -> Unit,
|
||||
ids: List<Long>,
|
||||
isScrollingUp: Boolean
|
||||
) = pointerInput(Unit) {
|
||||
|
||||
|
||||
var initialKey: Long? = null
|
||||
var currentKey: Long? = null
|
||||
val onDragCancelAndEnd = {
|
||||
|
@ -52,11 +53,15 @@ internal fun Modifier.listDragHandlerLongKey(
|
|||
|
||||
lazyListState.itemKeyAtPosition(change.position)?.let { key ->
|
||||
if (currentKey != key) {
|
||||
selectedIds.value = selectedIds.value
|
||||
.minus(initialKey!!..currentKey!!)
|
||||
.minus(currentKey!!..initialKey!!)
|
||||
.plus(initialKey!!..key)
|
||||
.plus(key..initialKey!!)
|
||||
val toSelect = if (selectedIds.value.contains(key) && ids.isNotEmpty()) {
|
||||
val successor = ids.indexOf(key) + if (isScrollingUp) +1 else -1
|
||||
selectedIds.value
|
||||
.minus(ids[successor])
|
||||
} else {
|
||||
selectedIds.value + setOf(currentKey!!, key)
|
||||
}
|
||||
|
||||
selectedIds.value = toSelect
|
||||
currentKey = key
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +70,27 @@ internal fun Modifier.listDragHandlerLongKey(
|
|||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the lazy list is currently scrolling up.
|
||||
*/
|
||||
@Composable
|
||||
internal fun LazyListState.isScrollingUp(): Boolean {
|
||||
var previousIndex by remember(this) { mutableStateOf(firstVisibleItemIndex) }
|
||||
var previousScrollOffset by remember(this) { mutableStateOf(firstVisibleItemScrollOffset) }
|
||||
return remember(this) {
|
||||
derivedStateOf {
|
||||
if (previousIndex != firstVisibleItemIndex) {
|
||||
previousIndex > firstVisibleItemIndex
|
||||
} else {
|
||||
previousScrollOffset >= firstVisibleItemScrollOffset
|
||||
}.also {
|
||||
previousIndex = firstVisibleItemIndex
|
||||
previousScrollOffset = firstVisibleItemScrollOffset
|
||||
}
|
||||
}
|
||||
}.value
|
||||
}
|
||||
|
||||
internal fun LazyListState.itemKeyAtPosition(hitPoint: Offset): Long? =
|
||||
layoutInfo.visibleItemsInfo
|
||||
.firstOrNull { lazyListItemInfo ->
|
||||
|
|
|
@ -4,3 +4,7 @@ import androidx.compose.ui.Modifier
|
|||
|
||||
inline fun Modifier.ifTrue(predicate: Boolean, builder: () -> Modifier) =
|
||||
then(if (predicate) builder() else Modifier)
|
||||
|
||||
inline fun Modifier.ifFalse(predicate: Boolean, builder: () -> Modifier) =
|
||||
then(if (!predicate) builder() else Modifier)
|
||||
|
||||
|
|
|
@ -177,17 +177,21 @@ internal fun ManageBlockedNumbersScreen(
|
|||
}
|
||||
LazyColumn(
|
||||
state = state,
|
||||
modifier = Modifier.listDragHandlerLongKey(
|
||||
lazyListState = state,
|
||||
haptics = LocalHapticFeedback.current,
|
||||
selectedIds = selectedIds,
|
||||
autoScrollSpeed = autoScrollSpeed,
|
||||
autoScrollThreshold = with(LocalDensity.current) { 40.dp.toPx() },
|
||||
dragUpdate = { isDraggingStarted ->
|
||||
hasDraggingStarted = isDraggingStarted
|
||||
triggerReset = RESET_IMMEDIATELY
|
||||
}
|
||||
),
|
||||
modifier = Modifier.ifFalse(blockedNumbers.isNullOrEmpty()) {
|
||||
Modifier.listDragHandlerLongKey(
|
||||
isScrollingUp = state.isScrollingUp(),
|
||||
lazyListState = state,
|
||||
haptics = hapticFeedback,
|
||||
selectedIds = selectedIds,
|
||||
autoScrollSpeed = autoScrollSpeed,
|
||||
autoScrollThreshold = with(LocalDensity.current) { 40.dp.toPx() },
|
||||
dragUpdate = { isDraggingStarted ->
|
||||
hasDraggingStarted = isDraggingStarted
|
||||
triggerReset = RESET_IMMEDIATELY
|
||||
},
|
||||
ids = blockedNumbers?.map { blockedNumber -> blockedNumber.id }.orEmpty()
|
||||
)
|
||||
},
|
||||
verticalArrangement = Arrangement.spacedBy(2.dp),
|
||||
contentPadding = PaddingValues(bottom = paddingValues.calculateBottomPadding())
|
||||
) {
|
||||
|
|
Loading…
Reference in a new issue