Merge pull request #333 from Bnyro/optionsdialog

Optionsdialog Share and on Search
This commit is contained in:
Bnyro 2022-05-30 17:49:20 +02:00 committed by GitHub
commit 860beff11a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 10 deletions

View File

@ -229,7 +229,7 @@ class SearchFragment : Fragment() {
nextPage = response.nextpage nextPage = response.nextpage
if (response.items!!.isNotEmpty()) { if (response.items!!.isNotEmpty()) {
runOnUiThread { runOnUiThread {
searchAdapter = SearchAdapter(response.items) searchAdapter = SearchAdapter(response.items, childFragmentManager)
searchRecView.adapter = searchAdapter searchRecView.adapter = searchAdapter
} }
} }

View File

@ -2,9 +2,12 @@ package com.github.libretube
import android.app.Dialog import android.app.Dialog
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.preference.PreferenceManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
/** /**
@ -18,7 +21,8 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog
*/ */
private val list = listOf( private val list = listOf(
context.getString(R.string.playOnBackground), context.getString(R.string.playOnBackground),
context.getString(R.string.addToPlaylist) context.getString(R.string.addToPlaylist),
context.getString(R.string.share)
) )
/** /**
@ -47,11 +51,61 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog
} }
// Add Video to Playlist Dialog // Add Video to Playlist Dialog
1 -> { 1 -> {
val newFragment = AddtoPlaylistDialog() val sharedPreferences = PreferenceManager
var bundle = Bundle() .getDefaultSharedPreferences(requireContext())
bundle.putString("videoId", videoId) val token = sharedPreferences.getString("token", "")
newFragment.arguments = bundle if (token != "") {
newFragment.show(parentFragmentManager, "AddToPlaylist") val newFragment = AddtoPlaylistDialog()
val 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()
}
}
2 -> {
/* crashes
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)
)
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 -> { else -> {
dialog.dismiss() dialog.dismiss()

View File

@ -9,15 +9,20 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.PlayerFragment import com.github.libretube.PlayerFragment
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.VideoOptionsDialog
import com.github.libretube.formatShort import com.github.libretube.formatShort
import com.github.libretube.obj.SearchItem import com.github.libretube.obj.SearchItem
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
class SearchAdapter(private val searchItems: MutableList<SearchItem>) : class SearchAdapter(
private val searchItems: MutableList<SearchItem>,
private val childFragmentManager: FragmentManager
) :
RecyclerView.Adapter<CustomViewHolder1>() { RecyclerView.Adapter<CustomViewHolder1>() {
fun updateItems(newItems: List<SearchItem>) { fun updateItems(newItems: List<SearchItem>) {
@ -39,7 +44,7 @@ class SearchAdapter(private val searchItems: MutableList<SearchItem>) :
} }
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val cell = layoutInflater.inflate(layout, parent, false) val cell = layoutInflater.inflate(layout, parent, false)
return CustomViewHolder1(cell) return CustomViewHolder1(cell, childFragmentManager)
} }
override fun onBindViewHolder(holder: CustomViewHolder1, position: Int) { override fun onBindViewHolder(holder: CustomViewHolder1, position: Int) {
@ -56,7 +61,10 @@ class SearchAdapter(private val searchItems: MutableList<SearchItem>) :
} }
} }
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) { private fun bindWatch(item: SearchItem) {
val thumbnailImage = v.findViewById<ImageView>(R.id.search_thumbnail) val thumbnailImage = v.findViewById<ImageView>(R.id.search_thumbnail)
@ -90,6 +98,12 @@ class CustomViewHolder1(private val v: View) : RecyclerView.ViewHolder(v) {
.replace(R.id.container, frag) .replace(R.id.container, frag)
.commitNow() .commitNow()
} }
v.setOnLongClickListener {
val videoId = item.url!!.replace("/watch?v=", "")
VideoOptionsDialog(videoId, v.context)
.show(childFragmentManager, VideoOptionsDialog.TAG)
true
}
channelImage.setOnClickListener { channelImage.setOnClickListener {
val activity = v.context as MainActivity val activity = v.context as MainActivity
val bundle = bundleOf("channel_id" to item.uploaderUrl) val bundle = bundleOf("channel_id" to item.uploaderUrl)