mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
refactor: simplify VideoOptionsBottomSheet logic and improve its performance
This commit is contained in:
parent
594488c77e
commit
c36d30629a
@ -24,4 +24,20 @@ data class ContentItem(
|
||||
val subscribers: Long = -1,
|
||||
val videos: Long = -1,
|
||||
val verified: Boolean? = null
|
||||
)
|
||||
) {
|
||||
fun toStreamItem() = StreamItem(
|
||||
url = url,
|
||||
type = StreamItem.TYPE_STREAM,
|
||||
title = title,
|
||||
thumbnail = thumbnail,
|
||||
uploaderName = uploaderName,
|
||||
uploaded = uploaded,
|
||||
uploaderAvatar = uploaderAvatar,
|
||||
uploaderUrl = uploaderUrl,
|
||||
duration = duration,
|
||||
uploaderVerified = uploaderVerified,
|
||||
shortDescription = shortDescription,
|
||||
views = views,
|
||||
isShort = isShort == true
|
||||
)
|
||||
}
|
||||
|
@ -8,22 +8,36 @@ import com.github.libretube.db.obj.SearchHistoryItem
|
||||
import com.github.libretube.db.obj.WatchHistoryItem
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.datetime.toKotlinLocalDate
|
||||
|
||||
object DatabaseHelper {
|
||||
private const val MAX_SEARCH_HISTORY_SIZE = 20
|
||||
|
||||
suspend fun addToWatchHistory(videoId: String, streams: Streams) = withContext(Dispatchers.IO) {
|
||||
suspend fun addToWatchHistory(videoId: String, streams: Streams) = addToWatchHistory(
|
||||
videoId,
|
||||
streams.toStreamItem(videoId)
|
||||
)
|
||||
suspend fun addToWatchHistory(
|
||||
videoId: String,
|
||||
stream: StreamItem
|
||||
) = withContext(Dispatchers.IO) {
|
||||
val watchHistoryItem = WatchHistoryItem(
|
||||
videoId,
|
||||
streams.title,
|
||||
streams.uploadDate,
|
||||
streams.uploader,
|
||||
streams.uploaderUrl.toID(),
|
||||
streams.uploaderAvatar,
|
||||
streams.thumbnailUrl,
|
||||
streams.duration
|
||||
stream.title,
|
||||
stream.uploaded?.let {
|
||||
Instant.ofEpochMilli(
|
||||
it
|
||||
).atZone(ZoneId.systemDefault()).toLocalDate().toKotlinLocalDate()
|
||||
},
|
||||
stream.uploaderName,
|
||||
stream.uploaderUrl?.toID(),
|
||||
stream.uploaderAvatar,
|
||||
stream.thumbnail,
|
||||
stream.duration
|
||||
)
|
||||
Database.watchHistoryDao().insert(watchHistoryItem)
|
||||
val maxHistorySize = PreferenceHelper.getString(PreferenceKeys.WATCH_HISTORY_SIZE, "100")
|
||||
|
@ -80,9 +80,8 @@ class PlaylistAdapter(
|
||||
NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId)
|
||||
}
|
||||
val videoId = streamItem.url!!.toID()
|
||||
val videoName = streamItem.title!!
|
||||
root.setOnLongClickListener {
|
||||
VideoOptionsBottomSheet(videoId, videoName, streamItem.duration) {
|
||||
VideoOptionsBottomSheet(streamItem) {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
.show(
|
||||
|
@ -96,9 +96,8 @@ class SearchAdapter(
|
||||
NavigationHelper.navigateVideo(root.context, item.url)
|
||||
}
|
||||
val videoId = item.url.toID()
|
||||
val videoName = item.title!!
|
||||
root.setOnLongClickListener {
|
||||
VideoOptionsBottomSheet(videoId, videoName, item.duration) {
|
||||
VideoOptionsBottomSheet(item.toStreamItem()) {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
.show(
|
||||
|
@ -148,7 +148,7 @@ class VideosAdapter(
|
||||
root.setOnLongClickListener {
|
||||
if (videoId == null || videoName == null) return@setOnLongClickListener true
|
||||
|
||||
VideoOptionsBottomSheet(videoId, videoName, video.duration) {
|
||||
VideoOptionsBottomSheet(video) {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
.show(
|
||||
@ -189,7 +189,7 @@ class VideosAdapter(
|
||||
|
||||
root.setOnLongClickListener {
|
||||
if (videoId == null || videoName == null) return@setOnLongClickListener true
|
||||
VideoOptionsBottomSheet(videoId, videoName, video.duration) {
|
||||
VideoOptionsBottomSheet(video) {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
.show(
|
||||
|
@ -68,7 +68,7 @@ class WatchHistoryAdapter(
|
||||
NavigationHelper.navigateVideo(root.context, video.videoId)
|
||||
}
|
||||
root.setOnLongClickListener {
|
||||
VideoOptionsBottomSheet(video.videoId, video.title!!, video.duration) {
|
||||
VideoOptionsBottomSheet(video.toStreamItem()) {
|
||||
notifyItemChanged(position)
|
||||
}
|
||||
.show(
|
||||
|
@ -384,9 +384,7 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions {
|
||||
override fun onLongTap() {
|
||||
val current = PlayingQueue.getCurrent() ?: return
|
||||
VideoOptionsBottomSheet(
|
||||
current.url?.toID() ?: return,
|
||||
current.title ?: return,
|
||||
current.duration
|
||||
current
|
||||
)
|
||||
.show(childFragmentManager)
|
||||
}
|
||||
|
@ -3,12 +3,13 @@ package com.github.libretube.ui.sheets
|
||||
import android.os.Bundle
|
||||
import androidx.navigation.fragment.NavHostFragment
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.api.RetrofitInstance
|
||||
import com.github.libretube.api.obj.StreamItem
|
||||
import com.github.libretube.constants.PreferenceKeys
|
||||
import com.github.libretube.db.DatabaseHelper
|
||||
import com.github.libretube.db.DatabaseHolder
|
||||
import com.github.libretube.db.obj.WatchPosition
|
||||
import com.github.libretube.enums.ShareObjectType
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.BackgroundHelper
|
||||
import com.github.libretube.helpers.NavigationHelper
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
@ -27,16 +28,15 @@ import kotlinx.coroutines.withContext
|
||||
/**
|
||||
* Dialog with different options for a selected video.
|
||||
*
|
||||
* Needs the [videoId] to load the content from the right video.
|
||||
* Needs the [streamItem] to load the content from the right video.
|
||||
*/
|
||||
class VideoOptionsBottomSheet(
|
||||
private val videoId: String,
|
||||
videoName: String,
|
||||
val duration: Long?,
|
||||
private val streamItem: StreamItem,
|
||||
private val onVideoChanged: () -> Unit = {}
|
||||
) : BaseBottomSheet() {
|
||||
private val shareData = ShareData(currentVideo = videoName)
|
||||
private val shareData = ShareData(currentVideo = streamItem.title)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
val videoId = streamItem.url?.toID() ?: return
|
||||
// List that stores the different menu options. In the future could be add more options here.
|
||||
val optionsList = mutableListOf(
|
||||
getString(R.string.playOnBackground),
|
||||
@ -60,9 +60,9 @@ class VideoOptionsBottomSheet(
|
||||
DatabaseHolder.Database.watchHistoryDao().findById(videoId)
|
||||
}
|
||||
|
||||
if (duration == null ||
|
||||
if (streamItem.duration == null ||
|
||||
watchPositionEntry == null ||
|
||||
watchPositionEntry.position < duration * 1000 * 0.9
|
||||
watchPositionEntry.position < streamItem.duration * 1000 * 0.9
|
||||
) {
|
||||
optionsList += getString(R.string.mark_as_watched)
|
||||
}
|
||||
@ -99,25 +99,11 @@ class VideoOptionsBottomSheet(
|
||||
}
|
||||
|
||||
getString(R.string.play_next) -> {
|
||||
try {
|
||||
val streamItem = withContext(Dispatchers.IO) {
|
||||
RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
|
||||
}
|
||||
PlayingQueue.addAsNext(streamItem)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
getString(R.string.add_to_queue) -> {
|
||||
try {
|
||||
val streamItem = withContext(Dispatchers.IO) {
|
||||
RetrofitInstance.api.getStreams(videoId).toStreamItem(videoId)
|
||||
}
|
||||
PlayingQueue.add(streamItem)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
getString(R.string.mark_as_watched) -> {
|
||||
@ -126,9 +112,7 @@ class VideoOptionsBottomSheet(
|
||||
DatabaseHolder.Database.watchPositionDao().insert(watchPosition)
|
||||
if (!PlayerHelper.watchHistoryEnabled) return@withContext
|
||||
// add video to watch history
|
||||
runCatching {
|
||||
RetrofitInstance.api.getStreams(videoId)
|
||||
}.getOrNull()?.let { DatabaseHelper.addToWatchHistory(videoId, it) }
|
||||
DatabaseHelper.addToWatchHistory(videoId, streamItem)
|
||||
}
|
||||
if (PreferenceHelper.getBoolean(PreferenceKeys.HIDE_WATCHED_FROM_FEED, false)) {
|
||||
// get the host fragment containing the current fragment
|
||||
|
Loading…
x
Reference in New Issue
Block a user