fix: crash in add to playlist dialog if playlists empty

This commit is contained in:
Bnyro 2023-12-21 14:16:27 +01:00
parent d24aa1cca8
commit 5297d870a9
2 changed files with 17 additions and 13 deletions

View File

@ -66,9 +66,11 @@ class AddToPlaylistDialog : DialogFragment() {
CreatePlaylistDialog().show(childFragmentManager, null)
}
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition
val playlist = playlists[index]
val playlistIndex = binding.playlistsSpinner.selectedItemPosition
val playlist = playlists.getOrElse(playlistIndex) { return@setOnClickListener }
viewModel.lastSelectedPlaylistId = playlist.id!!
dialog?.hide()
lifecycleScope.launch {
addToPlaylist(playlist.id, playlist.name!!)
@ -87,21 +89,17 @@ class AddToPlaylistDialog : DialogFragment() {
Log.e(TAG(), e.toString())
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
}
}.filter { !it.name.isNullOrEmpty() }
playlists = response.filter { !it.name.isNullOrEmpty() }
if (playlists.isEmpty()) return@repeatOnLifecycle
binding.playlistsSpinner.setItems(playlists.map { it.name!! })
binding.playlistsSpinner.adapter =
ArrayAdapter(
requireContext(),
R.layout.dropdown_item,
playlists.map { it.name!! }
)
if (response.isEmpty()) return@repeatOnLifecycle
// select the last used playlist
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)
}
}

View File

@ -24,7 +24,7 @@ class DropdownMenu(
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
set(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
@ -36,6 +36,12 @@ class DropdownMenu(
binding.textInputLayout.startIconDrawable =
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) {