Skip to content

Commit 8379aa0

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

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

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

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 NewPipe contributors <https://newpipe.net>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
16
package org.schabi.newpipe.local.playlist
27

38
import android.content.Context
@@ -21,11 +26,7 @@ fun export(
2126
}
2227
}
2328

24-
fun exportWithTitles(
25-
playlist: List<PlaylistStreamEntry>,
26-
context: Context
27-
): String {
28-
29+
private fun exportWithTitles(playlist: List<PlaylistStreamEntry>, context: Context): String {
2930
return playlist.asSequence()
3031
.map { it.streamEntity }
3132
.map { entity ->
@@ -38,18 +39,14 @@ fun exportWithTitles(
3839
.joinToString(separator = "\n")
3940
}
4041

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

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

5048
val videoIDs = playlist.asReversed().asSequence()
51-
.map { it.streamEntity.url }
52-
.mapNotNull(::getYouTubeId)
49+
.mapNotNull { getYouTubeId(it.streamEntity.url) }
5350
.take(50) // YouTube limitation: temp playlists can't have more than 50 items
5451
.toList()
5552
.asReversed()
@@ -58,15 +55,15 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
5855
return "https://www.youtube.com/watch_videos?video_ids=$videoIDs"
5956
}
6057

61-
val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
58+
private val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
6259

6360
/**
6461
* Gets the video id from a YouTube URL.
6562
*
6663
* @param url YouTube URL
6764
* @return the video id
6865
*/
69-
fun getYouTubeId(url: String): String? {
66+
private fun getYouTubeId(url: String): String? {
7067

7168
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
7269
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 NewPipe contributors <https://newpipe.net>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
16
package org.schabi.newpipe.local.playlist
27

38
import android.content.Context
@@ -9,7 +14,6 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
914
import org.schabi.newpipe.extractor.stream.StreamType
1015
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
1116
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
12-
import java.util.stream.Stream
1317

1418
class ExportPlaylistTest {
1519

@@ -41,9 +45,7 @@ class ExportPlaylistTest {
4145
*/
4246

4347
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()
48+
(10..70).map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
4749
)
4850

4951
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
@@ -78,13 +80,11 @@ class ExportPlaylistTest {
7880
}
7981

8082
fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> {
81-
return asPlaylist(Stream.of(*urls))
83+
return asPlaylist(listOf(*urls))
8284
}
8385

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

9090
fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry {

0 commit comments

Comments
 (0)