mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-13 22:00:30 +05:30
create playlist rewrite
This commit is contained in:
parent
d832d17c1a
commit
4947c97d70
@ -1,6 +1,8 @@
|
||||
package com.github.libretube
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@ -12,9 +14,16 @@ 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.textfield.TextInputEditText
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
|
||||
class CreatePlaylistDialog : DialogFragment() {
|
||||
val TAG = "CreatePlaylistDialog"
|
||||
private var token: String = ""
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
@ -36,13 +45,15 @@ class CreatePlaylistDialog : DialogFragment() {
|
||||
dismiss()
|
||||
}
|
||||
|
||||
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
||||
token = sharedPref?.getString("token", "")!!
|
||||
|
||||
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"))
|
||||
dismiss()
|
||||
createPlaylist("$listName")
|
||||
} else {
|
||||
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
@ -50,4 +61,31 @@ class CreatePlaylistDialog : DialogFragment() {
|
||||
|
||||
return rootView
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
run()
|
||||
}
|
||||
}
|
||||
|
@ -61,9 +61,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 +127,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
|
||||
|
Loading…
Reference in New Issue
Block a user