Skip to content

Commit 043c170

Browse files
committed
Treat decorations better
1 parent 9256945 commit 043c170

2 files changed

Lines changed: 79 additions & 48 deletions

File tree

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressMenu.kt

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ import androidx.compose.material3.Text
2929
import androidx.compose.material3.rememberModalBottomSheetState
3030
import androidx.compose.material3.rememberStandardBottomSheetState
3131
import androidx.compose.runtime.Composable
32+
import androidx.compose.runtime.DisposableEffect
3233
import androidx.compose.ui.Alignment
3334
import androidx.compose.ui.Modifier
3435
import androidx.compose.ui.draw.clip
3536
import androidx.compose.ui.graphics.Color
3637
import androidx.compose.ui.platform.LocalContext
3738
import androidx.compose.ui.res.painterResource
39+
import androidx.compose.ui.res.stringResource
3840
import androidx.compose.ui.tooling.preview.Preview
3941
import androidx.compose.ui.tooling.preview.PreviewParameter
4042
import androidx.compose.ui.tooling.preview.datasource.CollectionPreviewParameterProvider
@@ -130,51 +132,76 @@ fun LongPressMenuHeader(
130132
)
131133
}
132134

133-
item.playlistSize?.let { playlistSize ->
134-
Surface(
135-
color = Color.Black.copy(alpha = 0.6f),
136-
contentColor = Color.White,
137-
modifier = Modifier
138-
.align(Alignment.TopEnd)
139-
.height(thumbnailHeight)
140-
.width(40.dp)
141-
.clip(MaterialTheme.shapes.large),
142-
) {
143-
Column(
144-
verticalArrangement = Arrangement.Center,
145-
horizontalAlignment = Alignment.CenterHorizontally,
146-
modifier = Modifier.fillMaxWidth(),
147-
) {
148-
Icon(
149-
Icons.AutoMirrored.Default.PlaylistPlay,
150-
contentDescription = null,
151-
)
152-
Text(
153-
text = Localization.localizeStreamCountMini(ctx, playlistSize),
154-
style = MaterialTheme.typography.labelMedium,
155-
maxLines = 1,
156-
)
135+
when (val decoration = item.decoration) {
136+
is LongPressableDecoration.Duration -> {
137+
// only show duration if there is a thumbnail
138+
if (item.thumbnailUrl != null) {
139+
Surface(
140+
color = Color.Black.copy(alpha = 0.6f),
141+
contentColor = Color.White,
142+
modifier = Modifier
143+
.align(Alignment.BottomEnd)
144+
.padding(4.dp)
145+
.clip(MaterialTheme.shapes.medium),
146+
) {
147+
Text(
148+
text = Localization.getDurationString(decoration.duration),
149+
modifier = Modifier.padding(vertical = 2.dp, horizontal = 4.dp)
150+
)
151+
}
152+
}
153+
}
154+
is LongPressableDecoration.Live -> {
155+
// only show "Live" if there is a thumbnail
156+
if (item.thumbnailUrl != null) {
157+
Surface(
158+
color = Color.Red.copy(alpha = 0.6f),
159+
contentColor = Color.White,
160+
modifier = Modifier
161+
.align(Alignment.BottomEnd)
162+
.padding(4.dp)
163+
.clip(MaterialTheme.shapes.medium),
164+
) {
165+
Text(
166+
text = stringResource(R.string.duration_live).uppercase(),
167+
modifier = Modifier.padding(vertical = 2.dp, horizontal = 4.dp)
168+
)
169+
}
157170
}
158171
}
159-
}
160172

161-
item.duration?.takeIf { it >= 0 }?.let { duration ->
162-
// only show duration if there is a thumbnail and there is no playlist header
163-
if (item.thumbnailUrl != null && item.playlistSize == null) {
173+
is LongPressableDecoration.Playlist -> {
164174
Surface(
165175
color = Color.Black.copy(alpha = 0.6f),
166176
contentColor = Color.White,
167177
modifier = Modifier
168-
.align(Alignment.BottomEnd)
169-
.padding(4.dp)
170-
.clip(MaterialTheme.shapes.medium),
178+
.align(Alignment.TopEnd)
179+
.height(thumbnailHeight)
180+
.width(40.dp)
181+
.clip(MaterialTheme.shapes.large),
171182
) {
172-
Text(
173-
text = Localization.getDurationString(duration),
174-
modifier = Modifier.padding(vertical = 2.dp, horizontal = 3.dp)
175-
)
183+
Column(
184+
verticalArrangement = Arrangement.Center,
185+
horizontalAlignment = Alignment.CenterHorizontally,
186+
modifier = Modifier.fillMaxWidth(),
187+
) {
188+
Icon(
189+
Icons.AutoMirrored.Default.PlaylistPlay,
190+
contentDescription = null,
191+
)
192+
Text(
193+
text = Localization.localizeStreamCountMini(
194+
ctx,
195+
decoration.itemCount
196+
),
197+
style = MaterialTheme.typography.labelMedium,
198+
maxLines = 1,
199+
)
200+
}
176201
}
177202
}
203+
204+
null -> {}
178205
}
179206
}
180207

@@ -229,8 +256,7 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
229256
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
230257
override val viewCount: Long = 8765432
231258
override val uploadDate: Either<String, OffsetDateTime> = Either.left("16 years ago")
232-
override val playlistSize: Long = 12
233-
override val duration: Long = 500
259+
override val decoration: LongPressableDecoration = LongPressableDecoration.Playlist(12)
234260

235261
override fun getPlayQueue(): PlayQueue {
236262
return SinglePlayQueue(listOf(), 0)
@@ -244,8 +270,7 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
244270
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
245271
override val viewCount: Long = 8765432
246272
override val uploadDate: Either<String, OffsetDateTime> = Either.left("16 years ago")
247-
override val playlistSize: Long? = null
248-
override val duration: Long = 500
273+
override val decoration: LongPressableDecoration = LongPressableDecoration.Duration(500)
249274

250275
override fun getPlayQueue(): PlayQueue {
251276
return SinglePlayQueue(listOf(), 0)
@@ -259,8 +284,7 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
259284
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
260285
override val viewCount: Long? = null
261286
override val uploadDate: Either<String, OffsetDateTime>? = null
262-
override val playlistSize: Long? = null
263-
override val duration: Long? = null
287+
override val decoration: LongPressableDecoration? = null
264288

265289
override fun getPlayQueue(): PlayQueue {
266290
return SinglePlayQueue(listOf(), 0)
@@ -275,8 +299,7 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
275299
override val uploaderUrl: String? = null
276300
override val viewCount: Long? = null
277301
override val uploadDate: Either<String, OffsetDateTime>? = null
278-
override val playlistSize: Long? = null
279-
override val duration: Long = 500
302+
override val decoration: LongPressableDecoration = LongPressableDecoration.Live
280303

281304
override fun getPlayQueue(): PlayQueue {
282305
return SinglePlayQueue(listOf(), 0)
@@ -290,8 +313,7 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
290313
override val uploaderUrl: String? = null
291314
override val viewCount: Long? = null
292315
override val uploadDate: Either<String, OffsetDateTime> = Either.right(OffsetDateTime.now().minusSeconds(12))
293-
override val playlistSize: Long = 1500
294-
override val duration: Long = 500
316+
override val decoration: LongPressableDecoration = LongPressableDecoration.Playlist(1500)
295317

296318
override fun getPlayQueue(): PlayQueue {
297319
return SinglePlayQueue(listOf(), 0)
@@ -306,8 +328,12 @@ private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPre
306328
private fun LongPressMenuPreview(
307329
@PreviewParameter(LongPressablePreviews::class) longPressable: LongPressable
308330
) {
331+
DisposableEffect(null) {
332+
Localization.initPrettyTime(Localization.resolvePrettyTime())
333+
onDispose {}
334+
}
309335
LongPressMenu(
310-
longPressable = LongPressablePreviews().values.toList()[4],
336+
longPressable = longPressable,
311337
onDismissRequest = {},
312338
sheetState = rememberStandardBottomSheetState(), // makes it start out as open
313339
)

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressable.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import org.schabi.newpipe.player.playqueue.PlayQueue
55
import org.schabi.newpipe.util.Either
66
import java.time.OffsetDateTime
77

8+
sealed interface LongPressableDecoration {
9+
data class Duration(val duration: Long) : LongPressableDecoration
10+
data object Live : LongPressableDecoration
11+
data class Playlist(val itemCount: Long) : LongPressableDecoration
12+
}
13+
814
@Stable
915
interface LongPressable {
1016
val title: String
@@ -14,8 +20,7 @@ interface LongPressable {
1420
val uploaderUrl: String?
1521
val viewCount: Long?
1622
val uploadDate: Either<String, OffsetDateTime>?
17-
val playlistSize: Long? // null if this is not a playlist
18-
val duration: Long?
23+
val decoration: LongPressableDecoration?
1924

2025
fun getPlayQueue(): PlayQueue
2126
}

0 commit comments

Comments
 (0)