Skip to content

Commit

Permalink
Fix selection order in embedded picker by #1603
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 18, 2025
1 parent 9380f50 commit 5ac0d0e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ private val BASE64_PATTERN = Regex(

inline fun <reified T, reified R> T.cast(): R = this as R

inline fun <reified T, reified R> T.safeCast(): R? = this as? R


inline operator fun CharSequence.times(
count: Int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/

package ru.tech.imageresizershrinker.core.ui.utils.helper

import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.util.fastRoundToInt

@Composable
fun Dp.toSp(): TextUnit = with(LocalDensity.current) { toSp() }

@Composable
fun Dp.toPx(): Float = with(LocalDensity.current) { toPx() }

@Composable
fun Dp.roundToPx(): Int = toPx().fastRoundToInt()

@Composable
fun TextUnit.toPx(): Float = with(LocalDensity.current) { toPx() }

@Composable
fun TextUnit.roundToPx(): Int = toPx().fastRoundToInt()

@Composable
fun Int.toDp(): Dp = with(LocalDensity.current) { toDp() }

@Composable
fun Int.toSp(): TextUnit = toDp().toSp()

@Composable
fun Float.toDp(): Dp = with(LocalDensity.current) { toDp() }

@Composable
fun Float.toSp(): TextUnit = toDp().toSp()
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.domain.utils.safeCast
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.ui.utils.helper.toPx
import ru.tech.imageresizershrinker.core.ui.widget.modifier.dragHandler
import ru.tech.imageresizershrinker.feature.media_picker.domain.model.Media
import ru.tech.imageresizershrinker.feature.media_picker.domain.model.MediaItem
Expand Down Expand Up @@ -142,16 +143,20 @@ internal fun MediaPickerGrid(
isVertical = true,
haptics = LocalHapticFeedback.current,
selectedItems = privateSelection,
onSelectionChange = {
val data = state.mappedMedia.mapIndexedNotNull { index, mediaItem ->
if (index in it && mediaItem is MediaItem.MediaViewItem) mediaItem.media
else null
onSelectionChange = { indices ->
val order: MutableList<Any> = indices.toMutableList()
state.mappedMedia.forEachIndexed { index, mediaItem ->
if (index in indices && mediaItem is MediaItem.MediaViewItem) {
order.indexOf(index).takeIf { it >= 0 }?.let {
order[it] = mediaItem.media
}
}
}
selectedMedia.clear()
selectedMedia.addAll(data)
selectedMedia.addAll(order.mapNotNull { it.safeCast() })
},
autoScrollSpeed = autoScrollSpeed,
autoScrollThreshold = with(LocalDensity.current) { 40.dp.toPx() },
autoScrollThreshold = 40.dp.toPx(),
onLongTap = {
if (selectedMedia.isEmpty()) {
imagePreviewUri =
Expand Down Expand Up @@ -234,6 +239,12 @@ internal fun MediaPickerGrid(
}

is MediaItem.MediaViewItem -> {
val selectionIndex by remember(selectedMedia, item.media) {
derivedStateOf {
selectedMedia.indexOf(item.media)
}
}

MediaImage(
media = item.media,
canClick = !isSelectionOfAll || !allowMultiple,
Expand All @@ -246,18 +257,8 @@ internal fun MediaPickerGrid(
onItemLongClick = {
imagePreviewUri = it.uri
},
selectionIndex = remember(selectedMedia, item.media) {
derivedStateOf {
if (selectedMedia.size > 1) {
selectedMedia.indexOf(item.media)
} else -1
}
}.value,
isSelected = remember(item, selectedMedia) {
derivedStateOf {
selectedMedia.contains(item.media)
}
}.value
selectionIndex = if (selectedMedia.size > 1) selectionIndex else -1,
isSelected = selectionIndex >= 0
)
}
}
Expand Down

0 comments on commit 5ac0d0e

Please sign in to comment.