refactor: simplify VideoOptionsBottomSheet logic and improve its performance

This commit is contained in:
FineFindus 2023-08-28 16:22:04 +02:00
parent 594488c77e
commit c36d30629a
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
8 changed files with 56 additions and 46 deletions

View File

@ -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
)
}

View File

@ -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")

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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(

View File

@ -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)
}

View File

@ -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