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