From 06a5828bbebb7b00f35d61af901a7435cb04189b Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sun, 28 Jul 2024 09:46:21 +0530 Subject: [PATCH] Improved stream components --- .../ui/components/stream/StreamCardItem.kt | 2 +- .../ui/components/stream/StreamGridItem.kt | 11 +++---- .../ui/components/stream/StreamList.kt | 10 +++++-- .../ui/components/stream/StreamListItem.kt | 2 +- .../ui/components/stream/StreamMenu.kt | 29 ++++++++++++++++--- .../ui/components/stream/StreamThumbnail.kt | 15 ++++++++-- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamCardItem.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamCardItem.kt index 14c074568..13914f4c2 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamCardItem.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamCardItem.kt @@ -69,7 +69,7 @@ fun StreamCardItem( } if (isSelected) { - StreamMenu(onDismissPopup) + StreamMenu(stream, onDismissPopup) } } } diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamGridItem.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamGridItem.kt index 7190986e9..dad8699f4 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamGridItem.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamGridItem.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter +import androidx.compose.ui.unit.DpSize import androidx.compose.ui.unit.dp import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.ui.theme.AppTheme @@ -24,6 +25,7 @@ import org.schabi.newpipe.ui.theme.AppTheme fun StreamGridItem( stream: StreamInfoItem, isSelected: Boolean = false, + isMini: Boolean = false, onClick: (StreamInfoItem) -> Unit = {}, onLongClick: (StreamInfoItem) -> Unit = {}, onDismissPopup: () -> Unit = {} @@ -37,10 +39,9 @@ fun StreamGridItem( ) .padding(12.dp) ) { - StreamThumbnail( - modifier = Modifier.size(width = 246.dp, height = 138.dp), - stream = stream - ) + val size = if (isMini) DpSize(150.dp, 85.dp) else DpSize(246.dp, 138.dp) + + StreamThumbnail(modifier = Modifier.size(size), stream = stream) Text( text = stream.name, @@ -58,7 +59,7 @@ fun StreamGridItem( } if (isSelected) { - StreamMenu(onDismissPopup) + StreamMenu(stream, onDismissPopup) } } } diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamList.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamList.kt index 21a9cc137..d4745943a 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamList.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamList.kt @@ -7,6 +7,7 @@ import androidx.compose.foundation.lazy.grid.LazyGridScope import androidx.compose.foundation.lazy.grid.LazyVerticalGrid import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf @@ -16,6 +17,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp import androidx.fragment.app.FragmentActivity import androidx.paging.compose.LazyPagingItems +import androidx.window.core.layout.WindowWidthSizeClass import my.nanihadesuka.compose.LazyColumnScrollbar import my.nanihadesuka.compose.LazyVerticalGridScrollbar import org.schabi.newpipe.extractor.stream.StreamInfoItem @@ -58,13 +60,17 @@ fun StreamList( val gridState = rememberLazyGridState() LazyVerticalGridScrollbar(state = gridState) { - LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(250.dp)) { + val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass + val isCompact = windowSizeClass.windowWidthSizeClass == WindowWidthSizeClass.COMPACT + val minSize = if (isCompact) 150.dp else 250.dp + + LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(minSize)) { gridHeader() items(streams.itemCount) { val stream = streams[it]!! StreamGridItem( - stream, selectedStream == stream, onClick, onLongClick, + stream, selectedStream == stream, isCompact, onClick, onLongClick, onDismissPopup ) } diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamListItem.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamListItem.kt index 04efbddd4..7f8739283 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamListItem.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamListItem.kt @@ -67,7 +67,7 @@ fun StreamListItem( } if (isSelected) { - StreamMenu(onDismissPopup) + StreamMenu(stream, onDismissPopup) } } } diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamMenu.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamMenu.kt index a6c4135e0..71f8776f5 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamMenu.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamMenu.kt @@ -4,11 +4,24 @@ import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.fragment.app.FragmentActivity import org.schabi.newpipe.R +import org.schabi.newpipe.download.DownloadDialog +import org.schabi.newpipe.extractor.stream.StreamInfo +import org.schabi.newpipe.extractor.stream.StreamInfoItem +import org.schabi.newpipe.util.SparseItemUtil +import org.schabi.newpipe.util.external_communication.ShareUtils @Composable -fun StreamMenu(onDismissRequest: () -> Unit) { +fun StreamMenu( + stream: StreamInfoItem, + onDismissRequest: () -> Unit +) { + val context = LocalContext.current + + // TODO: Implement remaining click actions DropdownMenu(expanded = true, onDismissRequest = onDismissRequest) { DropdownMenuItem( text = { Text(text = stringResource(R.string.start_here_on_background)) }, @@ -20,7 +33,15 @@ fun StreamMenu(onDismissRequest: () -> Unit) { ) DropdownMenuItem( text = { Text(text = stringResource(R.string.download)) }, - onClick = onDismissRequest + onClick = { + SparseItemUtil.fetchStreamInfoAndSaveToDatabase( + context, stream.serviceId, stream.url + ) { info: StreamInfo -> + val downloadDialog = DownloadDialog(context, info) + val fragmentManager = (context as FragmentActivity).supportFragmentManager + downloadDialog.show(fragmentManager, "downloadDialog") + } + } ) DropdownMenuItem( text = { Text(text = stringResource(R.string.add_to_playlist)) }, @@ -28,11 +49,11 @@ fun StreamMenu(onDismissRequest: () -> Unit) { ) DropdownMenuItem( text = { Text(text = stringResource(R.string.share)) }, - onClick = onDismissRequest + onClick = { ShareUtils.shareText(context, stream.name, stream.url, stream.thumbnails) } ) DropdownMenuItem( text = { Text(text = stringResource(R.string.open_in_browser)) }, - onClick = onDismissRequest + onClick = { ShareUtils.openUrlInBrowser(context, stream.url) } ) DropdownMenuItem( text = { Text(text = stringResource(R.string.mark_as_watched)) }, diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamThumbnail.kt b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamThumbnail.kt index 2d0d1353c..00b8b94e0 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamThumbnail.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/stream/StreamThumbnail.kt @@ -11,11 +11,13 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import coil.compose.AsyncImage import org.schabi.newpipe.R import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.util.Localization +import org.schabi.newpipe.util.StreamTypeUtil import org.schabi.newpipe.util.image.ImageStrategy @Composable @@ -34,12 +36,19 @@ fun StreamThumbnail( modifier = modifier ) + val isLive = StreamTypeUtil.isLiveStream(stream.streamType) + val background = if (isLive) Color.Red else Color.Black Text( - text = Localization.getDurationString(stream.duration), + text = if (isLive) { + stringResource(R.string.duration_live) + } else { + Localization.getDurationString(stream.duration) + }, color = Color.White, style = MaterialTheme.typography.bodySmall, - modifier = Modifier.padding(2.dp) - .background(Color.Black.copy(alpha = 0.5f)) + modifier = Modifier + .padding(2.dp) + .background(background.copy(alpha = 0.5f)) ) } }