From a42d5f64e2c004b2962207e3c5335d226638e149 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 30 May 2022 15:43:55 +0200 Subject: [PATCH 1/3] added video options dialog to search --- .../com/github/libretube/SearchFragment.kt | 4 +-- .../github/libretube/VideoOptionsDialog.kt | 18 +++++++++---- .../libretube/adapters/SearchAdapter.kt | 25 +++++++++++++------ 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index cbb7b8c3e..572b66009 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -27,11 +27,11 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.adapters.SearchAdapter import com.github.libretube.adapters.SearchHistoryAdapter import com.google.android.material.dialog.MaterialAlertDialogBuilder -import java.io.IOException import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import retrofit2.HttpException +import java.io.IOException class SearchFragment : Fragment() { private val TAG = "SearchFragment" @@ -229,7 +229,7 @@ class SearchFragment : Fragment() { nextPage = response.nextpage if (response.items!!.isNotEmpty()) { runOnUiThread { - searchAdapter = SearchAdapter(response.items) + searchAdapter = SearchAdapter(response.items, childFragmentManager) searchRecView.adapter = searchAdapter } } diff --git a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt index c00f485a6..da8fe5fd2 100644 --- a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt @@ -4,7 +4,9 @@ import android.app.Dialog import android.content.Context import android.os.Bundle import android.widget.ArrayAdapter +import android.widget.Toast import androidx.fragment.app.DialogFragment +import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder /** @@ -47,11 +49,17 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog } // Add Video to Playlist Dialog 1 -> { - val newFragment = AddtoPlaylistDialog() - var bundle = Bundle() - bundle.putString("videoId", videoId) - newFragment.arguments = bundle - newFragment.show(parentFragmentManager, "AddToPlaylist") + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + val token = sharedPreferences.getString("token", "") + if (token != "") { + val newFragment = AddtoPlaylistDialog() + var bundle = Bundle() + bundle.putString("videoId", videoId) + newFragment.arguments = bundle + newFragment.show(parentFragmentManager, "AddToPlaylist") + } else { + Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() + } } else -> { dialog.dismiss() diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index faaaddadd..b3a4754a0 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -9,15 +9,17 @@ import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf +import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView -import com.github.libretube.MainActivity -import com.github.libretube.PlayerFragment -import com.github.libretube.R -import com.github.libretube.formatShort +import com.github.libretube.* import com.github.libretube.obj.SearchItem import com.squareup.picasso.Picasso +import kotlinx.coroutines.NonDisposableHandle.parent -class SearchAdapter(private val searchItems: MutableList) : +class SearchAdapter( + private val searchItems: MutableList, + private val childFragmentManager: FragmentManager +) : RecyclerView.Adapter() { fun updateItems(newItems: List) { @@ -39,7 +41,7 @@ class SearchAdapter(private val searchItems: MutableList) : } val layoutInflater = LayoutInflater.from(parent.context) val cell = layoutInflater.inflate(layout, parent, false) - return CustomViewHolder1(cell) + return CustomViewHolder1(cell, childFragmentManager) } override fun onBindViewHolder(holder: CustomViewHolder1, position: Int) { @@ -56,7 +58,10 @@ class SearchAdapter(private val searchItems: MutableList) : } } -class CustomViewHolder1(private val v: View) : RecyclerView.ViewHolder(v) { +class CustomViewHolder1( + private val v: View, + private val childFragmentManager: FragmentManager +) : RecyclerView.ViewHolder(v) { private fun bindWatch(item: SearchItem) { val thumbnailImage = v.findViewById(R.id.search_thumbnail) @@ -90,6 +95,12 @@ class CustomViewHolder1(private val v: View) : RecyclerView.ViewHolder(v) { .replace(R.id.container, frag) .commitNow() } + v.setOnLongClickListener { + val videoId = item.url!!.replace("/watch?v=", "") + VideoOptionsDialog(videoId, v.context) + .show(childFragmentManager, VideoOptionsDialog.TAG) + true + } channelImage.setOnClickListener { val activity = v.context as MainActivity val bundle = bundleOf("channel_id" to item.uploaderUrl) From 16cccc0a5d31302d4e256f428f09889d8df2a6ee Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 30 May 2022 17:41:16 +0200 Subject: [PATCH 2/3] share in videooptionsdialog --- .../github/libretube/VideoOptionsDialog.kt | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt index da8fe5fd2..4422dcb82 100644 --- a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt @@ -2,12 +2,14 @@ package com.github.libretube import android.app.Dialog import android.content.Context +import android.content.Intent import android.os.Bundle import android.widget.ArrayAdapter import android.widget.Toast import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder +import java.net.URLEncoder /** * Dialog with different options for a selected video. @@ -20,7 +22,8 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog */ private val list = listOf( context.getString(R.string.playOnBackground), - context.getString(R.string.addToPlaylist) + context.getString(R.string.addToPlaylist), + context.getString(R.string.share) ) /** @@ -61,6 +64,47 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show() } } + 2 -> { + val sharedPreferences = + PreferenceManager.getDefaultSharedPreferences(requireContext()) + val instancePref = sharedPreferences.getString( + "instance", + "https://pipedapi.kavin.rocks" + )!! + val instance = "&instance=${URLEncoder.encode(instancePref, "UTF-8")}" + val shareOptions = arrayOf( + getString(R.string.piped), + getString(R.string.instance), + getString(R.string.youtube) + ) + /* + // crashes + MaterialAlertDialogBuilder(requireContext()) + .setTitle(getString(R.string.share)) + .setItems( + shareOptions + ) { _, id -> + val url = when (id) { + 0 -> "https://piped.kavin.rocks/watch?v=$videoId" + 1 -> "https://piped.kavin.rocks/watch?v=$videoId$instance" + 2 -> "https://youtu.be/$videoId" + else -> "https://piped.kavin.rocks/watch?v=$videoId" + } + dismiss() + val intent = Intent() + intent.action = Intent.ACTION_SEND + intent.putExtra(Intent.EXTRA_TEXT, url) + intent.type = "text/plain" + startActivity(Intent.createChooser(intent, "Share Url To:")) + } + .show() + */ + val intent = Intent() + intent.action = Intent.ACTION_SEND + intent.putExtra(Intent.EXTRA_TEXT, "https://piped.kavin.rocks/watch?v=$videoId") + intent.type = "text/plain" + startActivity(Intent.createChooser(intent, "Share Url To:")) + } else -> { dialog.dismiss() } From a5c0f3154b183a19b6666b87137a876a27931e33 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 30 May 2022 17:47:54 +0200 Subject: [PATCH 3/3] ktlint --- .../java/com/github/libretube/SearchFragment.kt | 2 +- .../com/github/libretube/VideoOptionsDialog.kt | 14 ++++++++------ .../com/github/libretube/adapters/SearchAdapter.kt | 7 +++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/libretube/SearchFragment.kt b/app/src/main/java/com/github/libretube/SearchFragment.kt index 572b66009..d2228beac 100644 --- a/app/src/main/java/com/github/libretube/SearchFragment.kt +++ b/app/src/main/java/com/github/libretube/SearchFragment.kt @@ -27,11 +27,11 @@ import androidx.recyclerview.widget.RecyclerView import com.github.libretube.adapters.SearchAdapter import com.github.libretube.adapters.SearchHistoryAdapter import com.google.android.material.dialog.MaterialAlertDialogBuilder +import java.io.IOException import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch import retrofit2.HttpException -import java.io.IOException class SearchFragment : Fragment() { private val TAG = "SearchFragment" diff --git a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt index 4422dcb82..8675f137b 100644 --- a/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/VideoOptionsDialog.kt @@ -9,7 +9,6 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import androidx.preference.PreferenceManager import com.google.android.material.dialog.MaterialAlertDialogBuilder -import java.net.URLEncoder /** * Dialog with different options for a selected video. @@ -52,11 +51,12 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog } // Add Video to Playlist Dialog 1 -> { - val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) + val sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(requireContext()) val token = sharedPreferences.getString("token", "") if (token != "") { val newFragment = AddtoPlaylistDialog() - var bundle = Bundle() + val bundle = Bundle() bundle.putString("videoId", videoId) newFragment.arguments = bundle newFragment.show(parentFragmentManager, "AddToPlaylist") @@ -65,6 +65,7 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog } } 2 -> { + /* crashes val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext()) val instancePref = sharedPreferences.getString( @@ -77,8 +78,6 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog getString(R.string.instance), getString(R.string.youtube) ) - /* - // crashes MaterialAlertDialogBuilder(requireContext()) .setTitle(getString(R.string.share)) .setItems( @@ -101,7 +100,10 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog */ val intent = Intent() intent.action = Intent.ACTION_SEND - intent.putExtra(Intent.EXTRA_TEXT, "https://piped.kavin.rocks/watch?v=$videoId") + intent.putExtra( + Intent.EXTRA_TEXT, + "https://piped.kavin.rocks/watch?v=$videoId" + ) intent.type = "text/plain" startActivity(Intent.createChooser(intent, "Share Url To:")) } diff --git a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt index b3a4754a0..45abc649f 100644 --- a/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/SearchAdapter.kt @@ -11,10 +11,13 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.RecyclerView -import com.github.libretube.* +import com.github.libretube.MainActivity +import com.github.libretube.PlayerFragment +import com.github.libretube.R +import com.github.libretube.VideoOptionsDialog +import com.github.libretube.formatShort import com.github.libretube.obj.SearchItem import com.squareup.picasso.Picasso -import kotlinx.coroutines.NonDisposableHandle.parent class SearchAdapter( private val searchItems: MutableList,