feat: Playlist-Links im Jam (Feld und Teilen) (1.5.0)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-12 21:59:41 +01:00
co-authored by Claude Opus 5.5
parent 5ae9bc4431
commit 661d7ee72c
7 changed files with 64 additions and 4 deletions
@@ -140,7 +140,17 @@ class JamActivity : AppCompatActivity() {
if (nameInput.text.isNotBlank()) joinJam(code) else nameInput.requestFocus()
}
Intent.ACTION_SEND -> {
val videoId = JamSync.videoIdFromSharedText(intent.getStringExtra(Intent.EXTRA_TEXT)) ?: return
val shared = intent.getStringExtra(Intent.EXTRA_TEXT)
val playlistId = JamSync.playlistIdFromSharedText(shared)
if (playlistId != null && JamHub.active) {
JamHub.addPlaylist(playlistId)
Toast.makeText(this, R.string.jam_playlist_adding, Toast.LENGTH_SHORT).show()
return
}
val videoId = JamSync.videoIdFromSharedText(shared) ?: run {
if (playlistId != null) status.setText(R.string.jam_share_no_jam)
return
}
if (JamHub.active) {
JamHub.addVideo(videoId)
Toast.makeText(this, R.string.jam_added_shared, Toast.LENGTH_SHORT).show()
@@ -186,6 +196,13 @@ class JamActivity : AppCompatActivity() {
val text = addInput.text.toString().trim()
if (text.isEmpty()) return
val videoId = JamSync.parseVideoId(text)
val playlistId = if (videoId == null) JamSync.parsePlaylistId(text) else null
if (playlistId != null) {
JamHub.addPlaylist(playlistId)
addInput.setText("")
status.setText(R.string.jam_playlist_adding)
return
}
if (videoId != null) {
JamHub.addVideo(videoId)
addInput.setText("")
@@ -261,6 +261,7 @@ object JamHub {
"not_allowed" -> str(R.string.jam_error_not_allowed)
"queue_full" -> str(R.string.jam_error_queue_full)
"bad_video" -> str(R.string.jam_error_bad_video)
"playlist_not_found" -> str(R.string.jam_error_playlist)
"rate_limited" -> str(R.string.jam_error_rate_limited)
else -> str(R.string.jam_error_other, error)
}
@@ -313,6 +314,11 @@ object JamHub {
send(msg)
}
/** Ganze Playlist / ganzes Album: der Jam-Server holt die Songs. */
fun addPlaylist(playlistId: String) {
send(JSONObject().put("t", "addPlaylist").put("playlistId", playlistId))
}
data class SearchResult(val videoId: String, val title: String, val artist: String, val thumb: String)
fun search(query: String, done: (List<SearchResult>?) -> Unit) {
@@ -88,6 +88,28 @@ object JamSync {
return id?.takeIf { VIDEO_ID.matches(it) }
}
private val PLAYLIST_ID = Regex("^(PL|OLAK5uy_|RD|LL|UU|FL|OL|LM)[A-Za-z0-9_-]{0,80}$")
/** Playlist-/Album-ID aus einer ID oder einem Link mit `list=`, sonst null. */
fun parsePlaylistId(input: String?): String? {
val text = input?.trim() ?: return null
val raw = if (text.contains("youtube.com") || text.contains("youtu.be") || text.contains("://")) {
val rest = text.substringAfter("://", text)
val host = rest.substringBefore('/').lowercase().removePrefix("www.").removePrefix("m.").removePrefix("music.")
if (host != "youtube.com" && host != "youtu.be") return null
rest.substringAfter('?', "").substringBefore('#').split('&')
.firstOrNull { it.startsWith("list=") }?.removePrefix("list=") ?: return null
} else text
val id = raw.removePrefix("VL")
return id.takeIf { PLAYLIST_ID.matches(it) }
}
/** Playlist-Link (ohne einzelnes Video) aus einem geteilten Text. */
fun playlistIdFromSharedText(text: String?): String? =
text?.split(Regex("\\s+"))?.firstNotNullOfOrNull { word ->
if (parseVideoId(word) == null) parsePlaylistId(word) else null
}
/** Sucht in einem geteilten Text (z. B. aus der YouTube-App) den ersten YouTube-Link. */
fun videoIdFromSharedText(text: String?): String? =
text?.split(Regex("\\s+"))?.firstNotNullOfOrNull { parseVideoId(it) }