From 35fc7ddef46b59a1906985af032642c352bcdc83 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 6 Aug 2022 18:57:07 +0200 Subject: [PATCH] remember selected playlist --- .../main/java/com/github/libretube/Globals.kt | 3 +++ ...aylistDialog.kt => AddToPlaylistDialog.kt} | 20 ++++++++++++------- .../libretube/dialogs/VideoOptionsDialog.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 4 ++-- 4 files changed, 19 insertions(+), 10 deletions(-) rename app/src/main/java/com/github/libretube/dialogs/{AddtoPlaylistDialog.kt => AddToPlaylistDialog.kt} (85%) diff --git a/app/src/main/java/com/github/libretube/Globals.kt b/app/src/main/java/com/github/libretube/Globals.kt index 3dac425a9..6d3b43a11 100644 --- a/app/src/main/java/com/github/libretube/Globals.kt +++ b/app/src/main/java/com/github/libretube/Globals.kt @@ -13,4 +13,7 @@ object Globals { // for downloads var IS_DOWNLOAD_RUNNING = false + + // for playlists + var SELECTED_PLAYLIST_ID: String? = null } diff --git a/app/src/main/java/com/github/libretube/dialogs/AddtoPlaylistDialog.kt b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt similarity index 85% rename from app/src/main/java/com/github/libretube/dialogs/AddtoPlaylistDialog.kt rename to app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt index 64b57868f..772a484c1 100644 --- a/app/src/main/java/com/github/libretube/dialogs/AddtoPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/AddToPlaylistDialog.kt @@ -8,6 +8,7 @@ import android.widget.Toast import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope +import com.github.libretube.Globals import com.github.libretube.R import com.github.libretube.databinding.DialogAddtoplaylistBinding import com.github.libretube.obj.PlaylistId @@ -18,7 +19,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder import retrofit2.HttpException import java.io.IOException -class AddtoPlaylistDialog : DialogFragment() { +class AddToPlaylistDialog : DialogFragment() { private val TAG = "AddToPlaylistDialog" private lateinit var binding: DialogAddtoplaylistBinding @@ -59,24 +60,29 @@ class AddtoPlaylistDialog : DialogFragment() { return@launchWhenCreated } if (response.isNotEmpty()) { - var names = emptyList().toMutableList() - for (playlist in response) { - names.add(playlist.name!!) - } + val names = response.map { it.name } val arrayAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, names) arrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item ) binding.playlistsSpinner.adapter = arrayAdapter + if (Globals.SELECTED_PLAYLIST_ID != null) { + var selectionIndex = 0 + response.forEachIndexed { index, playlist -> + if (playlist.id == Globals.SELECTED_PLAYLIST_ID) selectionIndex = index + } + binding.playlistsSpinner.setSelection(selectionIndex) + } runOnUiThread { binding.addToPlaylist.setOnClickListener { + val index = binding.playlistsSpinner.selectedItemPosition + Globals.SELECTED_PLAYLIST_ID = response[index].id!! addToPlaylist( - response[binding.playlistsSpinner.selectedItemPosition].id!! + response[index].id!! ) } } - } else { } } } diff --git a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt index 257bb8401..5091ac576 100644 --- a/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/VideoOptionsDialog.kt @@ -50,7 +50,7 @@ class VideoOptionsDialog(private val videoId: String, context: Context) : Dialog context?.getString(R.string.addToPlaylist) -> { val token = PreferenceHelper.getToken() if (token != "") { - val newFragment = AddtoPlaylistDialog() + val newFragment = AddToPlaylistDialog() val bundle = Bundle() bundle.putString("videoId", videoId) newFragment.arguments = bundle diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 1f37d6ea7..40c2e8cbc 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -46,7 +46,7 @@ import com.github.libretube.adapters.TrendingAdapter import com.github.libretube.databinding.DoubleTapOverlayBinding import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding import com.github.libretube.databinding.FragmentPlayerBinding -import com.github.libretube.dialogs.AddtoPlaylistDialog +import com.github.libretube.dialogs.AddToPlaylistDialog import com.github.libretube.dialogs.DownloadDialog import com.github.libretube.dialogs.ShareDialog import com.github.libretube.obj.ChapterSegment @@ -1097,7 +1097,7 @@ class PlayerFragment : Fragment() { if (token != "") { isSubscribed() binding.relPlayerSave.setOnClickListener { - val newFragment = AddtoPlaylistDialog() + val newFragment = AddToPlaylistDialog() val bundle = Bundle() bundle.putString("videoId", videoId) newFragment.arguments = bundle