mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 05:40:31 +05:30
Merge pull request #321 from Bnyro/master
Add to Playlist in Video Options Dialog and CreatePlaylistDialog Rewrite
This commit is contained in:
commit
df9221d83d
@ -1,10 +1,11 @@
|
||||
package com.github.libretube
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
@ -12,42 +13,81 @@ import androidx.core.os.bundleOf
|
||||
import androidx.core.text.HtmlCompat
|
||||
import androidx.fragment.app.DialogFragment
|
||||
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 java.io.IOException
|
||||
import retrofit2.HttpException
|
||||
|
||||
class CreatePlaylistDialog : DialogFragment() {
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
var rootView: View = inflater.inflate(R.layout.dialog_create_playlist, container, false)
|
||||
val TAG = "CreatePlaylistDialog"
|
||||
private var token: String = ""
|
||||
|
||||
val typedValue = TypedValue()
|
||||
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
|
||||
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
|
||||
)
|
||||
rootView.findViewById<TextView>(R.id.title).text = appName
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
return activity?.let {
|
||||
val builder = MaterialAlertDialogBuilder(it)
|
||||
val inflater = requireActivity().layoutInflater
|
||||
val view: View = inflater.inflate(R.layout.dialog_create_playlist, null)
|
||||
|
||||
val cancelBtn = rootView.findViewById<Button>(R.id.cancel_button)
|
||||
cancelBtn.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
val typedValue = TypedValue()
|
||||
this.requireActivity().theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true)
|
||||
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 createPlaylistBtn = rootView.findViewById<Button>(R.id.create_new_playlist)
|
||||
createPlaylistBtn.setOnClickListener {
|
||||
var listName = playlistName.text.toString()
|
||||
if (listName != "") {
|
||||
setFragmentResult("key_parent", bundleOf("playlistName" to "$listName"))
|
||||
val cancelBtn = view.findViewById<Button>(R.id.cancel_button)
|
||||
cancelBtn.setOnClickListener {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
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()
|
||||
} else {
|
||||
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
return rootView
|
||||
run()
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import com.github.libretube.adapters.PlaylistsAdapter
|
||||
import com.github.libretube.obj.Playlists
|
||||
import java.io.IOException
|
||||
import retrofit2.HttpException
|
||||
|
||||
@ -61,9 +60,8 @@ class Library : Fragment() {
|
||||
val newFragment = CreatePlaylistDialog()
|
||||
newFragment.show(childFragmentManager, "Create Playlist")
|
||||
}
|
||||
childFragmentManager.setFragmentResultListener("key_parent", this) { _, result ->
|
||||
val playlistName = result.getString("playlistName")
|
||||
createPlaylist("$playlistName", view)
|
||||
childFragmentManager.setFragmentResultListener("fetchPlaylists", this) { _, _ ->
|
||||
fetchPlaylists(view)
|
||||
}
|
||||
} else {
|
||||
refreshLayout.isEnabled = false
|
||||
@ -128,31 +126,6 @@ class Library : Fragment() {
|
||||
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) {
|
||||
this ?: return
|
||||
if (!isAdded) return // Fragment not attached to an Activity
|
||||
|
@ -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.
|
||||
*/
|
||||
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()
|
||||
.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 -> {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
@ -23,10 +23,16 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:hintEnabled="false"
|
||||
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginLeft="7dp"
|
||||
android:layout_marginRight="7dp"
|
||||
android:layout_marginBottom="4dp"
|
||||
|
||||
app:boxCornerRadiusBottomStart="15dp"
|
||||
app:boxCornerRadiusBottomEnd="15dp"
|
||||
app:boxCornerRadiusTopEnd="15dp"
|
||||
app:boxCornerRadiusTopStart="15dp"
|
||||
>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
|
Loading…
Reference in New Issue
Block a user