Convert some extension functions to member functions.

This commit is contained in:
Isira Seneviratne 2023-02-17 07:11:06 +05:30
parent 86ee731d54
commit 10aa703aa5
19 changed files with 129 additions and 154 deletions

View File

@ -14,8 +14,6 @@ import com.github.libretube.db.obj.LocalPlaylist
import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toLocalPlaylistItem
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper

View File

@ -20,4 +20,9 @@ data class PipedStream(
val fps: Int? = null,
val audioTrackName: String? = null,
val audioTrackId: String? = null
)
) {
fun getQualityString(fileName: String): String {
return "${fileName}_${quality?.replace(" ", "_")}_$format." +
mimeType?.split("/")?.last()
}
}

View File

@ -1,5 +1,6 @@
package com.github.libretube.api.obj
import com.github.libretube.db.obj.PlaylistBookmark
import kotlinx.serialization.Serializable
@Serializable
@ -13,4 +14,15 @@ data class Playlist(
val uploaderAvatar: String? = null,
val videos: Int = 0,
val relatedStreams: List<StreamItem> = emptyList()
)
) {
fun toPlaylistBookmark(playlistId: String): PlaylistBookmark {
return PlaylistBookmark(
playlistId = playlistId,
playlistName = name,
thumbnailUrl = thumbnailUrl,
uploader = uploader,
uploaderAvatar = uploaderAvatar,
uploaderUrl = uploaderUrl
)
}
}

View File

@ -1,5 +1,7 @@
package com.github.libretube.api.obj
import com.github.libretube.db.obj.LocalPlaylistItem
import com.github.libretube.extensions.toID
import kotlinx.serialization.Serializable
@Serializable
@ -18,4 +20,18 @@ data class StreamItem(
val uploaded: Long? = null,
val shortDescription: String? = null,
val isShort: Boolean = false
)
) {
fun toLocalPlaylistItem(playlistId: String): LocalPlaylistItem {
return LocalPlaylistItem(
playlistId = playlistId.toInt(),
videoId = url!!.toID(),
title = title,
thumbnailUrl = thumbnail,
uploader = uploaderName,
uploaderUrl = uploaderUrl,
uploaderAvatar = uploaderAvatar,
uploadDate = uploadedDate,
duration = duration
)
}
}

View File

@ -1,5 +1,7 @@
package com.github.libretube.api.obj
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType
import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable
@ -29,4 +31,77 @@ data class Streams(
val chapters: List<ChapterSegment> = emptyList(),
val uploaderSubscriberCount: Long = 0,
val previewFrames: List<PreviewFrames> = emptyList()
)
) {
fun toDownloadItems(
videoId: String,
fileName: String,
videoFormat: String?,
videoQuality: String?,
audioFormat: String?,
audioQuality: String?,
subtitleCode: String?
): List<DownloadItem> {
val items = mutableListOf<DownloadItem>()
if (!videoQuality.isNullOrEmpty() && !videoFormat.isNullOrEmpty()) {
val stream = videoStreams.find { it.quality == videoQuality && it.format == videoFormat }
items.add(
DownloadItem(
type = FileType.VIDEO,
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = "",
url = stream?.url,
format = videoFormat,
quality = videoQuality
)
)
}
if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
val stream = audioStreams.find { it.quality == audioQuality && it.format == audioFormat }
items.add(
DownloadItem(
type = FileType.AUDIO,
videoId = videoId,
fileName = stream?.getQualityString(fileName).orEmpty(),
path = "",
url = stream?.url,
format = audioFormat,
quality = audioQuality
)
)
}
if (!subtitleCode.isNullOrEmpty()) {
items.add(
DownloadItem(
type = FileType.SUBTITLE,
videoId = videoId,
fileName = "${fileName}_$subtitleCode.srt",
path = "",
url = subtitles.find { it.code == subtitleCode }?.url
)
)
}
return items
}
fun toStreamItem(videoId: String): StreamItem {
return StreamItem(
url = videoId,
title = title,
thumbnail = thumbnailUrl,
uploaderName = uploader,
uploaderUrl = uploaderUrl,
uploaderAvatar = uploaderAvatar,
uploadedDate = uploadDate.toString(),
uploaded = null,
duration = duration,
views = views,
uploaderVerified = uploaderVerified,
shortDescription = description
)
}
}

View File

@ -3,6 +3,8 @@ package com.github.libretube.db.obj
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.helpers.ProxyHelper
import kotlinx.serialization.Serializable
@Serializable
@ -18,4 +20,18 @@ data class LocalPlaylistItem(
@ColumnInfo val uploaderAvatar: String? = null,
@ColumnInfo val thumbnailUrl: String? = null,
@ColumnInfo val duration: Long? = null
)
) {
fun toStreamItem(): StreamItem {
return StreamItem(
url = videoId,
title = title,
thumbnail = ProxyHelper.rewriteUrl(thumbnailUrl),
uploaderName = uploader,
uploaderUrl = uploaderUrl,
uploaderAvatar = ProxyHelper.rewriteUrl(uploaderAvatar),
uploadedDate = uploadDate,
uploaded = null,
duration = duration
)
}
}

View File

