2021-08-22 09:48:33 +02:00
|
|
|
package audio.funkwhale.ffa.model
|
|
|
|
|
2023-01-10 13:56:20 +01:00
|
|
|
import android.os.Parcelable
|
2022-12-09 09:49:41 +01:00
|
|
|
import audio.funkwhale.ffa.utils.containsIgnoringCase
|
2021-08-22 09:48:33 +02:00
|
|
|
import com.preference.PowerPreference
|
2023-01-10 13:56:20 +01:00
|
|
|
import kotlinx.parcelize.IgnoredOnParcel
|
|
|
|
import kotlinx.parcelize.Parcelize
|
2021-08-22 09:48:33 +02:00
|
|
|
|
2023-01-10 13:56:20 +01:00
|
|
|
@Parcelize
|
2021-08-22 09:48:33 +02:00
|
|
|
data class Track(
|
|
|
|
val id: Int = 0,
|
|
|
|
val title: String,
|
2023-01-11 13:41:04 +01:00
|
|
|
private val cover: Covers? ,
|
2021-08-22 09:48:33 +02:00
|
|
|
val artist: Artist,
|
|
|
|
val album: Album?,
|
|
|
|
val disc_number: Int = 0,
|
|
|
|
val position: Int = 0,
|
|
|
|
val uploads: List<Upload> = listOf(),
|
|
|
|
val copyright: String? = null,
|
|
|
|
val license: String? = null
|
2023-01-10 13:56:20 +01:00
|
|
|
) : SearchResult, Parcelable {
|
2023-01-11 13:41:04 +01:00
|
|
|
|
2023-01-10 13:56:20 +01:00
|
|
|
@IgnoredOnParcel
|
2021-08-22 09:48:33 +02:00
|
|
|
var current: Boolean = false
|
2023-01-10 13:56:20 +01:00
|
|
|
|
|
|
|
@IgnoredOnParcel
|
2021-08-22 09:48:33 +02:00
|
|
|
var favorite: Boolean = false
|
2023-01-10 13:56:20 +01:00
|
|
|
|
|
|
|
@IgnoredOnParcel
|
2021-08-22 09:48:33 +02:00
|
|
|
var cached: Boolean = false
|
2023-01-10 13:56:20 +01:00
|
|
|
|
|
|
|
@IgnoredOnParcel
|
2021-08-22 09:48:33 +02:00
|
|
|
var downloaded: Boolean = false
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
fun fromDownload(download: DownloadInfo): Track = Track(
|
|
|
|
id = download.id,
|
|
|
|
title = download.title,
|
2023-01-11 13:41:04 +01:00
|
|
|
cover = Covers(CoverUrls("")),
|
2021-08-22 09:48:33 +02:00
|
|
|
artist = Artist(0, download.artist, listOf()),
|
|
|
|
album = Album(0, Album.Artist(""), "", Covers(CoverUrls("")), ""),
|
|
|
|
uploads = listOf(Upload(download.contentId, 0, 0))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:56:20 +01:00
|
|
|
@Parcelize
|
|
|
|
data class Upload(val listen_url: String, val duration: Int, val bitrate: Int) : Parcelable
|
2022-12-09 09:49:41 +01:00
|
|
|
|
|
|
|
fun matchesFilter(filter: String): Boolean {
|
|
|
|
return title.containsIgnoringCase(filter) ||
|
|
|
|
artist.name.containsIgnoringCase(filter) ||
|
|
|
|
album?.title.containsIgnoringCase(filter)
|
|
|
|
}
|
2021-08-22 09:48:33 +02:00
|
|
|
|
|
|
|
override fun equals(other: Any?): Boolean {
|
|
|
|
return when (other) {
|
|
|
|
is Track -> other.id == id
|
|
|
|
else -> false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun hashCode(): Int {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
fun bestUpload(): Upload? {
|
|
|
|
if (uploads.isEmpty()) return null
|
|
|
|
|
2023-01-30 21:28:51 +01:00
|
|
|
var bestUpload = when (PowerPreference.getDefaultFile().getString("media_cache_quality")) {
|
2021-08-22 09:48:33 +02:00
|
|
|
"quality" -> uploads.maxByOrNull { it.bitrate } ?: uploads[0]
|
|
|
|
"size" -> uploads.minByOrNull { it.bitrate } ?: uploads[0]
|
|
|
|
else -> uploads.maxByOrNull { it.bitrate } ?: uploads[0]
|
|
|
|
}
|
2023-01-30 21:28:51 +01:00
|
|
|
|
|
|
|
return when (PowerPreference.getDefaultFile().getString("bandwidth_limitation")) {
|
|
|
|
"unlimited" -> bestUpload
|
|
|
|
"limited" -> {
|
|
|
|
var listenUrl = bestUpload.listen_url
|
|
|
|
Upload(listenUrl.plus("&to=mp3&max_bitrate=320"), uploads[0].duration, 320_000)
|
|
|
|
}
|
|
|
|
else -> bestUpload
|
|
|
|
}
|
2021-08-22 09:48:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 13:41:04 +01:00
|
|
|
override fun cover(): String? {
|
|
|
|
return if (cover?.urls?.original != null) {
|
|
|
|
cover.urls.original
|
|
|
|
} else {
|
|
|
|
album?.cover()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 09:48:33 +02:00
|
|
|
override fun title() = title
|
|
|
|
override fun subtitle() = artist.name
|
|
|
|
|
|
|
|
val formatted: String get() = "$id $artist ($album): $title"
|
2021-09-09 09:56:15 +02:00
|
|
|
}
|