Improve compose-reordering code
This commit is contained in:
@@ -35,8 +35,10 @@ import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.zIndex
|
||||
import it.vfsfitvnm.reordering.animateItemPlacement
|
||||
import it.vfsfitvnm.reordering.draggedItem
|
||||
import it.vfsfitvnm.reordering.rememberReorderingState
|
||||
import it.vfsfitvnm.reordering.verticalDragAfterLongPressToReorder
|
||||
import it.vfsfitvnm.reordering.verticalDragToReorder
|
||||
import it.vfsfitvnm.route.RouteHandler
|
||||
import it.vfsfitvnm.vimusic.Database
|
||||
import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder
|
||||
@@ -89,7 +91,44 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||
|
||||
val thumbnailSize = Dimensions.thumbnails.song.px
|
||||
|
||||
val reorderingState = rememberReorderingState(playlistWithSongs.songs)
|
||||
val reorderingState = rememberReorderingState(
|
||||
items = playlistWithSongs.songs,
|
||||
onDragStart = {
|
||||
hapticFeedback.performHapticFeedback(
|
||||
HapticFeedbackType.LongPress
|
||||
)
|
||||
},
|
||||
onDragEnd = { fromIndex, toIndex ->
|
||||
transaction {
|
||||
if (fromIndex > toIndex) {
|
||||
Database.incrementSongPositions(
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
fromPosition = toIndex,
|
||||
toPosition = fromIndex - 1
|
||||
)
|
||||
} else if (fromIndex < toIndex) {
|
||||
Database.decrementSongPositions(
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
fromPosition = fromIndex + 1,
|
||||
toPosition = toIndex
|
||||
)
|
||||
}
|
||||
|
||||
Database.update(
|
||||
SongPlaylistMap(
|
||||
songId = playlistWithSongs.songs[fromIndex].id,
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
position = toIndex
|
||||
)
|
||||
)
|
||||
}
|
||||
},
|
||||
itemSizeProvider = { index ->
|
||||
lazyListState.layoutInfo.visibleItemsInfo.find {
|
||||
it.index == index + 3
|
||||
}?.size
|
||||
}
|
||||
)
|
||||
|
||||
var isRenaming by rememberSaveable {
|
||||
mutableStateOf(false)
|
||||
@@ -99,9 +138,7 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||
TextFieldDialog(
|
||||
hintText = "Enter the playlist name",
|
||||
initialTextInput = playlistWithSongs.playlist.name,
|
||||
onDismiss = {
|
||||
isRenaming = false
|
||||
},
|
||||
onDismiss = { isRenaming = false },
|
||||
onDone = { text ->
|
||||
query {
|
||||
Database.update(playlistWithSongs.playlist.copy(name = text))
|
||||
@@ -117,9 +154,7 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||
if (isDeleting) {
|
||||
ConfirmationDialog(
|
||||
text = "Do you really want to delete this playlist?",
|
||||
onDismiss = {
|
||||
isDeleting = false
|
||||
},
|
||||
onDismiss = { isDeleting = false },
|
||||
onConfirm = {
|
||||
query {
|
||||
Database.delete(playlistWithSongs.playlist)
|
||||
@@ -131,7 +166,8 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||
|
||||
LazyColumn(
|
||||
state = lazyListState,
|
||||
contentPadding = WindowInsets.systemBars.asPaddingValues().add(bottom = Dimensions.collapsedPlayer),
|
||||
contentPadding = WindowInsets.systemBars.asPaddingValues()
|
||||
.add(bottom = Dimensions.collapsedPlayer),
|
||||
modifier = Modifier
|
||||
.background(colorPalette.background0)
|
||||
.fillMaxSize()
|
||||
@@ -274,47 +310,18 @@ fun LocalPlaylistScreen(playlistId: Long) {
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(colorPalette.textSecondary),
|
||||
modifier = Modifier
|
||||
.clickable {}
|
||||
.clickable { }
|
||||
.verticalDragToReorder(
|
||||
reorderingState = reorderingState,
|
||||
index = index
|
||||
)
|
||||
.padding(horizontal = 8.dp, vertical = 4.dp)
|
||||
.size(20.dp)
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
.animateItemPlacement()
|
||||
.verticalDragAfterLongPressToReorder(
|
||||
reorderingState = reorderingState,
|
||||
index = index,
|
||||
onDragStart = {
|
||||
hapticFeedback.performHapticFeedback(
|
||||
HapticFeedbackType.LongPress
|
||||
)
|
||||
},
|
||||
onDragEnd = { reachedIndex ->
|
||||
transaction {
|
||||
if (index > reachedIndex) {
|
||||
Database.incrementSongPositions(
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
fromPosition = reachedIndex,
|
||||
toPosition = index - 1
|
||||
)
|
||||
} else if (index < reachedIndex) {
|
||||
Database.decrementSongPositions(
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
fromPosition = index + 1,
|
||||
toPosition = reachedIndex
|
||||
)
|
||||
}
|
||||
|
||||
Database.update(
|
||||
SongPlaylistMap(
|
||||
songId = playlistWithSongs.songs[index].id,
|
||||
playlistId = playlistWithSongs.playlist.id,
|
||||
position = reachedIndex
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
.animateItemPlacement(reorderingState = reorderingState)
|
||||
.draggedItem(reorderingState = reorderingState, index = index)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package it.vfsfitvnm.vimusic.ui.views
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
@@ -36,8 +37,10 @@ import androidx.compose.ui.platform.LocalHapticFeedback
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.valentinilk.shimmer.shimmer
|
||||
import it.vfsfitvnm.reordering.animateItemPlacement
|
||||
import it.vfsfitvnm.reordering.draggedItem
|
||||
import it.vfsfitvnm.reordering.rememberReorderingState
|
||||
import it.vfsfitvnm.reordering.verticalDragAfterLongPressToReorder
|
||||
import it.vfsfitvnm.reordering.verticalDragToReorder
|
||||
import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder
|
||||
import it.vfsfitvnm.vimusic.R
|
||||
import it.vfsfitvnm.vimusic.enums.ThumbnailRoundness
|
||||
@@ -56,6 +59,7 @@ import it.vfsfitvnm.vimusic.utils.rememberShouldBePlaying
|
||||
import it.vfsfitvnm.vimusic.utils.rememberWindows
|
||||
import it.vfsfitvnm.vimusic.utils.shuffleQueue
|
||||
|
||||
@ExperimentalFoundationApi
|
||||
@ExperimentalAnimationApi
|
||||
@Composable
|
||||
fun CurrentPlaylistView(
|
||||
@@ -78,7 +82,22 @@ fun CurrentPlaylistView(
|
||||
val lazyListState =
|
||||
rememberLazyListState(initialFirstVisibleItemIndex = mediaItemIndex)
|
||||
|
||||
val reorderingState = rememberReorderingState(windows)
|
||||
val reorderingState = rememberReorderingState(
|
||||
items = windows,
|
||||
onDragStart = {
|
||||
hapticFeedback.performHapticFeedback(
|
||||
HapticFeedbackType.LongPress
|
||||
)
|
||||
},
|
||||
onDragEnd = { fromIndex, toIndex ->
|
||||
binder.player.moveMediaItem(fromIndex, toIndex)
|
||||
},
|
||||
itemSizeProvider = { index ->
|
||||
lazyListState.layoutInfo.visibleItemsInfo.find {
|
||||
it.index == index
|
||||
}?.size
|
||||
}
|
||||
)
|
||||
|
||||
val paddingValues = WindowInsets.systemBars.asPaddingValues()
|
||||
val bottomPadding = paddingValues.calculateBottomPadding()
|
||||
@@ -162,24 +181,20 @@ fun CurrentPlaylistView(
|
||||
contentDescription = null,
|
||||
colorFilter = ColorFilter.tint(colorPalette.textSecondary),
|
||||
modifier = Modifier
|
||||
.clickable {}
|
||||
.clickable { }
|
||||
.verticalDragToReorder(
|
||||
reorderingState = reorderingState,
|
||||
index = window.firstPeriodIndex
|
||||
)
|
||||
.padding(horizontal = 8.dp, vertical = 4.dp)
|
||||
.size(20.dp)
|
||||
)
|
||||
},
|
||||
modifier = Modifier
|
||||
// .animateItemPlacement()
|
||||
.verticalDragAfterLongPressToReorder(
|
||||
.animateItemPlacement(reorderingState)
|
||||
.draggedItem(
|
||||
reorderingState = reorderingState,
|
||||
index = window.firstPeriodIndex,
|
||||
onDragStart = {
|
||||
hapticFeedback.performHapticFeedback(
|
||||
HapticFeedbackType.LongPress
|
||||
)
|
||||
},
|
||||
onDragEnd = { reachedIndex ->
|
||||
binder.player.moveMediaItem(window.firstPeriodIndex, reachedIndex)
|
||||
}
|
||||
index = window.firstPeriodIndex
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package it.vfsfitvnm.vimusic.ui.views
|
||||
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.*
|
||||
@@ -17,6 +18,7 @@ import it.vfsfitvnm.vimusic.ui.components.BottomSheet
|
||||
import it.vfsfitvnm.vimusic.ui.components.BottomSheetState
|
||||
import it.vfsfitvnm.vimusic.ui.styling.LocalAppearance
|
||||
|
||||
@ExperimentalFoundationApi
|
||||
@ExperimentalAnimationApi
|
||||
@Composable
|
||||
fun PlayerBottomSheet(
|
||||
|
||||
@@ -7,6 +7,7 @@ import android.widget.Toast
|
||||
import androidx.activity.compose.LocalActivityResultRegistryOwner
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
@@ -72,6 +73,7 @@ import it.vfsfitvnm.vimusic.utils.thumbnail
|
||||
import it.vfsfitvnm.youtubemusic.models.NavigationEndpoint
|
||||
import kotlin.math.absoluteValue
|
||||
|
||||
@ExperimentalFoundationApi
|
||||
@ExperimentalAnimationApi
|
||||
@Composable
|
||||
fun PlayerView(
|
||||
|
||||
Reference in New Issue
Block a user