Skip to content

Commit

Permalink
make language item display the current language and make language men…
Browse files Browse the repository at this point in the history
…u accessible in audio player
  • Loading branch information
theScrabi committed Oct 17, 2024
1 parent 14cf795 commit ae9fbda
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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 = {
Expand All @@ -200,6 +199,13 @@ private fun Menu(viewModel: InternalNewPlayerViewModel, uiState: NewPlayerUIStat
})
}
}
LanguageMenu(
uiState = uiState,
viewModel = viewModel,
isVisible = showLanguageMenu,
makeInvisible = {
showLanguageMenu = false
})
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* NewPlayer
*
* @author Christian Schabesberger
*
* Copyright (C) NewPipe e.V. 2024 <code(at)newpipe-ev.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,7 +84,7 @@ internal fun VideoPlayerMenu(viewModel: InternalNewPlayerViewModel, uiState: New
else
EmbeddedUiConfig.DUMMY

val availableLanguages = TrackUtils.getAvailableLanguages(uiState.currentlyAvailableTracks)


Box {
IconButton(onClick = {
Expand Down Expand Up @@ -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
})
}
}

///////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion new-player/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<string name="audio_mode">Audio Mode</string>
<string name="menu_item_fit_screen">Fit screen</string>
<string name="menu_item_sub_titles">Subtitles</string>
<string name="menu_item_language">Language</string>
<string name="menu_selected_language_item">Selected language: %s</string>
<string name="menu_item_playback_speed">Playback speed</string>
<string name="widget_description_previous_stream">Previous stream</string>
<string name="widget_description_fast_rewind">Fast rewind</string>
Expand Down

0 comments on commit ae9fbda

Please sign in to comment.