Merge pull request #321 from Bnyro/master

Add to Playlist in Video Options Dialog and CreatePlaylistDialog Rewrite
This commit is contained in:
Bnyro 2022-05-27 19:08:19 +02:00 committed by GitHub
commit df9221d83d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 60 deletions

View File

@ -1,10 +1,11 @@
package com.github.libretube package com.github.libretube
import android.app.Dialog
import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.Log
import android.util.TypedValue import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
@ -12,42 +13,81 @@ import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.fragment.app.setFragmentResult import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope
import com.github.libretube.obj.Playlists
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import java.io.IOException
import retrofit2.HttpException
class CreatePlaylistDialog : DialogFragment() { class CreatePlaylistDialog : DialogFragment() {
override fun onCreateView( val TAG = "CreatePlaylistDialog"
inflater: LayoutInflater, private var token: String = ""
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var rootView: View = inflater.inflate(R.layout.dialog_create_playlist, container, false)
val typedValue = TypedValue() override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true) return activity?.let {
val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data)) val builder = MaterialAlertDialogBuilder(it)
val appName = HtmlCompat.fromHtml( val inflater = requireActivity().layoutInflater
"Libre<span style='color:$hexColor';>Tube</span>", val view: View = inflater.inflate(R.layout.dialog_create_playlist, null)
HtmlCompat.FROM_HTML_MODE_COMPACT
)
rootView.findViewById<TextView>(R.id.title).text = appName
val cancelBtn = rootView.findViewById<Button>(R.id.cancel_button) val typedValue = TypedValue()
cancelBtn.setOnClickListener { this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
dismiss() val hexColor = String.format("#%06X", (0xFFFFFF and typedValue.data))
} val appName = HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
view.findViewById<TextView>(R.id.title).text = appName
val playlistName = rootView.findViewById<TextInputEditText>(R.id.playlist_name) val cancelBtn = view.findViewById<Button>(R.id.cancel_button)
val createPlaylistBtn = rootView.findViewById<Button>(R.id.create_new_playlist) cancelBtn.setOnClickListener {
createPlaylistBtn.setOnClickListener { dismiss()
var listName = playlistName.text.toString() }
if (listName != "") {
setFragmentResult("key_parent", bundleOf("playlistName" to "$listName")) val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
token = sharedPref?.getString("token", "")!!
val playlistName = view.findViewById<TextInputEditText>(R.id.playlist_name)
val createPlaylistBtn = view.findViewById<Button>(R.id.create_new_playlist)
createPlaylistBtn.setOnClickListener {
var listName = playlistName.text.toString()
if (listName != "") {
createPlaylist("$listName")
} else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
}
}
builder.setView(view)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
private fun createPlaylist(name: String) {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.createPlaylist(token, Playlists(name = name))
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response $e")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
if (response != null) {
Toast.makeText(context, R.string.playlistCreated, Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT)
.show()
}
}.invokeOnCompletion {
setFragmentResult("fetchPlaylists", bundleOf("" to ""))
dismiss() dismiss()
} else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
} }
} }
run()
return rootView
} }
} }

View File

@ -16,7 +16,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.github.libretube.adapters.PlaylistsAdapter import com.github.libretube.adapters.PlaylistsAdapter
import com.github.libretube.obj.Playlists
import java.io.IOException import java.io.IOException
import retrofit2.HttpException import retrofit2.HttpException
@ -61,9 +60,8 @@ class Library : Fragment() {
val newFragment = CreatePlaylistDialog() val newFragment = CreatePlaylistDialog()
newFragment.show(childFragmentManager, "Create Playlist") newFragment.show(childFragmentManager, "Create Playlist")
} }
childFragmentManager.setFragmentResultListener("key_parent", this) { _, result -> childFragmentManager.setFragmentResultListener("fetchPlaylists", this) { _, _ ->
val playlistName = result.getString("playlistName") fetchPlaylists(view)
createPlaylist("$playlistName", view)
} }
} else { } else {
refreshLayout.isEnabled = false refreshLayout.isEnabled = false
@ -128,31 +126,6 @@ class Library : Fragment() {
run() run()
} }
private fun createPlaylist(name: String, view: View) {
fun run() {
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.createPlaylist(token, Playlists(name = name))
} catch (e: IOException) {
println(e)
Log.e(TAG, "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
} catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response $e")
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated
}
if (response != null) {
Toast.makeText(context, R.string.playlistCreated, Toast.LENGTH_SHORT).show()
fetchPlaylists(view)
} else {
}
}
}
run()
}
private fun Fragment?.runOnUiThread(action: () -> Unit) { private fun Fragment?.runOnUiThread(action: () -> Unit) {
this ?: return this ?: return
if (!isAdded) return // Fragment not attached to an Activity if (!isAdded) return // Fragment not attached to an Activity

View File

@ -17,7 +17,8 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog
* List that stores the different menu options. In the future could be add more options here. * List that stores the different menu options. In the future could be add more options here.
*/ */
private val list = listOf( private val list = listOf(
context.getString(R.string.playOnBackground) context.getString(R.string.playOnBackground),
context.getString(R.string.addToPlaylist)
) )
/** /**
@ -44,6 +45,14 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog
.getInstance() .getInstance()
.playOnBackgroundMode(requireContext(), videoId, 0) .playOnBackgroundMode(requireContext(), videoId, 0)
} }
// Add Video to Playlist Dialog
1 -> {
val newFragment = AddtoPlaylistDialog()
var bundle = Bundle()
bundle.putString("videoId", videoId)
newFragment.arguments = bundle
newFragment.show(parentFragmentManager, "AddToPlaylist")
}
else -> { else -> {
dialog.dismiss() dialog.dismiss()
} }

View File

@ -23,10 +23,16 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:hintEnabled="false" app:hintEnabled="false"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginLeft="7dp" android:layout_marginLeft="7dp"
android:layout_marginRight="7dp" android:layout_marginRight="7dp"
android:layout_marginBottom="4dp" android:layout_marginBottom="4dp"
app:boxCornerRadiusBottomStart="15dp"
app:boxCornerRadiusBottomEnd="15dp"
app:boxCornerRadiusTopEnd="15dp"
app:boxCornerRadiusTopStart="15dp"
> >
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputEditText