Skip to content

Commit 62a400b

Browse files
committed
Make LongPressable a data class
1 parent f5c0e6a commit 62a400b

2 files changed

Lines changed: 83 additions & 122 deletions

File tree

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

Lines changed: 68 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ import androidx.compose.ui.unit.times
6969
import coil3.compose.AsyncImage
7070
import org.schabi.newpipe.R
7171
import org.schabi.newpipe.extractor.stream.StreamInfoItem
72-
import org.schabi.newpipe.player.playqueue.PlayQueue
73-
import org.schabi.newpipe.player.playqueue.SinglePlayQueue
7472
import org.schabi.newpipe.ui.theme.AppTheme
7573
import org.schabi.newpipe.ui.theme.customColors
7674
import org.schabi.newpipe.util.Either
@@ -80,36 +78,26 @@ import java.time.OffsetDateTime
8078

8179
fun getLongPressMenuView(
8280
context: Context,
83-
streamInfoItem: StreamInfoItem,
81+
item: StreamInfoItem,
8482
): ComposeView {
8583
return ComposeView(context).apply {
8684
setContent {
8785
LongPressMenu(
88-
longPressable = object : LongPressable {
89-
override val title: String = streamInfoItem.name
90-
override val url: String? = streamInfoItem.url?.takeIf { it.isNotBlank() }
91-
override val thumbnailUrl: String? =
92-
ImageStrategy.choosePreferredImage(streamInfoItem.thumbnails)
93-
override val uploader: String? =
94-
streamInfoItem.uploaderName?.takeIf { it.isNotBlank() }
95-
override val uploaderUrl: String? =
96-
streamInfoItem.uploaderUrl?.takeIf { it.isNotBlank() }
97-
override val viewCount: Long? =
98-
streamInfoItem.viewCount.takeIf { it >= 0 }
99-
override val uploadDate: Either<String, OffsetDateTime>? =
100-
streamInfoItem.uploadDate?.let { Either.right(it.offsetDateTime()) }
101-
?: streamInfoItem.textualUploadDate?.let { Either.left(it) }
102-
override val decoration: LongPressableDecoration? =
103-
streamInfoItem.duration.takeIf { it >= 0 }?.let {
104-
LongPressableDecoration.Duration(it)
105-
}
106-
107-
override fun getPlayQueue(): PlayQueue {
108-
TODO("Not yet implemented")
109-
}
110-
},
86+
longPressable = LongPressable(
87+
title = item.name,
88+
url = item.url?.takeIf { it.isNotBlank() },
89+
thumbnailUrl = ImageStrategy.choosePreferredImage(item.thumbnails),
90+
uploader = item.uploaderName?.takeIf { it.isNotBlank() },
91+
uploaderUrl = item.uploaderUrl?.takeIf { it.isNotBlank() },
92+
viewCount = item.viewCount.takeIf { it >= 0 },
93+
uploadDate = item.uploadDate?.let { Either.right(it.offsetDateTime()) }
94+
?: item.textualUploadDate?.let { Either.left(it) },
95+
decoration = item.duration.takeIf { it >= 0 }?.let {
96+
LongPressable.Decoration.Duration(it)
97+
},
98+
),
11199
onDismissRequest = { (this.parent as ViewGroup).removeView(this) },
112-
actions = LongPressAction.buildActionList(streamInfoItem, false),
100+
actions = LongPressAction.buildActionList(item, false),
113101
onEditActions = {},
114102
)
115103
}
@@ -245,7 +233,7 @@ fun LongPressMenuHeader(
245233
}
246234

247235
when (val decoration = item.decoration) {
248-
is LongPressableDecoration.Duration -> {
236+
is LongPressable.Decoration.Duration -> {
249237
// only show duration if there is a thumbnail
250238
if (item.thumbnailUrl != null) {
251239
Surface(
@@ -263,7 +251,7 @@ fun LongPressMenuHeader(
263251
}
264252
}
265253
}
266-
is LongPressableDecoration.Live -> {
254+
is LongPressable.Decoration.Live -> {
267255
// only show "Live" if there is a thumbnail
268256
if (item.thumbnailUrl != null) {
269257
Surface(
@@ -282,7 +270,7 @@ fun LongPressMenuHeader(
282270
}
283271
}
284272

285-
is LongPressableDecoration.Playlist -> {
273+
is LongPressable.Decoration.Playlist -> {
286274
Surface(
287275
color = Color.Black.copy(alpha = 0.6f),
288276
contentColor = Color.White,
@@ -443,78 +431,56 @@ fun LongPressMenuButton(
443431

444432
private class LongPressablePreviews : CollectionPreviewParameterProvider<LongPressable>(
445433
listOf(
446-
object : LongPressable {
447-
override val title: String = "Big Buck Bunny"
448-
override val url: String = "https://www.youtube.com/watch?v=YE7VzlLtp-4"
449-
override val thumbnailUrl: String =
450-
"https://i.ytimg.com/vi_webp/YE7VzlLtp-4/maxresdefault.webp"
451-
override val uploader: String = "Blender"
452-
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
453-
override val viewCount: Long = 8765432
454-
override val uploadDate: Either<String, OffsetDateTime> = Either.left("16 years ago")
455-
override val decoration: LongPressableDecoration = LongPressableDecoration.Playlist(12)
456-
457-
override fun getPlayQueue(): PlayQueue {
458-
return SinglePlayQueue(listOf(), 0)
459-
}
460-
},
461-
object : LongPressable {
462-
override val title: String = LoremIpsum().values.first()
463-
override val url: String = "https://www.youtube.com/watch?v=YE7VzlLtp-4"
464-
override val thumbnailUrl: String? = null
465-
override val uploader: String = "Blender"
466-
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
467-
override val viewCount: Long = 8765432
468-
override val uploadDate: Either<String, OffsetDateTime> = Either.left("16 years ago")
469-
override val decoration: LongPressableDecoration = LongPressableDecoration.Duration(500)
470-
471-
override fun getPlayQueue(): PlayQueue {
472-
return SinglePlayQueue(listOf(), 0)
473-
}
474-
},
475-
object : LongPressable {
476-
override val title: String = LoremIpsum().values.first()
477-
override val url: String = "https://www.youtube.com/watch?v=YE7VzlLtp-4"
478-
override val thumbnailUrl: String? = null
479-
override val uploader: String? = null
480-
override val uploaderUrl: String = "https://www.youtube.com/@BlenderOfficial"
481-
override val viewCount: Long? = null
482-
override val uploadDate: Either<String, OffsetDateTime>? = null
483-
override val decoration: LongPressableDecoration? = null
484-
485-
override fun getPlayQueue(): PlayQueue {
486-
return SinglePlayQueue(listOf(), 0)
487-
}
488-
},
489-
object : LongPressable {
490-
override val title: String = LoremIpsum().values.first()
491-
override val url: String = "https://www.youtube.com/watch?v=YE7VzlLtp-4"
492-
override val thumbnailUrl: String =
493-
"https://i.ytimg.com/vi_webp/YE7VzlLtp-4/maxresdefault.webp"
494-
override val uploader: String? = null
495-
override val uploaderUrl: String? = null
496-
override val viewCount: Long? = null
497-
override val uploadDate: Either<String, OffsetDateTime>? = null
498-
override val decoration: LongPressableDecoration = LongPressableDecoration.Live
499-
500-
override fun getPlayQueue(): PlayQueue {
501-
return SinglePlayQueue(listOf(), 0)
502-
}
503-
},
504-
object : LongPressable {
505-
override val title: String = LoremIpsum().values.first()
506-
override val url: String = "https://www.youtube.com/watch?v=YE7VzlLtp-4"
507-
override val thumbnailUrl: String? = null
508-
override val uploader: String? = null
509-
override val uploaderUrl: String? = null
510-
override val viewCount: Long? = null
511-
override val uploadDate: Either<String, OffsetDateTime> = Either.right(OffsetDateTime.now().minusSeconds(12))
512-
override val decoration: LongPressableDecoration = LongPressableDecoration.Playlist(1500)
513-
514-
override fun getPlayQueue(): PlayQueue {
515-
return SinglePlayQueue(listOf(), 0)
516-
}
517-
}
434+
LongPressable(
435+
title = "Big Buck Bunny",
436+
url = "https://www.youtube.com/watch?v=YE7VzlLtp-4",
437+
thumbnailUrl = "https://i.ytimg.com/vi_webp/YE7VzlLtp-4/maxresdefault.webp",
438+
uploader = "Blender",
439+
uploaderUrl = "https://www.youtube.com/@BlenderOfficial",
440+
viewCount = 8765432,
441+
uploadDate = Either.left("16 years ago"),
442+
decoration = LongPressable.Decoration.Playlist(12),
443+
),
444+
LongPressable(
445+
title = LoremIpsum().values.first(),
446+
url = "https://www.youtube.com/watch?v=YE7VzlLtp-4",
447+
thumbnailUrl = null,
448+
uploader = "Blender",
449+
uploaderUrl = "https://www.youtube.com/@BlenderOfficial",
450+
viewCount = 8765432,
451+
uploadDate = Either.left("16 years ago"),
452+
decoration = LongPressable.Decoration.Duration(500),
453+
),
454+
LongPressable(
455+
title = LoremIpsum().values.first(),
456+
url = "https://www.youtube.com/watch?v=YE7VzlLtp-4",
457+
thumbnailUrl = null,
458+
uploader = null,
459+
uploaderUrl = "https://www.youtube.com/@BlenderOfficial",
460+
viewCount = null,
461+
uploadDate = null,
462+
decoration = null,
463+
),
464+
LongPressable(
465+
title = LoremIpsum().values.first(),
466+
url = "https://www.youtube.com/watch?v=YE7VzlLtp-4",
467+
thumbnailUrl = "https://i.ytimg.com/vi_webp/YE7VzlLtp-4/maxresdefault.webp",
468+
uploader = null,
469+
uploaderUrl = null,
470+
viewCount = null,
471+
uploadDate = null,
472+
decoration = LongPressable.Decoration.Live,
473+
),
474+
LongPressable(
475+
title = LoremIpsum().values.first(),
476+
url = "https://www.youtube.com/watch?v=YE7VzlLtp-4",
477+
thumbnailUrl = null,
478+
uploader = null,
479+
uploaderUrl = null,
480+
viewCount = null,
481+
uploadDate = Either.right(OffsetDateTime.now().minusSeconds(12)),
482+
decoration = LongPressable.Decoration.Playlist(1500),
483+
),
518484
)
519485
)
520486

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package org.schabi.newpipe.ui.components.menu
22

33
import androidx.compose.runtime.Stable
4-
import org.schabi.newpipe.player.playqueue.PlayQueue
54
import org.schabi.newpipe.util.Either
65
import java.time.OffsetDateTime
76

8-
// TODO move within LongPressable
9-
sealed interface LongPressableDecoration {
10-
data class Duration(val duration: Long) : LongPressableDecoration
11-
data object Live : LongPressableDecoration
12-
data class Playlist(val itemCount: Long) : LongPressableDecoration
13-
}
14-
15-
// TODO this can be a data class
167
@Stable
17-
interface LongPressable {
18-
val title: String
19-
val url: String?
20-
val thumbnailUrl: String?
21-
val uploader: String?
22-
val uploaderUrl: String?
23-
val viewCount: Long?
24-
val uploadDate: Either<String, OffsetDateTime>?
25-
val decoration: LongPressableDecoration?
26-
27-
fun getPlayQueue(): PlayQueue
8+
data class LongPressable(
9+
val title: String,
10+
val url: String?,
11+
val thumbnailUrl: String?,
12+
val uploader: String?,
13+
val uploaderUrl: String?,
14+
val viewCount: Long?,
15+
val uploadDate: Either<String, OffsetDateTime>?,
16+
val decoration: Decoration?,
17+
) {
18+
sealed interface Decoration {
19+
data class Duration(val duration: Long) : Decoration
20+
data object Live : Decoration
21+
data class Playlist(val itemCount: Long) : Decoration
22+
}
2823
}

0 commit comments

Comments
 (0)