From 64751dfd96b3cdd56c553291e0cb1b332b5903bf Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 17 Jul 2022 22:06:43 +0200 Subject: [PATCH] support for opening links with /c/ or /user/ --- .../libretube/activities/MainActivity.kt | 125 ++++++++++-------- .../libretube/fragments/ChannelFragment.kt | 13 +- .../libretube/fragments/PlayerFragment.kt | 10 +- .../libretube/preferences/PreferenceHelper.kt | 6 +- .../com/github/libretube/util/PipedApi.kt | 3 + .../layout/exo_styled_player_control_view.xml | 6 +- app/src/main/res/layout/fragment_player.xml | 3 +- 7 files changed, 94 insertions(+), 72 deletions(-) diff --git a/app/src/main/java/com/github/libretube/activities/MainActivity.kt b/app/src/main/java/com/github/libretube/activities/MainActivity.kt index 5b614a4cd..fad02aa03 100644 --- a/app/src/main/java/com/github/libretube/activities/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/activities/MainActivity.kt @@ -169,92 +169,93 @@ class MainActivity : AppCompatActivity() { val intentData: Uri? = intent?.data // check whether an URI got submitted over the intent data if (intentData != null && intentData.host != null && intentData.path != null) { - Log.d("intentData", "${intentData.host} ${intentData.path} ") + Log.d(TAG, "intentData: ${intentData.host} ${intentData.path} ") // load the URI of the submitted link (e.g. video) loadIntentData(intentData) } } private fun loadIntentData(data: Uri) { - // channel - if (data.path!!.contains("/channel/") || + if (data.path!!.contains("/channel/") + ) { + val channelId = data.path!! + .replace("/channel/", "") + + loadChannel(channelId = channelId) + } else if ( data.path!!.contains("/c/") || data.path!!.contains("/user/") ) { - Log.i(TAG, "URI Type: Channel") - var channel = data.path - channel = channel!!.replace("/c/", "") - channel = channel.replace("/user/", "") - val bundle = bundleOf("channel_id" to channel) - navController.navigate(R.id.channelFragment, bundle) - } else if (data.path!!.contains("/playlist")) { - Log.i(TAG, "URI Type: Playlist") - var playlist = data.query!! - if (playlist.contains("&")) { - val playlists = playlist.split("&") - for (v in playlists) { + val channelName = data.path!! + .replace("/c/", "") + .replace("/user/", "") + + loadChannel(channelName = channelName) + } else if ( + data.path!!.contains("/playlist") + ) { + var playlistId = data.query!! + if (playlistId.contains("&")) { + for (v in playlistId.split("&")) { if (v.contains("list=")) { - playlist = v + playlistId = v.replace("list=", "") break } } } - playlist = playlist.replace("list=", "") - val bundle = bundleOf("playlist_id" to playlist) - navController.navigate(R.id.playlistFragment, bundle) - } else if (data.path!!.contains("/shorts/") || + + loadPlaylist(playlistId) + } else if ( + data.path!!.contains("/shorts/") || data.path!!.contains("/embed/") || data.path!!.contains("/v/") ) { - Log.i(TAG, "URI Type: Video") - val watch = data.path!! + val videoId = data.path!! .replace("/shorts/", "") .replace("/v/", "") .replace("/embed/", "") - val bundle = Bundle() - bundle.putString("videoId", watch) - // for time stamped links - if (data.query != null && data.query?.contains("t=")!!) { - val timeStamp = data.query.toString().split("t=")[1] - bundle.putLong("timeStamp", timeStamp.toLong()) - } - loadWatch(bundle) + + loadVideo(videoId, data.query) } else if (data.path!!.contains("/watch") && data.query != null) { - Log.d("dafaq", data.query!!) - var watch = data.query!! - if (watch.contains("&")) { - val watches = watch.split("&") + var videoId = data.query!! + + if (videoId.contains("&")) { + val watches = videoId.split("&") for (v in watches) { if (v.contains("v=")) { - watch = v + videoId = v.replace("v=", "") break } } + } else { + videoId = videoId + .replace("v=", "") } - val bundle = Bundle() - bundle.putString("videoId", watch.replace("v=", "")) - // for time stamped links - if (data.query != null && data.query?.contains("t=")!!) { - val timeStamp = data.query.toString().split("t=")[1] - bundle.putLong("timeStamp", timeStamp.toLong()) - } - loadWatch(bundle) + + loadVideo(videoId, data.query) } else { - val watch = data.path!!.replace("/", "") - val bundle = Bundle() - bundle.putString("videoId", watch) - // for time stamped links - if (data.query != null && data.query?.contains("t=")!!) { - val timeStamp = data.query.toString().split("t=")[1] - bundle.putLong("timeStamp", timeStamp.toLong()) - } - loadWatch(bundle) + val videoId = data.path!!.replace("/", "") + + loadVideo(videoId, data.query) } } - private fun loadWatch(bundle: Bundle) { + private fun loadVideo(videoId: String, query: String?) { + Log.i(TAG, "URI type: Video") + + val bundle = Bundle() + Log.e(TAG, videoId) + + // for time stamped links + if (query != null && query.contains("t=")) { + val timeStamp = query.toString().split("t=")[1] + bundle.putLong("timeStamp", timeStamp.toLong()) + } + + bundle.putString("videoId", videoId) val frag = PlayerFragment() frag.arguments = bundle + supportFragmentManager.beginTransaction() .remove(PlayerFragment()) .commit() @@ -268,6 +269,24 @@ class MainActivity : AppCompatActivity() { }, 100) } + private fun loadChannel( + channelId: String? = null, + channelName: String? = null + ) { + Log.i(TAG, "Uri Type: Channel") + + val bundle = if (channelId != null) bundleOf("channel_id" to channelId) + else bundleOf("channel_name" to channelName) + navController.navigate(R.id.channelFragment, bundle) + } + + private fun loadPlaylist(playlistId: String) { + Log.i(TAG, "Uri Type: Playlist") + + val bundle = bundleOf("playlist_id" to playlistId) + navController.navigate(R.id.playlistFragment, bundle) + } + override fun onBackPressed() { if (binding.mainMotionLayout.progress == 0F) { try { diff --git a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt index 89f4215f9..7679277bc 100644 --- a/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/ChannelFragment.kt @@ -25,6 +25,8 @@ class ChannelFragment : Fragment() { private lateinit var binding: FragmentChannelBinding private var channelId: String? = null + private var channelName: String? = null + var nextPage: String? = null private var channelAdapter: ChannelAdapter? = null private var isLoading = true @@ -33,7 +35,10 @@ class ChannelFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { - channelId = it.getString("channel_id") + channelId = it.getString("channel_id")?.replace("/channel/", "") + channelName = it.getString("channel_name") + ?.replace("/c/", "") + ?.replace("/user/", "") } } @@ -49,7 +54,6 @@ class ChannelFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - channelId = channelId!!.replace("/channel/", "") binding.channelName.text = channelId binding.channelRecView.layoutManager = LinearLayoutManager(context) @@ -159,7 +163,8 @@ class ChannelFragment : Fragment() { fun run() { lifecycleScope.launchWhenCreated { val response = try { - RetrofitInstance.api.getChannel(channelId!!) + if (channelId != null) RetrofitInstance.api.getChannel(channelId!!) + else RetrofitInstance.api.getChannelByName(channelName!!) } catch (e: IOException) { binding.channelRefresh.isRefreshing = false println(e) @@ -196,6 +201,8 @@ class ChannelFragment : Fragment() { ConnectionHelper.loadImage(response.bannerUrl, binding.channelBanner) ConnectionHelper.loadImage(response.avatarUrl, binding.channelImage) + + // recyclerview of the videos by the channel channelAdapter = ChannelAdapter( response.relatedStreams!!.toMutableList(), childFragmentManager 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 67e56ba2e..8fd09cfe2 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -1079,7 +1079,7 @@ class PlayerFragment : Fragment() { } override fun onSingleTapConfirmed(e: MotionEvent?): Boolean { - toggleController() + binding.player.performClick() return super.onSingleTapConfirmed(e) } } @@ -1108,7 +1108,7 @@ class PlayerFragment : Fragment() { } override fun onSingleTapConfirmed(e: MotionEvent?): Boolean { - toggleController() + binding.player.performClick() return super.onSingleTapConfirmed(e) } } @@ -1130,12 +1130,6 @@ class PlayerFragment : Fragment() { binding.rewindFL.visibility = View.GONE } - // toggle the visibility of the player controller - private fun toggleController() { - if (exoPlayerView.isControllerFullyVisible) exoPlayerView.hideController() - else exoPlayerView.showController() - } - // enable seek bar preview private fun enableSeekbarPreview() { playerBinding.exoProgress.addListener(object : TimeBar.OnScrubListener { diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 9b73b23a8..dfb53f198 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -24,7 +24,7 @@ object PreferenceHelper { fun setContext(context: Context) { prefContext = context settings = getDefaultSharedPreferences(prefContext) - editor = getDefaultSharedPreferencesEditor(prefContext) + editor = settings.edit() } fun setString(key: String?, value: String?) { @@ -215,8 +215,4 @@ object PreferenceHelper { private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) } - - private fun getDefaultSharedPreferencesEditor(context: Context): SharedPreferences.Editor { - return getDefaultSharedPreferences(context).edit() - } } diff --git a/app/src/main/java/com/github/libretube/util/PipedApi.kt b/app/src/main/java/com/github/libretube/util/PipedApi.kt index ca0f791a5..394da668e 100644 --- a/app/src/main/java/com/github/libretube/util/PipedApi.kt +++ b/app/src/main/java/com/github/libretube/util/PipedApi.kt @@ -66,6 +66,9 @@ interface PipedApi { @GET("channel/{channelId}") suspend fun getChannel(@Path("channelId") channelId: String): Channel + @GET("user/{name}") + suspend fun getChannelByName(@Path("name") channelName: String): Channel + @GET("nextpage/channel/{channelId}") suspend fun getChannelNextPage( @Path("channelId") channelId: String, diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index 43d300c00..5502c1eb0 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -15,6 +15,7 @@ android:layout_gravity="top" android:animateLayoutChanges="true" android:orientation="vertical" + android:paddingTop="5dp" android:paddingStart="5dp" android:paddingEnd="10dp"> @@ -90,8 +91,9 @@ android:id="@+id/advanced_options" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="right" - android:layout_marginTop="-10dp" + android:layout_gravity="end" + android:layout_marginEnd="3dp" + android:layout_marginTop="-12dp" android:orientation="horizontal" android:visibility="gone"> diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index 8f51f28c6..9999c6d05 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -379,7 +379,8 @@ android:id="@+id/doubleTapOverlayLL" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_marginVertical="60dp"> + android:layout_marginTop="70dp" + android:layout_marginBottom="60dp">