Merge pull request #5369 from Bnyro/master

fix: crash in add to playlist dialog if playlists empty
This commit is contained in:
Bnyro 2023-12-21 14:16:45 +01:00 committed by GitHub
commit 4dce4166bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 13 deletions

View File

@ -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)
} }
} }

View File

@ -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) {