@ -1,8 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.PipedStream
fun PipedStream?.qualityString(fileName: String): String {
this ?: return ""
return fileName + "_" + quality?.replace(" ", "_") + "_" + format + "." + mimeType?.split("/")?.last()
}

View File

@ -1,61 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.Streams
import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType
fun Streams.toDownloadItems(
videoId: String,
fileName: String,
videoFormat: String?,
videoQuality: String?,
audioFormat: String?,
audioQuality: String?,
subtitleCode: String?
): List<DownloadItem> {
val items = mutableListOf<DownloadItem>()
if (!videoQuality.isNullOrEmpty() && !videoFormat.isNullOrEmpty()) {
val stream = videoStreams.find { it.quality == videoQuality && it.format == videoFormat }
items.add(
DownloadItem(
type = FileType.VIDEO,
videoId = videoId,
fileName = stream.qualityString(fileName),
path = "",
url = stream?.url,
format = videoFormat,
quality = videoQuality
)
)
}
if (!audioQuality.isNullOrEmpty() && !audioFormat.isNullOrEmpty()) {
val stream = audioStreams.find { it.quality == audioQuality && it.format == audioFormat }
items.add(
DownloadItem(
type = FileType.AUDIO,
videoId = videoId,
fileName = stream.qualityString(fileName),
path = "",
url = stream?.url,
format = audioFormat,
quality = audioQuality
)
)
}
if (!subtitleCode.isNullOrEmpty()) {
items.add(
DownloadItem(
type = FileType.SUBTITLE,
videoId = videoId,
fileName = "${fileName}_$subtitleCode.srt",
path = "",
url = subtitles.find { it.code == subtitleCode }?.url
)
)
}
return items
}

View File

@ -1,18 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.db.obj.LocalPlaylistItem
fun StreamItem.toLocalPlaylistItem(playlistId: String): LocalPlaylistItem {
return LocalPlaylistItem(
playlistId = playlistId.toInt(),
videoId = url!!.toID(),
title = title,
thumbnailUrl = thumbnail,
uploader = uploaderName,
uploaderUrl = uploaderUrl,
uploaderAvatar = uploaderAvatar,
uploadDate = uploadedDate,
duration = duration
)
}

View File

@ -1,15 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.Playlist
import com.github.libretube.db.obj.PlaylistBookmark
fun Playlist.toPlaylistBookmark(playlistId: String): PlaylistBookmark {
return PlaylistBookmark(
playlistId = playlistId,
playlistName = name,
thumbnailUrl = thumbnailUrl,
uploader = uploader,
uploaderAvatar = uploaderAvatar,
uploaderUrl = uploaderUrl
)
}

View File

@ -1,37 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Streams
import com.github.libretube.db.obj.LocalPlaylistItem
import com.github.libretube.helpers.ProxyHelper
fun Streams.toStreamItem(videoId: String): StreamItem {
return StreamItem(
url = videoId,
title = title,
thumbnail = thumbnailUrl,
uploaderName = uploader,
uploaderUrl = uploaderUrl,
uploaderAvatar = uploaderAvatar,
uploadedDate = uploadDate.toString(),
uploaded = null,
duration = duration,
views = views,
uploaderVerified = uploaderVerified,
shortDescription = description
)
}
fun LocalPlaylistItem.toStreamItem(): StreamItem {
return StreamItem(
url = videoId,
title = title,
thumbnail = ProxyHelper.rewriteUrl(thumbnailUrl),
uploaderName = uploader,
uploaderUrl = uploaderUrl,
uploaderAvatar = ProxyHelper.rewriteUrl(uploaderAvatar),
uploadedDate = uploadDate,
uploaded = null,
duration = duration
)
}

View File

@ -28,7 +28,6 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PlayerHelper.checkForSegments
import com.github.libretube.helpers.PlayerHelper.loadPlaybackParams

View File

@ -25,7 +25,6 @@ import com.github.libretube.db.obj.DownloadItem
import com.github.libretube.enums.FileType
import com.github.libretube.extensions.formatAsFileSize
import com.github.libretube.extensions.getContentLength
import com.github.libretube.extensions.toDownloadItems
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.DownloadHelper.getNotificationId

View File

@ -14,7 +14,6 @@ import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.databinding.DialogAddtoplaylistBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.ui.models.PlaylistViewModel
import com.github.libretube.util.PlayingQueue

View File

@ -62,7 +62,6 @@ import com.github.libretube.extensions.formatShort
import com.github.libretube.extensions.hideKeyboard
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.extensions.updateParameters
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.DashHelper

View File

@ -25,7 +25,6 @@ import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.dpToPx
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toPlaylistBookmark
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.adapters.PlaylistAdapter

View File

@ -10,7 +10,6 @@ import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.query
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toPlaylistBookmark
import com.github.libretube.extensions.toastFromMainThread
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.obj.ShareData

View File

@ -10,7 +10,6 @@ import com.github.libretube.db.DatabaseHolder
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.awaitQuery
import com.github.libretube.extensions.toStreamItem
import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.helpers.PlayerHelper
import com.github.libretube.helpers.PreferenceHelper

View File

@ -6,7 +6,6 @@ import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.StreamItem
import com.github.libretube.extensions.move
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toStreamItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch