create playlist rewrite

This commit is contained in:
Bnyro 2022-05-27 20:51:56 +02:00
parent d832d17c1a
commit 4947c97d70
2 changed files with 42 additions and 30 deletions

View File

@ -1,6 +1,8 @@
package com.github.libretube package com.github.libretube
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.LayoutInflater
import android.view.View import android.view.View
@ -12,9 +14,16 @@ 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.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import retrofit2.HttpException
import java.io.IOException
class CreatePlaylistDialog : DialogFragment() { class CreatePlaylistDialog : DialogFragment() {
val TAG = "CreatePlaylistDialog"
private var token: String = ""
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -36,13 +45,15 @@ class CreatePlaylistDialog : DialogFragment() {
dismiss() dismiss()
} }
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
token = sharedPref?.getString("token", "")!!
val playlistName = rootView.findViewById<TextInputEditText>(R.id.playlist_name) val playlistName = rootView.findViewById<TextInputEditText>(R.id.playlist_name)
val createPlaylistBtn = rootView.findViewById<Button>(R.id.create_new_playlist) val createPlaylistBtn = rootView.findViewById<Button>(R.id.create_new_playlist)
createPlaylistBtn.setOnClickListener { createPlaylistBtn.setOnClickListener {
var listName = playlistName.text.toString() var listName = playlistName.text.toString()
if (listName != "") { if (listName != "") {
setFragmentResult("key_parent", bundleOf("playlistName" to "$listName")) createPlaylist("$listName")
dismiss()
} else { } else {
Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show() Toast.makeText(context, R.string.emptyPlaylistName, Toast.LENGTH_LONG).show()
} }
@ -50,4 +61,31 @@ class CreatePlaylistDialog : DialogFragment() {
return rootView 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()
}
} }

View File

@ -61,9 +61,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 +127,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