This commit is contained in:
Bnyro 2022-07-29 09:00:13 +02:00
parent 285a37bcd5
commit 0f1743c35a
14 changed files with 45 additions and 23 deletions

View File

@ -12,6 +12,7 @@ import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
class ChannelAdapter(
private val videoFeed: MutableList<StreamItem>,
@ -47,7 +48,7 @@ class ChannelAdapter(
root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, trending.url)
}
val videoId = trending.url!!.replace("/watch?v=", "")
val videoId = trending.url.toID()
root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog")

View File

@ -17,6 +17,7 @@ import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -57,7 +58,7 @@ class PlaylistAdapter(
root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId)
}
val videoId = streamItem.url!!.replace("/watch?v=", "")
val videoId = streamItem.url.toID()
root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog")

View File

@ -20,6 +20,7 @@ import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -102,7 +103,7 @@ class SearchAdapter(
root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, item.url)
}
val videoId = item.url!!.replace("/watch?v=", "")
val videoId = item.url.toID()
root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog")
@ -126,7 +127,7 @@ class SearchAdapter(
root.setOnClickListener {
NavigationHelper.navigateChannel(root.context, item.url)
}
val channelId = item.url?.replace("/channel/", "")!!
val channelId = item.url.toID()
val token = PreferenceHelper.getToken()
// only show subscribe button if logged in
@ -215,7 +216,7 @@ class SearchAdapter(
NavigationHelper.navigatePlaylist(root.context, item.url)
}
root.setOnLongClickListener {
val playlistId = item.url!!.replace("/playlist?list=", "")
val playlistId = item.url!!.toID()
PlaylistOptionsDialog(playlistId, false, root.context)
.show(childFragmentManager, "PlaylistOptionsDialog")
true

View File

@ -12,6 +12,7 @@ import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -42,7 +43,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
NavigationHelper.navigateChannel(root.context, subscription.url)
}
subscriptionSubscribe.setOnClickListener {
val channelId = subscription.url?.replace("/channel/", "")!!
val channelId = subscription.url.toID()
if (subscribed) {
subscriptionSubscribe.text = root.context.getString(R.string.subscribe)
unsubscribe(channelId)

View File

@ -13,6 +13,7 @@ import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
class TrendingAdapter(
private val streamItems: List<StreamItem>,
@ -64,7 +65,7 @@ class TrendingAdapter(
root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, trending.url)
}
val videoId = trending.url!!.replace("/watch?v=", "")
val videoId = trending.url!!.toID()
root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog")

View File

@ -17,6 +17,7 @@ import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.github.libretube.util.toID
import retrofit2.HttpException
import java.io.IOException
@ -35,7 +36,7 @@ class ChannelFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
channelId = it.getString("channel_id")?.replace("/channel/", "")
channelId = it.getString("channel_id").toID()
channelName = it.getString("channel_name")
?.replace("/c/", "")
?.replace("/user/", "")

View File

@ -65,6 +65,7 @@ import com.github.libretube.util.PlayerHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort
import com.github.libretube.util.hideKeyboard
import com.github.libretube.util.toID
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.ExoPlayer
@ -343,7 +344,7 @@ class PlayerFragment : Fragment() {
}
private fun initializeTransitionLayout() {
videoId = videoId!!.replace("/watch?v=", "")
videoId = videoId.toID()
val mainActivity = activity as MainActivity
mainActivity.binding.container.visibility = View.VISIBLE
@ -751,7 +752,7 @@ class PlayerFragment : Fragment() {
title = response.title!!
uploader = response.uploader!!
thumbnailUrl = response.thumbnailUrl!!
channelId = response.uploaderUrl?.replace("/channel/", "")
channelId = response.uploaderUrl.toID()
// save related streams for autoplay
relatedStreams = response.relatedStreams
@ -809,7 +810,7 @@ class PlayerFragment : Fragment() {
playlist = RetrofitInstance.api.getPlaylist(playlistId!!)
// save the playlist urls in the array
playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!!
playlistStreamIds += video.url?.toID()
}
// save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage
@ -830,7 +831,7 @@ class PlayerFragment : Fragment() {
RetrofitInstance.api.getPlaylistNextPage(playlistId!!, playlistNextPage!!)
// append all the playlist item urls to the array
playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!!
playlistStreamIds += video.url.toID()
}
// save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage
@ -843,7 +844,7 @@ class PlayerFragment : Fragment() {
// if it's not a playlist then use the next related video
} else if (relatedStreams != null && relatedStreams!!.isNotEmpty()) {
// save next video from related streams for autoplay
nextStreamId = relatedStreams!![0].url!!.replace("/watch?v=", "")
nextStreamId = relatedStreams!![0].url.toID()
}
}
}
@ -1090,7 +1091,7 @@ class PlayerFragment : Fragment() {
binding.playerMotionLayout.transitionToEnd()
}
if (token != "") {
val channelId = response.uploaderUrl?.replace("/channel/", "")
val channelId = response.uploaderUrl?.toID()
isSubscribed()
binding.relPlayerSave.setOnClickListener {
val newFragment = AddtoPlaylistDialog()

View File

@ -14,6 +14,7 @@ import com.github.libretube.databinding.FragmentPlaylistBinding
import com.github.libretube.dialogs.PlaylistOptionsDialog
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException
import java.io.IOException
@ -46,7 +47,7 @@ class PlaylistFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
playlistId = playlistId!!.replace("/playlist?list=", "")
playlistId = playlistId!!.toID()
binding.playlistRecView.layoutManager = LinearLayoutManager(context)
binding.playlistProgress.visibility = View.VISIBLE

View File

@ -20,6 +20,7 @@ import com.github.libretube.databinding.FragmentSubscriptionsBinding
import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException
import java.io.IOException
@ -159,7 +160,7 @@ class SubscriptionsFragment : Fragment() {
}
if (response.isNotEmpty()) {
// save the last recent video to the prefs for the notification worker
PreferenceHelper.setLatestVideoId(response[0].url.toString().replace("/watch?v=", ""))
PreferenceHelper.setLatestVideoId(response[0].url.toID())
channelRecView.adapter = SubscriptionChannelAdapter(response.toMutableList())
} else {
Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show()

View File

@ -8,6 +8,7 @@ import com.github.libretube.obj.CustomInstance
import com.github.libretube.obj.Streams
import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.obj.WatchPosition
import com.github.libretube.util.toID
object PreferenceHelper {
private val TAG = "PreferenceHelper"
@ -129,7 +130,7 @@ object PreferenceHelper {
streams.title,
streams.uploadDate,
streams.uploader,
streams.uploaderUrl?.replace("/channel/", ""),
streams.uploaderUrl.toID(),
streams.uploaderAvatar,
streams.thumbnailUrl,
streams.duration

View File

@ -15,6 +15,7 @@ import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.DescriptionAdapter
import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
@ -166,8 +167,7 @@ class BackgroundMode : Service() {
private fun playNextVideo() {
if (response!!.relatedStreams!!.isNotEmpty()) {
val videoId = response!!
.relatedStreams!![0].url!!
.replace("/watch?v=", "")
.relatedStreams!![0].url.toID()
// destroy previous notification and player
destroyPlayer()

View File

@ -31,7 +31,7 @@ object NavigationHelper {
fun navigateVideo(context: Context, videoId: String?, playlistId: String? = null) {
if (videoId != null) {
val bundle = Bundle()
bundle.putString("videoId", videoId.replace("/watch?v=", ""))
bundle.putString("videoId", videoId.toID())
if (playlistId != null) bundle.putString("playlistId", playlistId)
val frag = PlayerFragment()
frag.arguments = bundle

View File

@ -83,7 +83,7 @@ object NotificationHelper {
}
val lastSeenStreamId = PreferenceHelper.getLatestVideoId()
val latestFeedStreamId = videoFeed[0].url?.replace("/watch?v=", "")
val latestFeedStreamId = videoFeed[0].url.toID()
// first time notifications enabled
if (lastSeenStreamId == "") PreferenceHelper.setLatestVideoId(lastSeenStreamId)
@ -91,7 +91,7 @@ object NotificationHelper {
// get the index of the last user-seen video
var newStreamIndex = -1
videoFeed.forEachIndexed { index, stream ->
if (stream.url?.replace("/watch?v=", "") == lastSeenStreamId) {
if (stream.url?.toID() == lastSeenStreamId) {
newStreamIndex = index
}
}
@ -117,7 +117,7 @@ object NotificationHelper {
}
}
// save the id of the last recent video for the next time it's running
PreferenceHelper.setLatestVideoId(videoFeed[0].url?.replace("/watch?v=", "")!!)
PreferenceHelper.setLatestVideoId(videoFeed[0].url.toID())
createNotification(context, title!!, description!!)
}
}

View File

@ -0,0 +1,12 @@
package com.github.libretube.util
/**
* format a Piped route to an ID
*/
fun Any?.toID(): String {
return this!!
.toString()
.replace("/watch?v=", "") // videos
.replace("/channel/", "") // channels
.replace("/playlist?list=", "") // playlists
}