From d95ee702ad58beac9a314eb8b7475c953bb23388 Mon Sep 17 00:00:00 2001 From: oxy Date: Wed, 29 May 2024 22:32:42 +0800 Subject: [PATCH] Show current playlist source and type by TopBar subtitle text. --- .../java/com/m3u/androidApp/ui/Scaffold.kt | 8 +--- .../m3u/features/playlist/PlaylistScreen.kt | 48 ++++++++++++++----- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/androidApp/src/main/java/com/m3u/androidApp/ui/Scaffold.kt b/androidApp/src/main/java/com/m3u/androidApp/ui/Scaffold.kt index 9cc3faf51..8f3a2c237 100644 --- a/androidApp/src/main/java/com/m3u/androidApp/ui/Scaffold.kt +++ b/androidApp/src/main/java/com/m3u/androidApp/ui/Scaffold.kt @@ -159,9 +159,7 @@ internal fun MainContent( AnimatedContent( targetState = title, label = "app-scaffold-title", - transitionSpec = { - fadeIn() togetherWith fadeOut() - } + transitionSpec = { fadeIn() togetherWith fadeOut() } ) { title -> Text( text = title, @@ -170,9 +168,7 @@ internal fun MainContent( fontFamily = FontFamilies.LexendExa ) } - AnimatedVisibility( - visible = subtitle.text.isNotEmpty() - ) { + AnimatedVisibility(subtitle.text.isNotEmpty()) { Text( text = subtitle, style = MaterialTheme.typography.titleMedium, diff --git a/features/playlist/src/main/java/com/m3u/features/playlist/PlaylistScreen.kt b/features/playlist/src/main/java/com/m3u/features/playlist/PlaylistScreen.kt index 666a727b5..67ab46c0b 100644 --- a/features/playlist/src/main/java/com/m3u/features/playlist/PlaylistScreen.kt +++ b/features/playlist/src/main/java/com/m3u/features/playlist/PlaylistScreen.kt @@ -43,16 +43,20 @@ import androidx.compose.ui.text.withStyle import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.Lifecycle import androidx.lifecycle.compose.LifecycleResumeEffect +import androidx.lifecycle.compose.LocalLifecycleOwner import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.repeatOnLifecycle import com.google.accompanist.permissions.rememberPermissionState import com.m3u.core.architecture.preferences.hiltPreferences import com.m3u.core.util.basic.title import com.m3u.core.wrapper.Event import com.m3u.core.wrapper.eventOf +import com.m3u.data.database.model.DataSource import com.m3u.data.database.model.Programme import com.m3u.data.database.model.Stream import com.m3u.data.database.model.isSeries import com.m3u.data.database.model.isVod +import com.m3u.data.database.model.type import com.m3u.data.service.MediaCommand import com.m3u.features.playlist.internal.SmartphonePlaylistScreenImpl import com.m3u.features.playlist.internal.TvPlaylistScreenImpl @@ -70,10 +74,12 @@ import com.m3u.ui.Sort import com.m3u.ui.helper.Fob import com.m3u.ui.helper.LocalHelper import com.m3u.ui.helper.Metadata +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import kotlin.time.Duration.Companion.milliseconds import androidx.tv.material3.MaterialTheme as TvMaterialTheme @Composable @@ -88,6 +94,7 @@ internal fun PlaylistRoute( val helper = LocalHelper.current val coroutineScope = rememberCoroutineScope() val colorScheme = TvMaterialTheme.colorScheme + val lifecycleOwner = LocalLifecycleOwner.current val tv = isTelevision() @@ -122,34 +129,53 @@ internal fun PlaylistRoute( Manifest.permission.POST_NOTIFICATIONS ) - val title by remember { - derivedStateOf { playlist?.title?.title().orEmpty() } - } + val title by remember { derivedStateOf { playlist?.title?.title().orEmpty() } } val subtitle by remember { derivedStateOf { - buildAnnotatedString { + val spans = buildMap { + val typeWithSource = playlist.run { + when { + this == null || query.isNotEmpty() -> null + source == DataSource.Xtream -> "$source $type".uppercase() + else -> null + } + } + if (typeWithSource != null) { + put(typeWithSource, SpanStyle(color = colorScheme.secondary)) + } if (query.isNotEmpty()) { - withStyle( - SpanStyle( - color = colorScheme.primary - ) - ) { - append("\"$query\"") + put("\"$query\"", SpanStyle(color = colorScheme.primary)) + } + } + + buildAnnotatedString { + spans.entries.forEachIndexed { index, (text, span) -> + withStyle(span) { append(text) } + if (index != spans.entries.size - 1) { + append(" ") } } } } } + LifecycleResumeEffect( title, subtitle, colorScheme ) { Metadata.title = AnnotatedString(title) - Metadata.subtitle = subtitle + coroutineScope.launch { + lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) { + delay(400.milliseconds) + Metadata.subtitle = subtitle + } + } + Metadata.color = colorScheme.secondaryContainer Metadata.contentColor = colorScheme.onSecondaryContainer onPauseOrDispose { + Metadata.subtitle = AnnotatedString("") } }