From a6cd8a8c127e43aa9350eb301722102fb30443f1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Wed, 27 Jul 2022 11:17:05 +0200 Subject: [PATCH] fixes --- .../libretube/fragments/PlayerFragment.kt | 36 +++++++++++-------- ...istener.kt => OnDoubleTapEventListener.kt} | 2 +- .../libretube/views/CustomExoPlayerView.kt | 14 ++++---- 3 files changed, 31 insertions(+), 21 deletions(-) rename app/src/main/java/com/github/libretube/util/{OnCustomEventListener.kt => OnDoubleTapEventListener.kt} (63%) diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 8f47a6add..780ef60ef 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -60,7 +60,7 @@ import com.github.libretube.util.BackgroundHelper import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.CronetHelper import com.github.libretube.util.DescriptionAdapter -import com.github.libretube.util.OnCustomEventListener +import com.github.libretube.util.OnDoubleTapEventListener import com.github.libretube.util.PlayerHelper import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.formatShort @@ -90,7 +90,6 @@ import com.google.android.exoplayer2.upstream.DefaultDataSource import com.google.android.exoplayer2.upstream.DefaultHttpDataSource import com.google.android.exoplayer2.util.RepeatModeUtil import com.google.android.exoplayer2.video.VideoSize -import com.google.android.material.button.MaterialButton import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -113,6 +112,7 @@ class PlayerFragment : Fragment() { */ private var videoId: String? = null private var playlistId: String? = null + private var channelId: String? = null private var isSubscribed: Boolean = false /** @@ -740,6 +740,7 @@ class PlayerFragment : Fragment() { title = response.title!! uploader = response.uploader!! thumbnailUrl = response.thumbnailUrl!! + channelId = response.uploaderUrl?.replace("/channel/", "") // save related streams for autoplay relatedStreams = response.relatedStreams @@ -1078,7 +1079,7 @@ class PlayerFragment : Fragment() { } if (token != "") { val channelId = response.uploaderUrl?.replace("/channel/", "") - isSubscribed(binding.playerSubscribe, channelId!!) + isSubscribed() binding.relPlayerSave.setOnClickListener { val newFragment = AddtoPlaylistDialog() val bundle = Bundle() @@ -1086,6 +1087,10 @@ class PlayerFragment : Fragment() { newFragment.arguments = bundle newFragment.show(childFragmentManager, "AddToPlaylist") } + } else { + binding.relPlayerSave.setOnClickListener { + Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() + } } } @@ -1095,7 +1100,7 @@ class PlayerFragment : Fragment() { doubleTapOverlayBinding.rewindTV.text = seekIncrementText doubleTapOverlayBinding.forwardTV.text = seekIncrementText binding.player.setOnDoubleTapListener( - object : OnCustomEventListener { + object : OnDoubleTapEventListener { override fun onEvent(x: Float) { val width = exoPlayerView.width when { @@ -1527,12 +1532,12 @@ class PlayerFragment : Fragment() { } } - private fun isSubscribed(button: MaterialButton, channel_id: String) { + private fun isSubscribed() { fun run() { lifecycleScope.launchWhenCreated { val response = try { RetrofitInstance.authApi.isSubscribed( - channel_id, + channelId!!, token ) } catch (e: IOException) { @@ -1547,18 +1552,21 @@ class PlayerFragment : Fragment() { runOnUiThread { if (response.subscribed == true) { isSubscribed = true - button.text = getString(R.string.unsubscribe) + binding.playerSubscribe.text = getString(R.string.unsubscribe) } if (response.subscribed != null) { - button.setOnClickListener { + binding.playerSubscribe.setOnClickListener { if (isSubscribed) { - unsubscribe(channel_id) - button.text = getString(R.string.subscribe) + unsubscribe(channelId!!) + binding.playerSubscribe.text = getString(R.string.subscribe) } else { - subscribe(channel_id) - button.text = getString(R.string.unsubscribe) + subscribe(channelId!!) + binding.playerSubscribe.text = getString(R.string.unsubscribe) } } + } else { + Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT) + .show() } } } @@ -1566,13 +1574,13 @@ class PlayerFragment : Fragment() { run() } - private fun subscribe(channel_id: String) { + private fun subscribe(channelId: String) { fun run() { lifecycleScope.launchWhenCreated { try { RetrofitInstance.authApi.subscribe( token, - Subscribe(channel_id) + Subscribe(channelId) ) } catch (e: IOException) { println(e) diff --git a/app/src/main/java/com/github/libretube/util/OnCustomEventListener.kt b/app/src/main/java/com/github/libretube/util/OnDoubleTapEventListener.kt similarity index 63% rename from app/src/main/java/com/github/libretube/util/OnCustomEventListener.kt rename to app/src/main/java/com/github/libretube/util/OnDoubleTapEventListener.kt index 8884b00f1..d9b135f06 100644 --- a/app/src/main/java/com/github/libretube/util/OnCustomEventListener.kt +++ b/app/src/main/java/com/github/libretube/util/OnDoubleTapEventListener.kt @@ -1,5 +1,5 @@ package com.github.libretube.util -interface OnCustomEventListener { +interface OnDoubleTapEventListener { fun onEvent(x: Float) } diff --git a/app/src/main/java/com/github/libretube/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/views/CustomExoPlayerView.kt index dbcbae23e..3557cc3a7 100644 --- a/app/src/main/java/com/github/libretube/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/views/CustomExoPlayerView.kt @@ -8,7 +8,7 @@ import android.view.View import com.github.libretube.R import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.util.DoubleTapListener -import com.github.libretube.util.OnCustomEventListener +import com.github.libretube.util.OnDoubleTapEventListener import com.google.android.exoplayer2.ui.StyledPlayerView @SuppressLint("ClickableViewAccessibility") @@ -19,19 +19,18 @@ internal class CustomExoPlayerView( val TAG = "CustomExoPlayerView" val binding: ExoStyledPlayerControlViewBinding = ExoStyledPlayerControlViewBinding.bind(this) - var doubleTapListener: OnCustomEventListener? = null + private var doubleTapListener: OnDoubleTapEventListener? = null - var lastToggled: Long? = null - var xPos = 0F + // the x-position of where the user clicked + private var xPos = 0F fun setOnDoubleTapListener( - eventListener: OnCustomEventListener? + eventListener: OnDoubleTapEventListener? ) { doubleTapListener = eventListener } private fun toggleController() { - lastToggled = System.currentTimeMillis() if (isControllerFullyVisible) hideController() else showController() } @@ -51,6 +50,7 @@ internal class CustomExoPlayerView( binding.toggleOptions.animate().rotation(0F).setDuration(250).start() binding.advancedOptions.visibility = View.GONE } + // set the double click listener for rewind/forward setOnClickListener(doubleTouchListener) } @@ -75,7 +75,9 @@ internal class CustomExoPlayerView( } override fun onTouchEvent(event: MotionEvent): Boolean { + // save the x position of the touch event xPos = event.x + // listen for a double touch doubleTouchListener.onClick(this) return false }