optimize playlist creation

This commit is contained in:
Bnyro 2022-06-26 19:34:54 +02:00
parent 2e9238a512
commit 731379e006
3 changed files with 21 additions and 24 deletions

View File

@ -14,6 +14,7 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.setFragmentResult import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.fragments.Library
import com.github.libretube.obj.Playlists import com.github.libretube.obj.Playlists
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
@ -87,8 +88,13 @@ class CreatePlaylistDialog : DialogFragment() {
Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT) Toast.makeText(context, getString(R.string.unknown_error), Toast.LENGTH_SHORT)
.show() .show()
} }
// tell the Subscription Activity to fetch the playlists again // refresh the playlists in the library
setFragmentResult("fetchPlaylists", bundleOf("" to "")) try {
val parent = parentFragment as Library
parent.fetchPlaylists()
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
dismiss() dismiss()
} }
} }

View File

@ -53,19 +53,16 @@ class Library : Fragment() {
if (token != "") { if (token != "") {
view.findViewById<ImageView>(R.id.boogh2).visibility = View.GONE view.findViewById<ImageView>(R.id.boogh2).visibility = View.GONE
view.findViewById<TextView>(R.id.textLike2).visibility = View.GONE view.findViewById<TextView>(R.id.textLike2).visibility = View.GONE
fetchPlaylists(view) fetchPlaylists()
refreshLayout.isEnabled = true refreshLayout.isEnabled = true
refreshLayout.setOnRefreshListener { refreshLayout.setOnRefreshListener {
fetchPlaylists(view) fetchPlaylists()
} }
val createPlaylistButton = view.findViewById<FloatingActionButton>(R.id.create_playlist) val createPlaylistButton = view.findViewById<FloatingActionButton>(R.id.create_playlist)
createPlaylistButton.setOnClickListener { createPlaylistButton.setOnClickListener {
val newFragment = CreatePlaylistDialog() val newFragment = CreatePlaylistDialog()
newFragment.show(childFragmentManager, "Create Playlist") newFragment.show(childFragmentManager, "Create Playlist")
} }
childFragmentManager.setFragmentResultListener("fetchPlaylists", this) { _, _ ->
fetchPlaylists(view)
}
} else { } else {
refreshLayout.isEnabled = false refreshLayout.isEnabled = false
view.findViewById<FloatingActionButton>(R.id.create_playlist).visibility = View.GONE view.findViewById<FloatingActionButton>(R.id.create_playlist).visibility = View.GONE
@ -81,7 +78,7 @@ class Library : Fragment() {
super.onResume() super.onResume()
} }
private fun fetchPlaylists(view: View) { fun fetchPlaylists() {
fun run() { fun run() {
refreshLayout.isRefreshing = true refreshLayout.isRefreshing = true
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
@ -101,12 +98,8 @@ class Library : Fragment() {
} }
if (response.isNotEmpty()) { if (response.isNotEmpty()) {
runOnUiThread { runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh2)) { view?.findViewById<ImageView>(R.id.boogh2)?.visibility = View.GONE
visibility = View.GONE view?.findViewById<TextView>(R.id.textLike2)?.visibility = View.GONE
}
with(view.findViewById<TextView>(R.id.textLike2)) {
visibility = View.GONE
}
} }
val playlistsAdapter = PlaylistsAdapter( val playlistsAdapter = PlaylistsAdapter(
response.toMutableList(), response.toMutableList(),
@ -115,13 +108,13 @@ class Library : Fragment() {
playlistRecyclerView.adapter = playlistsAdapter playlistRecyclerView.adapter = playlistsAdapter
} else { } else {
runOnUiThread { runOnUiThread {
with(view.findViewById<ImageView>(R.id.boogh2)) { view?.findViewById<ImageView>(R.id.boogh2).apply {
visibility = View.VISIBLE this?.visibility = View.VISIBLE
setImageResource(R.drawable.ic_list) this?.setImageResource(R.drawable.ic_list)
} }
with(view.findViewById<TextView>(R.id.textLike2)) { view?.findViewById<TextView>(R.id.textLike2).apply {
visibility = View.VISIBLE this?.visibility = View.VISIBLE
text = getString(R.string.emptyList) this?.text = getString(R.string.emptyList)
} }
} }
} }

View File

@ -55,14 +55,12 @@ object PreferenceHelper {
fun clearPreferences(context: Context) { fun clearPreferences(context: Context) {
val editor = getDefaultSharedPreferencesEditor(context) val editor = getDefaultSharedPreferencesEditor(context)
editor.clear() editor.clear().apply()
editor.apply()
} }
fun removePreference(context: Context, value: String?) { fun removePreference(context: Context, value: String?) {
val editor = getDefaultSharedPreferencesEditor(context) val editor = getDefaultSharedPreferencesEditor(context)
editor.remove(value) editor.remove(value).apply()
editor.apply()
} }
fun getToken(context: Context): String { fun getToken(context: Context): String {