Skip to content

Commit edacd69

Browse files
author
Yevhen Babiichuk (DustDFG)
committed
Refactor ExportPlaylist to use more idiomatic kotlin code
1 parent 8893a27 commit edacd69

4 files changed

Lines changed: 36 additions & 32 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/playlist/ExportPlaylist.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2018-2022 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
17
package org.schabi.newpipe.local.playlist
28

39
import android.content.Context
@@ -21,11 +27,7 @@ fun export(
2127
}
2228
}
2329

24-
fun exportWithTitles(
25-
playlist: List<PlaylistStreamEntry>,
26-
context: Context
27-
): String {
28-
30+
private fun exportWithTitles(playlist: List<PlaylistStreamEntry>, context: Context): String {
2931
return playlist.asSequence()
3032
.map { it.streamEntity }
3133
.map { entity ->
@@ -38,18 +40,14 @@ fun exportWithTitles(
3840
.joinToString(separator = "\n")
3941
}
4042

41-
fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
42-
43-
return playlist.asSequence()
44-
.map { it.streamEntity.url }
45-
.joinToString(separator = "\n")
43+
private fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
44+
return playlist.joinToString(separator = "\n") { it.streamEntity.url }
4645
}
4746

48-
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
47+
private fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
4948

5049
val videoIDs = playlist.asReversed().asSequence()
51-
.map { it.streamEntity.url }
52-
.mapNotNull(::getYouTubeId)
50+
.mapNotNull { getYouTubeId(it.streamEntity.url) }
5351
.take(50) // YouTube limitation: temp playlists can't have more than 50 items
5452
.toList()
5553
.asReversed()
@@ -58,15 +56,15 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
5856
return "https://www.youtube.com/watch_videos?video_ids=$videoIDs"
5957
}
6058

61-
val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
59+
private val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
6260

6361
/**
6462
* Gets the video id from a YouTube URL.
6563
*
6664
* @param url YouTube URL
6765
* @return the video id
6866
*/
69-
fun getYouTubeId(url: String): String? {
67+
private fun getYouTubeId(url: String): String? {
7068

7169
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
7270
}

app/src/main/java/org/schabi/newpipe/local/playlist/PlayListShareMode.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2018-2022 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package org.schabi.newpipe.local.playlist
8+
9+
enum class PlayListShareMode {
10+
JUST_URLS,
11+
WITH_TITLES,
12+
YOUTUBE_TEMP_PLAYLIST
13+
}

app/src/test/java/org/schabi/newpipe/local/playlist/ExportPlaylistTest.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2018-2022 NewPipe contributors <https://newpipe.net>
3+
* SPDX-FileCopyrightText: 2025 NewPipe e.V. <https://newpipe-ev.de>
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
17
package org.schabi.newpipe.local.playlist
28

39
import android.content.Context
@@ -9,7 +15,6 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
915
import org.schabi.newpipe.extractor.stream.StreamType
1016
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
1117
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
12-
import java.util.stream.Stream
1318

1419
class ExportPlaylistTest {
1520

@@ -41,9 +46,7 @@ class ExportPlaylistTest {
4146
*/
4247

4348
val playlist = asPlaylist(
44-
(10..70)
45-
.map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
46-
.stream()
49+
(10..70).map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
4750
)
4851

4952
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
@@ -78,13 +81,11 @@ class ExportPlaylistTest {
7881
}
7982

8083
fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> {
81-
return asPlaylist(Stream.of(*urls))
84+
return asPlaylist(listOf(*urls))
8285
}
8386

84-
fun asPlaylist(urls: Stream<String>): List<PlaylistStreamEntry> {
85-
return urls
86-
.map { url: String -> newPlaylistStreamEntry(url) }
87-
.toList()
87+
fun asPlaylist(urls: List<String>): List<PlaylistStreamEntry> {
88+
return urls.map { newPlaylistStreamEntry(it) }
8889
}
8990

9091
fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry {

0 commit comments

Comments
 (0)