From ae9fbdae3f5098ac7535eafe79b364df50571a46 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Thu, 17 Oct 2024 19:14:58 +0200 Subject: [PATCH] make language item display the current language and make language menu accessible in audio player --- .../newplayer/ui/audioplayer/BottomUI.kt | 24 ++++-- .../newplayer/ui/common/LanguageMenu.kt | 83 +++++++++++++++++++ .../ui/videoplayer/controller/Menu.kt | 46 ++++------ new-player/src/main/res/values/strings.xml | 2 +- 4 files changed, 114 insertions(+), 41 deletions(-) create mode 100644 new-player/src/main/java/net/newpipe/newplayer/ui/common/LanguageMenu.kt diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/audioplayer/BottomUI.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/audioplayer/BottomUI.kt index 745169f5..4f76c0c2 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/audioplayer/BottomUI.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/audioplayer/BottomUI.kt @@ -54,6 +54,8 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.media3.common.util.UnstableApi import net.newpipe.newplayer.R +import net.newpipe.newplayer.ui.common.LanguageMenu +import net.newpipe.newplayer.ui.common.LanguageMenuItem import net.newpipe.newplayer.uiModel.EmbeddedUiConfig import net.newpipe.newplayer.uiModel.NewPlayerUIState import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel @@ -135,6 +137,7 @@ internal fun AudioBottomUI(viewModel: InternalNewPlayerViewModel, uiState: NewPl @Composable private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIState) { var showMenu: Boolean by remember { mutableStateOf(false) } + var showLanguageMenu: Boolean by remember { mutableStateOf(false) } val embeddedUiConfig = if (LocalContext.current is Activity) getEmbeddedUiConfig(activity = LocalContext.current as Activity) @@ -159,14 +162,10 @@ private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIStat ) }, onClick = { /*TODO*/ showMenu = false }) - DropdownMenuItem(text = { Text(stringResource(R.string.menu_item_language)) }, - leadingIcon = { - Icon( - imageVector = Icons.Filled.Translate, - contentDescription = stringResource(R.string.menu_item_language) - ) - }, - onClick = { /*TODO*/ showMenu = false }) + LanguageMenuItem(uiState = uiState, onClick = { + showLanguageMenu = true + showMenu = false + }) DropdownMenuItem(text = { Text(stringResource(R.string.menu_item_share_timestamp)) }, leadingIcon = { Icon( @@ -184,7 +183,7 @@ private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIStat }, onClick = { /*TODO*/ showMenu = false }) - if(supportsPip(LocalContext.current)) { + if (supportsPip(LocalContext.current)) { DropdownMenuItem( text = { Text(stringResource(R.string.pip_button_description)) }, onClick = { @@ -200,6 +199,13 @@ private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIStat }) } } + LanguageMenu( + uiState = uiState, + viewModel = viewModel, + isVisible = showLanguageMenu, + makeInvisible = { + showLanguageMenu = false + }) } } diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/common/LanguageMenu.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/common/LanguageMenu.kt new file mode 100644 index 00000000..0c1b52b7 --- /dev/null +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/common/LanguageMenu.kt @@ -0,0 +1,83 @@ +/* NewPlayer + * + * @author Christian Schabesberger + * + * Copyright (C) NewPipe e.V. 2024 + * + * NewPlayer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPlayer. If not, see . + */ + +package net.newpipe.newplayer.ui.common + +import androidx.annotation.OptIn +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Translate +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.Icon +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource +import androidx.media3.common.util.UnstableApi +import net.newpipe.newplayer.R +import net.newpipe.newplayer.logic.TrackUtils +import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel +import net.newpipe.newplayer.uiModel.NewPlayerUIState +import net.newpipe.newplayer.uiModel.UIModeState +import java.util.Locale + +@OptIn(UnstableApi::class) +@Composable +internal fun LanguageMenu(uiState: NewPlayerUIState, viewModel: InternalNewPlayerViewModel, isVisible: Boolean, makeInvisible: () -> Unit) { + val availableLanguages = TrackUtils.getAvailableLanguages(uiState.currentlyAvailableTracks) + + DropdownMenu(expanded = isVisible, onDismissRequest = { + makeInvisible() + viewModel.dialogVisible(false) + }) { + for (language in availableLanguages) { + val locale = Locale(language) + + DropdownMenuItem( + text = { + Text(locale.displayLanguage) + }, + onClick = { /*TODO*/ makeInvisible() + viewModel.dialogVisible(false) + }) + } + } +} + + +@OptIn(UnstableApi::class) +@Composable +internal fun LanguageMenuItem(uiState: NewPlayerUIState, onClick: () -> Unit) { + val availableLanguages = TrackUtils.getAvailableLanguages(uiState.currentlyAvailableTracks) + + if (2 <= availableLanguages.size) { + val language = TrackUtils.getAvailableLanguages(uiState.currentlyAvailableTracks)[0] + val locale = Locale(language) + DropdownMenuItem(text = { Text(locale.displayLanguage) }, + leadingIcon = { + Icon( + imageVector = Icons.Filled.Translate, + contentDescription = String.format( + stringResource(R.string.menu_selected_language_item), + locale.displayLanguage + ) + ) + }, onClick = onClick) + } +} \ No newline at end of file diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/controller/Menu.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/controller/Menu.kt index 6d72fe88..17dd0adb 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/controller/Menu.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/controller/Menu.kt @@ -55,6 +55,8 @@ import androidx.compose.ui.unit.dp import androidx.media3.common.util.UnstableApi import net.newpipe.newplayer.R import net.newpipe.newplayer.logic.TrackUtils +import net.newpipe.newplayer.ui.common.LanguageMenu +import net.newpipe.newplayer.ui.common.LanguageMenuItem import net.newpipe.newplayer.uiModel.EmbeddedUiConfig import net.newpipe.newplayer.uiModel.NewPlayerUIState import net.newpipe.newplayer.uiModel.InternalNewPlayerViewModel @@ -82,7 +84,7 @@ internal fun VideoPlayerMenu(viewModel: InternalNewPlayerViewModel, uiState: New else EmbeddedUiConfig.DUMMY - val availableLanguages = TrackUtils.getAvailableLanguages(uiState.currentlyAvailableTracks) + Box { IconButton(onClick = { @@ -167,39 +169,21 @@ internal fun VideoPlayerMenu(viewModel: InternalNewPlayerViewModel, uiState: New ) }, onClick = { /*TODO*/ showMainMenu = false }) - if (2 <= availableLanguages.size) { - DropdownMenuItem(text = { Text(stringResource(R.string.menu_item_language)) }, - leadingIcon = { - Icon( - imageVector = Icons.Filled.Translate, - contentDescription = stringResource(R.string.menu_item_language) - ) - }, - onClick = { - showLanguageMenu = true - showMainMenu = false - }) - } - } - DropdownMenu(expanded = showLanguageMenu, onDismissRequest = { - showLanguageMenu = false - viewModel.dialogVisible(false) - }) { - for (language in availableLanguages) { - val locale = Locale(language) - - DropdownMenuItem( - text = { - Text(locale.displayLanguage) - }, - onClick = { /*TODO*/ showLanguageMenu = false - viewModel.dialogVisible(false) - }) - } + LanguageMenuItem(uiState = uiState, onClick = { + showLanguageMenu = true + showMainMenu = false + }) } - } + LanguageMenu( + uiState = uiState, + viewModel = viewModel, + isVisible = showLanguageMenu, + makeInvisible = { + showLanguageMenu = false + }) + } } /////////////////////////////////////////////////////////////////// diff --git a/new-player/src/main/res/values/strings.xml b/new-player/src/main/res/values/strings.xml index db18cf5c..c795a6d0 100644 --- a/new-player/src/main/res/values/strings.xml +++ b/new-player/src/main/res/values/strings.xml @@ -27,7 +27,7 @@ Audio Mode Fit screen Subtitles - Language + Selected language: %s Playback speed Previous stream Fast rewind