[#11930] Converting to Kotlin

This commit is contained in:
Thiago F. G. Albuquerque 2025-02-24 20:57:06 -03:00
parent 76a02d5858
commit 998d84de6c
5 changed files with 183 additions and 223 deletions

View file

@ -1,90 +0,0 @@
package org.schabi.newpipe.local.playlist;
import static com.google.common.collect.Streams.stream;
import static org.apache.commons.collections4.IterableUtils.reversedIterable;
import static java.util.Collections.reverse;
import android.content.Context;
import org.schabi.newpipe.R;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import okhttp3.HttpUrl;
final class ExportPlaylist {
private ExportPlaylist() {
}
static String export(final PlayListShareMode shareMode,
final List<PlaylistStreamEntry> playlist,
final Context context) {
return switch (shareMode) {
case WITH_TITLES -> exportWithTitles(playlist, context);
case JUST_URLS -> exportJustUrls(playlist);
case YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist);
};
}
static String exportWithTitles(final List<PlaylistStreamEntry> playlist,
final Context context) {
return playlist.stream()
.map(PlaylistStreamEntry::getStreamEntity)
.map(entity -> context.getString(R.string.video_details_list_item,
entity.getTitle(),
entity.getUrl()
)
)
.collect(Collectors.joining("\n"));
}
static String exportJustUrls(final List<PlaylistStreamEntry> playlist) {
return playlist.stream()
.map(PlaylistStreamEntry::getStreamEntity)
.map(StreamEntity::getUrl)
.collect(Collectors.joining("\n"));
}
static String exportAsYoutubeTempPlaylist(final List<PlaylistStreamEntry> playlist) {
final List<String> videoIDs =
stream(reversedIterable(playlist))
.map(PlaylistStreamEntry::getStreamEntity)
.map(entity -> getYouTubeId(entity.getUrl()))
.filter(Objects::nonNull)
.limit(50)
.collect(Collectors.toList());
reverse(videoIDs);
final String commaSeparatedVideoIDs = videoIDs.stream()
.collect(Collectors.joining(","));
return "http://www.youtube.com/watch_videos?video_ids=" + commaSeparatedVideoIDs;
}
/**
* Gets the video id from a YouTube URL.
*
* @param url YouTube URL
* @return the video id
*/
static String getYouTubeId(final String url) {
final HttpUrl httpUrl = HttpUrl.parse(url);
return httpUrl == null ? null
: httpUrl.queryParameter("v");
}
}

View file

@ -0,0 +1,72 @@
package org.schabi.newpipe.local.playlist
import android.content.Context
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.schabi.newpipe.R
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
import org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
import java.util.Objects.nonNull
fun export(
shareMode: PlayListShareMode,
playlist: List<PlaylistStreamEntry>,
context: Context
): String {
return when (shareMode) {
WITH_TITLES -> exportWithTitles(playlist, context)
JUST_URLS -> exportJustUrls(playlist)
YOUTUBE_TEMP_PLAYLIST -> exportAsYoutubeTempPlaylist(playlist)
}
}
fun exportWithTitles(
playlist: List<PlaylistStreamEntry>,
context: Context
): String {
return playlist.asSequence()
.map { it.streamEntity }
.map { entity ->
context.getString(
R.string.video_details_list_item,
entity.title,
entity.url
)
}
.joinToString(separator = "\n")
}
fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
return playlist.asSequence()
.map { it.streamEntity.url }
.joinToString(separator = "\n")
}
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
val videoIDs = playlist.asReversed().asSequence()
.map { it.streamEntity }
.map { getYouTubeId(it.url) }
.filter(::nonNull)
.take(50)
.toList()
.asReversed()
.joinToString(separator = ",")
return "http://www.youtube.com/watch_videos?video_ids=$videoIDs"
}
/**
* Gets the video id from a YouTube URL.
*
* @param url YouTube URL
* @return the video id
*/
fun getYouTubeId(url: String): String? {
val httpUrl = url.toHttpUrlOrNull()
return httpUrl?.queryParameter("v")
}

View file

@ -2,7 +2,7 @@ package org.schabi.newpipe.local.playlist;
import static org.schabi.newpipe.error.ErrorUtil.showUiErrorSnackbar;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.local.playlist.ExportPlaylist.export;
import static org.schabi.newpipe.local.playlist.ExportPlaylistKt.export;
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
import static org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES;
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;