Tweak code

This commit is contained in:
vfsfitvnm 2022-10-04 21:45:40 +02:00
parent 047231e389
commit 5e6787567e
14 changed files with 148 additions and 94 deletions

View file

@ -45,7 +45,6 @@ import it.vfsfitvnm.vimusic.models.Playlist
import it.vfsfitvnm.vimusic.models.SongPlaylistMap
import it.vfsfitvnm.vimusic.query
import it.vfsfitvnm.vimusic.transaction
import it.vfsfitvnm.vimusic.ui.components.LocalMenuState
import it.vfsfitvnm.vimusic.ui.screens.albumRoute
import it.vfsfitvnm.vimusic.ui.screens.artistRoute
import it.vfsfitvnm.vimusic.ui.screens.viewPlaylistsRoute
@ -62,9 +61,9 @@ import kotlinx.coroutines.flow.flowOf
@ExperimentalAnimationApi
@Composable
fun InFavoritesMediaItemMenu(
onDismiss: () -> Unit,
song: DetailedSong,
modifier: Modifier = Modifier,
onDismiss: (() -> Unit)? = null
) {
NonQueuedMediaItemMenu(
mediaItem = song.asMediaItem,
@ -81,11 +80,10 @@ fun InFavoritesMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun InHistoryMediaItemMenu(
onDismiss: () -> Unit,
song: DetailedSong,
modifier: Modifier = Modifier,
onDismiss: (() -> Unit)? = null
modifier: Modifier = Modifier
) {
val menuState = LocalMenuState.current
val binder = LocalPlayerServiceBinder.current
var isHiding by remember {
@ -97,7 +95,7 @@ fun InHistoryMediaItemMenu(
text = "Do you really hide this song? Its playback time and cache will be wiped.\nThis action is irreversible.",
onDismiss = { isHiding = false },
onConfirm = {
(onDismiss ?: menuState::hide).invoke()
onDismiss()
query {
// Not sure we can to this here
binder?.cache?.removeResource(song.id)
@ -118,11 +116,11 @@ fun InHistoryMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun InPlaylistMediaItemMenu(
onDismiss: () -> Unit,
playlistId: Long,
positionInPlaylist: Int,
song: DetailedSong,
modifier: Modifier = Modifier,
onDismiss: (() -> Unit)? = null
modifier: Modifier = Modifier
) {
NonQueuedMediaItemMenu(
mediaItem = song.asMediaItem,
@ -140,19 +138,18 @@ fun InPlaylistMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun NonQueuedMediaItemMenu(
onDismiss: () -> Unit,
mediaItem: MediaItem,
modifier: Modifier = Modifier,
onDismiss: (() -> Unit)? = null,
onRemoveFromPlaylist: (() -> Unit)? = null,
onHideFromDatabase: (() -> Unit)? = null,
onRemoveFromFavorites: (() -> Unit)? = null,
) {
val menuState = LocalMenuState.current
val binder = LocalPlayerServiceBinder.current
BaseMediaItemMenu(
mediaItem = mediaItem,
onDismiss = onDismiss ?: menuState::hide,
onDismiss = onDismiss,
onStartRadio = {
binder?.stopRadio()
binder?.player?.forcePlay(mediaItem)
@ -175,17 +172,16 @@ fun NonQueuedMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun QueuedMediaItemMenu(
onDismiss: () -> Unit,
mediaItem: MediaItem,
indexInQueue: Int?,
modifier: Modifier = Modifier,
onDismiss: (() -> Unit)? = null
modifier: Modifier = Modifier
) {
val menuState = LocalMenuState.current
val binder = LocalPlayerServiceBinder.current
BaseMediaItemMenu(
mediaItem = mediaItem,
onDismiss = onDismiss ?: menuState::hide,
onDismiss = onDismiss,
onRemoveFromQueue = if (indexInQueue != null) ({
binder?.player?.removeMediaItem(indexInQueue)
}) else null,
@ -196,11 +192,11 @@ fun QueuedMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun BaseMediaItemMenu(
mediaItem: MediaItem,
onDismiss: () -> Unit,
mediaItem: MediaItem,
modifier: Modifier = Modifier,
onGoToEqualizer: (() -> Unit)? = null,
onSetSleepTimer: (() -> Unit)? = null,
onShowSleepTimer: (() -> Unit)? = null,
onStartRadio: (() -> Unit)? = null,
onPlayNext: (() -> Unit)? = null,
onEnqueue: (() -> Unit)? = null,
@ -215,7 +211,7 @@ fun BaseMediaItemMenu(
mediaItem = mediaItem,
onDismiss = onDismiss,
onGoToEqualizer = onGoToEqualizer,
onSetSleepTimer = onSetSleepTimer,
onShowSleepTimer = onShowSleepTimer,
onStartRadio = onStartRadio,
onPlayNext = onPlayNext,
onEnqueue = onEnqueue,
@ -256,11 +252,11 @@ fun BaseMediaItemMenu(
@ExperimentalAnimationApi
@Composable
fun MediaItemMenu(
mediaItem: MediaItem,
onDismiss: () -> Unit,
mediaItem: MediaItem,
modifier: Modifier = Modifier,
onGoToEqualizer: (() -> Unit)? = null,
onSetSleepTimer: (() -> Unit)? = null,
onShowSleepTimer: (() -> Unit)? = null,
onStartRadio: (() -> Unit)? = null,
onPlayNext: (() -> Unit)? = null,
onEnqueue: (() -> Unit)? = null,
@ -413,7 +409,8 @@ fun MediaItemMenu(
)
}
onSetSleepTimer?.let {
// TODO: find solution to this shit
onShowSleepTimer?.let {
val binder = LocalPlayerServiceBinder.current
val (colorPalette, typography) = LocalAppearance.current
@ -432,17 +429,15 @@ fun MediaItemMenu(
confirmText = "Stop",
onDismiss = {
isShowingSleepTimerDialog = false
onDismiss()
},
onConfirm = {
binder?.cancelSleepTimer()
onDismiss()
}
)
} else {
DefaultDialog(
onDismiss = {
isShowingSleepTimerDialog = false
}
) {
DefaultDialog(onDismiss = { isShowingSleepTimerDialog = false }) {
var amount by remember {
mutableStateOf(1)
}
@ -514,7 +509,10 @@ fun MediaItemMenu(
) {
DialogTextButton(
text = "Cancel",
onClick = { isShowingSleepTimerDialog = false }
onClick = {
isShowingSleepTimerDialog = false
onDismiss()
}
)
DialogTextButton(
@ -523,6 +521,7 @@ fun MediaItemMenu(
onClick = {
binder?.startSleepTimer(amount * 10 * 60 * 1000L)
isShowingSleepTimerDialog = false
onDismiss()
}
)
}

View file

@ -117,7 +117,10 @@ fun AlbumSongs(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {

View file

@ -102,12 +102,18 @@ fun ArtistLocalSongs(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {
binder?.stopRadio()
binder?.player?.forcePlayAtIndex(songs.map(DetailedSong::asMediaItem), index)
binder?.player?.forcePlayAtIndex(
songs.map(DetailedSong::asMediaItem),
index
)
}
)
)

View file

@ -125,7 +125,10 @@ fun ArtistOverview(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {

View file

@ -284,7 +284,10 @@ fun ArtistScreen(browseId: String) {
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {

View file

@ -116,8 +116,14 @@ fun BuiltInPlaylistSongs(builtInPlaylist: BuiltInPlaylist) {
onLongClick = {
menuState.display {
when (builtInPlaylist) {
BuiltInPlaylist.Favorites -> InFavoritesMediaItemMenu(song = song)
BuiltInPlaylist.Offline -> InHistoryMediaItemMenu(song = song)
BuiltInPlaylist.Favorites -> InFavoritesMediaItemMenu(
song = song,
onDismiss = menuState::hide
)
BuiltInPlaylist.Offline -> InHistoryMediaItemMenu(
song = song,
onDismiss = menuState::hide
)
}
}
},

View file

@ -168,12 +168,18 @@ fun HomeSongs() {
.combinedClickable(
onLongClick = {
menuState.display {
InHistoryMediaItemMenu(song = song)
InHistoryMediaItemMenu(
song = song,
onDismiss = menuState::hide
)
}
},
onClick = {
binder?.stopRadio()
binder?.player?.forcePlayAtIndex(items.map(DetailedSong::asMediaItem), index)
binder?.player?.forcePlayAtIndex(
items.map(DetailedSong::asMediaItem),
index
)
}
)
.animateItemPlacement()

View file

@ -138,7 +138,10 @@ fun QuickPicks(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {
@ -168,7 +171,10 @@ fun QuickPicks(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {

View file

@ -172,13 +172,13 @@ fun LocalPlaylistSongs(
transaction {
runBlocking(Dispatchers.IO) {
withContext(Dispatchers.IO) {
Innertube.playlistPage(BrowseBody(browseId = browseId))?.completed()
Innertube.playlistPage(BrowseBody(browseId = browseId))
?.completed()
}
}?.getOrNull()?.let { remotePlaylist ->
Database.clearPlaylist(playlistId)
remotePlaylist.
songsPage
remotePlaylist.songsPage
?.items
?.map(Innertube.SongItem::asMediaItem)
?.onEach(Database::insert)
@ -240,12 +240,14 @@ fun LocalPlaylistSongs(
InPlaylistMediaItemMenu(
playlistId = playlistId,
positionInPlaylist = index,
song = song
song = song,
onDismiss = menuState::hide
)
}
},
onClick = {
playlistWithSongs?.songs?.map(DetailedSong::asMediaItem)
playlistWithSongs?.songs
?.map(DetailedSong::asMediaItem)
?.let { mediaItems ->
binder?.stopRadio()
binder?.player?.forcePlayAtIndex(mediaItems, index)

View file

@ -210,7 +210,8 @@ fun PlayerBottomSheet(
menuState.display {
QueuedMediaItemMenu(
mediaItem = window.mediaItem,
indexInQueue = if (isPlayingThisMediaItem) null else window.firstPeriodIndex
indexInQueue = if (isPlayingThisMediaItem) null else window.firstPeriodIndex,
onDismiss = menuState::hide
)
}
},
@ -268,7 +269,10 @@ fun PlayerBottomSheet(
text = "${windows.size} songs",
style = typography.xxs.medium,
modifier = Modifier
.background(color = colorPalette.background1, shape = RoundedCornerShape(16.dp))
.background(
color = colorPalette.background1,
shape = RoundedCornerShape(16.dp)
)
.align(Alignment.CenterStart)
.padding(all = 8.dp)
)

View file

@ -44,11 +44,13 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import coil.compose.AsyncImage
import it.vfsfitvnm.route.OnGlobalRoute
import it.vfsfitvnm.vimusic.LocalPlayerServiceBinder
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.service.PlayerService
import it.vfsfitvnm.vimusic.ui.components.BottomSheet
import it.vfsfitvnm.vimusic.ui.components.BottomSheetState
import it.vfsfitvnm.vimusic.ui.components.LocalMenuState
@ -219,7 +221,10 @@ fun PlayerView(
}
val paddingValues = WindowInsets.navigationBars.asPaddingValues()
val playerBottomSheetState = rememberBottomSheetState(64.dp + paddingValues.calculateBottomPadding(), layoutState.expandedBound)
val playerBottomSheetState = rememberBottomSheetState(
64.dp + paddingValues.calculateBottomPadding(),
layoutState.expandedBound
)
when (configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> {
@ -265,6 +270,7 @@ fun PlayerView(
)
}
}
else -> {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
@ -325,50 +331,10 @@ fun PlayerView(
color = colorPalette.text,
onClick = {
menuState.display {
val resultRegistryOwner =
LocalActivityResultRegistryOwner.current
BaseMediaItemMenu(
PlayerMenu(
onDismiss = menuState::hide,
mediaItem = mediaItem,
onStartRadio = {
binder.stopRadio()
binder.player.seamlessPlay(mediaItem)
binder.setupRadio(
NavigationEndpoint.Endpoint.Watch(videoId = mediaItem.mediaId)
)
},
onGoToEqualizer = {
val intent =
Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL).apply {
putExtra(
AudioEffect.EXTRA_AUDIO_SESSION,
binder.player.audioSessionId
)
putExtra(
AudioEffect.EXTRA_PACKAGE_NAME,
context.packageName
)
putExtra(
AudioEffect.EXTRA_CONTENT_TYPE,
AudioEffect.CONTENT_TYPE_MUSIC
)
}
if (intent.resolveActivity(context.packageManager) != null) {
val contract =
ActivityResultContracts.StartActivityForResult()
resultRegistryOwner?.activityResultRegistry
?.register("", contract) {}
?.launch(intent)
} else {
Toast
.makeText(context, "No equalizer app found!", Toast.LENGTH_SHORT)
.show()
}
},
onSetSleepTimer = {},
onDismiss = menuState::hide
binder = binder
)
}
},
@ -389,3 +355,41 @@ fun PlayerView(
)
}
}
@ExperimentalAnimationApi
@Composable
private fun PlayerMenu(
binder: PlayerService.Binder,
mediaItem: MediaItem,
onDismiss: () -> Unit
) {
val context = LocalContext.current
val resultRegistryOwner = LocalActivityResultRegistryOwner.current
BaseMediaItemMenu(
mediaItem = mediaItem,
onStartRadio = {
binder.stopRadio()
binder.player.seamlessPlay(mediaItem)
binder.setupRadio(NavigationEndpoint.Endpoint.Watch(videoId = mediaItem.mediaId))
},
onGoToEqualizer = {
val intent = Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL).apply {
putExtra(AudioEffect.EXTRA_AUDIO_SESSION, binder.player.audioSessionId)
putExtra(AudioEffect.EXTRA_PACKAGE_NAME, context.packageName)
putExtra(AudioEffect.EXTRA_CONTENT_TYPE, AudioEffect.CONTENT_TYPE_MUSIC)
}
if (intent.resolveActivity(context.packageManager) != null) {
val contract = ActivityResultContracts.StartActivityForResult()
resultRegistryOwner?.activityResultRegistry
?.register("", contract) {}?.launch(intent)
} else {
Toast.makeText(context, "No equalizer app found!", Toast.LENGTH_SHORT).show()
}
},
onShowSleepTimer = {},
onDismiss = onDismiss
)
}

View file

@ -199,7 +199,10 @@ fun PlaylistSongList(
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {

View file

@ -90,7 +90,7 @@ fun LocalSongSearch(
actionsContent = {
if (textFieldValue.text.isNotEmpty()) {
SecondaryTextButton(
text = "Clear",
text = "Clear",
onClick = { onTextFieldValueChanged(TextFieldValue()) }
)
}
@ -110,7 +110,10 @@ fun LocalSongSearch(
.combinedClickable(
onLongClick = {
menuState.display {
InHistoryMediaItemMenu(song = song)
InHistoryMediaItemMenu(
song = song,
onDismiss = menuState::hide
)
}
},
onClick = {

View file

@ -124,7 +124,10 @@ fun SearchResultScreen(query: String, onSearchAgain: () -> Unit) {
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = song.asMediaItem)
NonQueuedMediaItemMenu(
onDismiss = menuState::hide,
mediaItem = song.asMediaItem,
)
}
},
onClick = {
@ -246,7 +249,10 @@ fun SearchResultScreen(query: String, onSearchAgain: () -> Unit) {
.combinedClickable(
onLongClick = {
menuState.display {
NonQueuedMediaItemMenu(mediaItem = video.asMediaItem)
NonQueuedMediaItemMenu(
mediaItem = video.asMediaItem,
onDismiss = menuState::hide
)
}
},
onClick = {