mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
fix: crash in add to playlist dialog if playlists empty
This commit is contained in:
parent
d24aa1cca8
commit
5297d870a9
@ -66,9 +66,11 @@ class AddToPlaylistDialog : DialogFragment() {
|
|||||||
CreatePlaylistDialog().show(childFragmentManager, null)
|
CreatePlaylistDialog().show(childFragmentManager, null)
|
||||||
}
|
}
|
||||||
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
|
||||||
val index = binding.playlistsSpinner.selectedItemPosition
|
val playlistIndex = binding.playlistsSpinner.selectedItemPosition
|
||||||
val playlist = playlists[index]
|
|
||||||
|
val playlist = playlists.getOrElse(playlistIndex) { return@setOnClickListener }
|
||||||
viewModel.lastSelectedPlaylistId = playlist.id!!
|
viewModel.lastSelectedPlaylistId = playlist.id!!
|
||||||
|
|
||||||
dialog?.hide()
|
dialog?.hide()
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
addToPlaylist(playlist.id, playlist.name!!)
|
addToPlaylist(playlist.id, playlist.name!!)
|
||||||
@ -87,21 +89,17 @@ class AddToPlaylistDialog : DialogFragment() {
|
|||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
|
||||||
return@repeatOnLifecycle
|
return@repeatOnLifecycle
|
||||||
}
|
}.filter { !it.name.isNullOrEmpty() }
|
||||||
|
|
||||||
playlists = response.filter { !it.name.isNullOrEmpty() }
|
binding.playlistsSpinner.setItems(playlists.map { it.name!! })
|
||||||
if (playlists.isEmpty()) return@repeatOnLifecycle
|
|
||||||
|
|
||||||
binding.playlistsSpinner.adapter =
|
if (response.isEmpty()) return@repeatOnLifecycle
|
||||||
ArrayAdapter(
|
|
||||||
requireContext(),
|
|
||||||
R.layout.dropdown_item,
|
|
||||||
playlists.map { it.name!! }
|
|
||||||
)
|
|
||||||
|
|
||||||
// select the last used playlist
|
// select the last used playlist
|
||||||
viewModel.lastSelectedPlaylistId?.let { id ->
|
viewModel.lastSelectedPlaylistId?.let { id ->
|
||||||
val latestIndex = response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0
|
val latestIndex = response
|
||||||
|
.indexOfFirst { it.id == id }
|
||||||
|
.takeIf { it >= 0 } ?: 0
|
||||||
binding.playlistsSpinner.setSelection(latestIndex)
|
binding.playlistsSpinner.setSelection(latestIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ class DropdownMenu(
|
|||||||
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
|
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
|
||||||
set(value) {
|
set(value) {
|
||||||
binding.autoCompleteTextView.setAdapter(value)
|
binding.autoCompleteTextView.setAdapter(value)
|
||||||
binding.autoCompleteTextView.setText(value.getItem(0), false)
|
if (!value.isEmpty) binding.autoCompleteTextView.setText(value.getItem(0), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
val selectedItemPosition: Int
|
val selectedItemPosition: Int
|
||||||
@ -36,6 +36,12 @@ class DropdownMenu(
|
|||||||
binding.textInputLayout.startIconDrawable =
|
binding.textInputLayout.startIconDrawable =
|
||||||
it.getDrawable(R.styleable.DropdownMenu_icon)
|
it.getDrawable(R.styleable.DropdownMenu_icon)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
adapter = ArrayAdapter(context, R.layout.dropdown_item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setItems(items: List<String>) {
|
||||||
|
adapter = ArrayAdapter(context, R.layout.dropdown_item, items)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setSelection(index: Int) {
|
fun setSelection(index: Int) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user