Merge pull request #911 from Bnyro/master

cleanup
This commit is contained in:
Bnyro 2022-07-29 09:15:51 +02:00 committed by GitHub
commit 691dc2e363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 47 additions and 27 deletions

View File

@ -12,6 +12,7 @@ import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
class ChannelAdapter( class ChannelAdapter(
private val videoFeed: MutableList<StreamItem>, private val videoFeed: MutableList<StreamItem>,
@ -47,7 +48,7 @@ class ChannelAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, trending.url) NavigationHelper.navigateVideo(root.context, trending.url)
} }
val videoId = trending.url!!.replace("/watch?v=", "") val videoId = trending.url.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context) VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog") .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.NavigationHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.setWatchProgressLength import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -57,7 +58,7 @@ class PlaylistAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId) NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId)
} }
val videoId = streamItem.url!!.replace("/watch?v=", "") val videoId = streamItem.url.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context) VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog") .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.RetrofitInstance
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -102,7 +103,7 @@ class SearchAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, item.url) NavigationHelper.navigateVideo(root.context, item.url)
} }
val videoId = item.url!!.replace("/watch?v=", "") val videoId = item.url.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context) VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog") .show(childFragmentManager, "VideoOptionsDialog")
@ -126,7 +127,7 @@ class SearchAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateChannel(root.context, item.url) NavigationHelper.navigateChannel(root.context, item.url)
} }
val channelId = item.url?.replace("/channel/", "")!! val channelId = item.url.toID()
val token = PreferenceHelper.getToken() val token = PreferenceHelper.getToken()
// only show subscribe button if logged in // only show subscribe button if logged in
@ -215,7 +216,7 @@ class SearchAdapter(
NavigationHelper.navigatePlaylist(root.context, item.url) NavigationHelper.navigatePlaylist(root.context, item.url)
} }
root.setOnLongClickListener { root.setOnLongClickListener {
val playlistId = item.url!!.replace("/playlist?list=", "") val playlistId = item.url!!.toID()
PlaylistOptionsDialog(playlistId, false, root.context) PlaylistOptionsDialog(playlistId, false, root.context)
.show(childFragmentManager, "PlaylistOptionsDialog") .show(childFragmentManager, "PlaylistOptionsDialog")
true true

View File

@ -12,6 +12,7 @@ import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -42,7 +43,7 @@ class SubscriptionChannelAdapter(private val subscriptions: MutableList<Subscrip
NavigationHelper.navigateChannel(root.context, subscription.url) NavigationHelper.navigateChannel(root.context, subscription.url)
} }
subscriptionSubscribe.setOnClickListener { subscriptionSubscribe.setOnClickListener {
val channelId = subscription.url?.replace("/channel/", "")!! val channelId = subscription.url.toID()
if (subscribed) { if (subscribed) {
subscriptionSubscribe.text = root.context.getString(R.string.subscribe) subscriptionSubscribe.text = root.context.getString(R.string.subscribe)
unsubscribe(channelId) 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.NavigationHelper
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.github.libretube.util.setWatchProgressLength import com.github.libretube.util.setWatchProgressLength
import com.github.libretube.util.toID
class TrendingAdapter( class TrendingAdapter(
private val streamItems: List<StreamItem>, private val streamItems: List<StreamItem>,
@ -64,7 +65,7 @@ class TrendingAdapter(
root.setOnClickListener { root.setOnClickListener {
NavigationHelper.navigateVideo(root.context, trending.url) NavigationHelper.navigateVideo(root.context, trending.url)
} }
val videoId = trending.url!!.replace("/watch?v=", "") val videoId = trending.url!!.toID()
root.setOnLongClickListener { root.setOnLongClickListener {
VideoOptionsDialog(videoId, root.context) VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, "VideoOptionsDialog") .show(childFragmentManager, "VideoOptionsDialog")

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
}

View File

@ -17,6 +17,7 @@ import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.github.libretube.util.toID
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -35,7 +36,7 @@ class ChannelFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
arguments?.let { arguments?.let {
channelId = it.getString("channel_id")?.replace("/channel/", "") channelId = it.getString("channel_id").toID()
channelName = it.getString("channel_name") channelName = it.getString("channel_name")
?.replace("/c/", "") ?.replace("/c/", "")
?.replace("/user/", "") ?.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.RetrofitInstance
import com.github.libretube.util.formatShort import com.github.libretube.util.formatShort
import com.github.libretube.util.hideKeyboard import com.github.libretube.util.hideKeyboard
import com.github.libretube.util.toID
import com.google.android.exoplayer2.C import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.DefaultLoadControl import com.google.android.exoplayer2.DefaultLoadControl
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
@ -343,7 +344,7 @@ class PlayerFragment : Fragment() {
} }
private fun initializeTransitionLayout() { private fun initializeTransitionLayout() {
videoId = videoId!!.replace("/watch?v=", "") videoId = videoId.toID()
val mainActivity = activity as MainActivity val mainActivity = activity as MainActivity
mainActivity.binding.container.visibility = View.VISIBLE mainActivity.binding.container.visibility = View.VISIBLE
@ -751,7 +752,7 @@ class PlayerFragment : Fragment() {
title = response.title!! title = response.title!!
uploader = response.uploader!! uploader = response.uploader!!
thumbnailUrl = response.thumbnailUrl!! thumbnailUrl = response.thumbnailUrl!!
channelId = response.uploaderUrl?.replace("/channel/", "") channelId = response.uploaderUrl.toID()
// save related streams for autoplay // save related streams for autoplay
relatedStreams = response.relatedStreams relatedStreams = response.relatedStreams
@ -809,7 +810,7 @@ class PlayerFragment : Fragment() {
playlist = RetrofitInstance.api.getPlaylist(playlistId!!) playlist = RetrofitInstance.api.getPlaylist(playlistId!!)
// save the playlist urls in the array // save the playlist urls in the array
playlist.relatedStreams?.forEach { video -> playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!! playlistStreamIds += video.url.toID()
} }
// save playlistNextPage for usage if video is not contained // save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage playlistNextPage = playlist.nextpage
@ -830,7 +831,7 @@ class PlayerFragment : Fragment() {
RetrofitInstance.api.getPlaylistNextPage(playlistId!!, playlistNextPage!!) RetrofitInstance.api.getPlaylistNextPage(playlistId!!, playlistNextPage!!)
// append all the playlist item urls to the array // append all the playlist item urls to the array
playlist.relatedStreams?.forEach { video -> playlist.relatedStreams?.forEach { video ->
playlistStreamIds += video.url?.replace("/watch?v=", "")!! playlistStreamIds += video.url.toID()
} }
// save playlistNextPage for usage if video is not contained // save playlistNextPage for usage if video is not contained
playlistNextPage = playlist.nextpage playlistNextPage = playlist.nextpage
@ -843,7 +844,7 @@ class PlayerFragment : Fragment() {
// if it's not a playlist then use the next related video // if it's not a playlist then use the next related video
} else if (relatedStreams != null && relatedStreams!!.isNotEmpty()) { } else if (relatedStreams != null && relatedStreams!!.isNotEmpty()) {
// save next video from related streams for autoplay // 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() binding.playerMotionLayout.transitionToEnd()
} }
if (token != "") { if (token != "") {
val channelId = response.uploaderUrl?.replace("/channel/", "") val channelId = response.uploaderUrl?.toID()
isSubscribed() isSubscribed()
binding.relPlayerSave.setOnClickListener { binding.relPlayerSave.setOnClickListener {
val newFragment = AddtoPlaylistDialog() 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.dialogs.PlaylistOptionsDialog
import com.github.libretube.preferences.PreferenceHelper import com.github.libretube.preferences.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -46,7 +47,7 @@ class PlaylistFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
playlistId = playlistId!!.replace("/playlist?list=", "") playlistId = playlistId!!.toID()
binding.playlistRecView.layoutManager = LinearLayoutManager(context) binding.playlistRecView.layoutManager = LinearLayoutManager(context)
binding.playlistProgress.visibility = View.VISIBLE 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.PreferenceHelper
import com.github.libretube.preferences.PreferenceKeys import com.github.libretube.preferences.PreferenceKeys
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.github.libretube.util.toID
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
@ -159,7 +160,7 @@ class SubscriptionsFragment : Fragment() {
} }
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
// save the last recent video to the prefs for the notification worker // 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()) channelRecView.adapter = SubscriptionChannelAdapter(response.toMutableList())
} else { } else {
Toast.makeText(context, R.string.subscribeIsEmpty, Toast.LENGTH_SHORT).show() 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.Streams
import com.github.libretube.obj.WatchHistoryItem import com.github.libretube.obj.WatchHistoryItem
import com.github.libretube.obj.WatchPosition import com.github.libretube.obj.WatchPosition
import com.github.libretube.util.toID
object PreferenceHelper { object PreferenceHelper {
private val TAG = "PreferenceHelper" private val TAG = "PreferenceHelper"
@ -15,7 +16,7 @@ object PreferenceHelper {
private lateinit var prefContext: Context private lateinit var prefContext: Context
private lateinit var settings: SharedPreferences private lateinit var settings: SharedPreferences
private lateinit var editor: SharedPreferences.Editor private lateinit var editor: SharedPreferences.Editor
val mapper = ObjectMapper() private val mapper = ObjectMapper()
/** /**
* set the context that is being used to access the shared preferences * set the context that is being used to access the shared preferences
@ -129,13 +130,12 @@ object PreferenceHelper {
streams.title, streams.title,
streams.uploadDate, streams.uploadDate,
streams.uploader, streams.uploader,
streams.uploaderUrl?.replace("/channel/", ""), streams.uploaderUrl.toID(),
streams.uploaderAvatar, streams.uploaderAvatar,
streams.thumbnailUrl, streams.thumbnailUrl,
streams.duration streams.duration
) )
val mapper = ObjectMapper()
val watchHistory = getWatchHistory() val watchHistory = getWatchHistory()
watchHistory += watchHistoryItem watchHistory += watchHistoryItem
@ -145,7 +145,6 @@ object PreferenceHelper {
} }
fun removeFromWatchHistory(videoId: String) { fun removeFromWatchHistory(videoId: String) {
val mapper = ObjectMapper()
val watchHistory = getWatchHistory() val watchHistory = getWatchHistory()
var indexToRemove: Int? = null var indexToRemove: Int? = null
@ -219,7 +218,7 @@ object PreferenceHelper {
} }
fun setLatestVideoId(videoId: String) { fun setLatestVideoId(videoId: String) {
setString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId) editor.putString(PreferenceKeys.LAST_STREAM_VIDEO_ID, videoId)
} }
fun getLatestVideoId(): String { fun getLatestVideoId(): String {

View File

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

View File

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

View File

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