@@ -13,6 +13,7 @@ import com.github.libretube.enums.PlaylistType
1313import com.github.libretube.extensions.getWhileDigit
1414import com.github.libretube.extensions.serializable
1515import com.github.libretube.helpers.LocaleHelper
16+ import com.github.libretube.helpers.PreferenceHelper
1617import com.github.libretube.services.PlaylistDownloadEnqueueService
1718import com.github.libretube.util.TextUtils
1819import com.google.android.material.dialog.MaterialAlertDialogBuilder
@@ -26,7 +27,8 @@ class DownloadPlaylistDialog : DialogFragment() {
2627 super .onCreate(savedInstanceState)
2728
2829 playlistId = requireArguments().getString(IntentData .playlistId)!!
29- playlistName = requireArguments().getString(IntentData .playlistName) ? : TextUtils .getFileSafeTimeStampNow()
30+ playlistName = requireArguments().getString(IntentData .playlistName)
31+ ? : TextUtils .getFileSafeTimeStampNow()
3032 playlistType = requireArguments().serializable(IntentData .playlistType)!!
3133 }
3234
@@ -38,6 +40,7 @@ class DownloadPlaylistDialog : DialogFragment() {
3840 it.subList(1 , it.size)
3941 }
4042 val possibleAudioQualities = resources.getStringArray(R .array.audioQualityBitrates)
43+ .toList()
4144 val availableLanguages = LocaleHelper .getAvailableLocales()
4245
4346 binding.videoSpinner.items = listOf (getString(R .string.no_video)) + possibleVideoQualities
@@ -47,6 +50,8 @@ class DownloadPlaylistDialog : DialogFragment() {
4750 binding.audioLanguageSpinner.items =
4851 listOf (getString(R .string.default_language)) + availableLanguages.map { it.name }
4952
53+ restoreSelections(binding)
54+
5055 return MaterialAlertDialogBuilder (requireContext())
5156 .setTitle(getString(R .string.download_playlist) + " : " + playlistName)
5257 .setView(binding.root)
@@ -78,6 +83,8 @@ class DownloadPlaylistDialog : DialogFragment() {
7883 null
7984 }
8085
86+ saveSelections(binding)
87+
8188 if (maxVideoQuality == null && maxAudioQuality == null ) {
8289 Toast .makeText(context, R .string.nothing_selected, Toast .LENGTH_SHORT )
8390 .show()
@@ -100,4 +107,44 @@ class DownloadPlaylistDialog : DialogFragment() {
100107 .setNegativeButton(R .string.cancel, null )
101108 .show()
102109 }
110+
111+ private fun saveSelections (binding : DialogDownloadPlaylistBinding ) {
112+ binding.audioSpinner.selectedItemPosition.let { index ->
113+ val item = binding.audioSpinner.items[index].takeIf { index != 0 }
114+ PreferenceHelper .putString(PLAYLIST_DOWNLOAD_AUDIO_QUALITY , item.orEmpty())
115+ }
116+ binding.videoSpinner.selectedItemPosition.let { index ->
117+ val item = binding.videoSpinner.items[index].takeIf { index != 0 }
118+ PreferenceHelper .putString(PLAYLIST_DOWNLOAD_VIDEO_QUALITY , item.orEmpty())
119+ }
120+ binding.audioLanguageSpinner.selectedItemPosition.let { index ->
121+ val item = binding.audioLanguageSpinner.items[index].takeIf { index != 0 }
122+ PreferenceHelper .putString(PLAYLIST_DOWNLOAD_AUDIO_LANGUAGE , item.orEmpty())
123+ }
124+ binding.subtitleSpinner.selectedItemPosition.let { index ->
125+ val item = binding.subtitleSpinner.items[index].takeIf { index != 0 }
126+ PreferenceHelper .putString(PLAYLIST_DOWNLOAD_CAPTION_LANGUAGE , item.orEmpty())
127+ }
128+ }
129+
130+ private fun restoreSelections (binding : DialogDownloadPlaylistBinding ) {
131+ val videoQuality = PreferenceHelper .getString(PLAYLIST_DOWNLOAD_VIDEO_QUALITY , " " )
132+ binding.videoSpinner.selectedItemPosition = binding.videoSpinner.items.indexOf(videoQuality).takeIf { it != - 1 } ? : 0
133+
134+ val audioQuality = PreferenceHelper .getString(PLAYLIST_DOWNLOAD_AUDIO_QUALITY , " " )
135+ binding.audioSpinner.selectedItemPosition = binding.audioSpinner.items.indexOf(audioQuality).takeIf { it != - 1 } ? : 0
136+
137+ val audioLanguage = PreferenceHelper .getString(PLAYLIST_DOWNLOAD_AUDIO_LANGUAGE , " " )
138+ binding.audioLanguageSpinner.selectedItemPosition = binding.audioLanguageSpinner.items.indexOf(audioLanguage).takeIf { it != - 1 } ? : 0
139+
140+ val captionLanguage = PreferenceHelper .getString(PLAYLIST_DOWNLOAD_CAPTION_LANGUAGE , " " )
141+ binding.subtitleSpinner.selectedItemPosition = binding.subtitleSpinner.items.indexOf(captionLanguage).takeIf { it != - 1 } ? : 0
142+ }
143+
144+ companion object {
145+ private const val PLAYLIST_DOWNLOAD_VIDEO_QUALITY = " playlist_download_video_quality"
146+ private const val PLAYLIST_DOWNLOAD_AUDIO_QUALITY = " playlist_download_audio_quality"
147+ private const val PLAYLIST_DOWNLOAD_AUDIO_LANGUAGE = " playlist_download_audio_language"
148+ private const val PLAYLIST_DOWNLOAD_CAPTION_LANGUAGE = " playlist_download_caption_language"
149+ }
103150}
0 commit comments