mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Merge pull request #5370 from Bnyro/master
refactor: simplify the dropdown menu component
This commit is contained in:
commit
28ec625026
@ -91,16 +91,15 @@ class AddToPlaylistDialog : DialogFragment() {
|
||||
return@repeatOnLifecycle
|
||||
}.filter { !it.name.isNullOrEmpty() }
|
||||
|
||||
binding.playlistsSpinner.setItems(playlists.map { it.name!! })
|
||||
binding.playlistsSpinner.items = playlists.map { it.name!! }
|
||||
|
||||
if (response.isEmpty()) return@repeatOnLifecycle
|
||||
|
||||
// select the last used playlist
|
||||
viewModel.lastSelectedPlaylistId?.let { id ->
|
||||
val latestIndex = response
|
||||
binding.playlistsSpinner.selectedItemPosition = response
|
||||
.indexOfFirst { it.id == id }
|
||||
.takeIf { it >= 0 } ?: 0
|
||||
binding.playlistsSpinner.setSelection(latestIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import android.os.Bundle
|
||||
import android.text.InputFilter
|
||||
import android.text.format.Formatter
|
||||
import android.util.Log
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Toast
|
||||
import androidx.core.view.isGone
|
||||
import androidx.fragment.app.DialogFragment
|
||||
@ -25,11 +24,11 @@ import com.github.libretube.helpers.PreferenceHelper
|
||||
import com.github.libretube.parcelable.DownloadData
|
||||
import com.github.libretube.util.TextUtils
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.io.IOException
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import retrofit2.HttpException
|
||||
import java.io.IOException
|
||||
|
||||
class DownloadDialog : DialogFragment() {
|
||||
private lateinit var videoId: String
|
||||
@ -117,22 +116,14 @@ class DownloadDialog : DialogFragment() {
|
||||
|
||||
if (subtitles.isEmpty()) binding.subtitleSpinner.isGone = true
|
||||
|
||||
// initialize the video sources
|
||||
val videoArrayAdapter = ArrayAdapter(
|
||||
requireContext(),
|
||||
R.layout.dropdown_item,
|
||||
videoStreams.map {
|
||||
binding.videoSpinner.items = videoStreams.map {
|
||||
val fileSize = Formatter.formatShortFileSize(context, it.contentLength)
|
||||
"${it.quality} ${it.codec} ($fileSize)"
|
||||
}.toMutableList().also {
|
||||
it.add(0, getString(R.string.no_video))
|
||||
}
|
||||
)
|
||||
|
||||
val audioArrayAdapter = ArrayAdapter(
|
||||
requireContext(),
|
||||
R.layout.dropdown_item,
|
||||
audioStreams.map {
|
||||
binding.audioSpinner.items = audioStreams.map {
|
||||
val fileSize = it.contentLength
|
||||
.takeIf { l -> l > 0 }
|
||||
?.let { cl -> Formatter.formatShortFileSize(context, cl) }
|
||||
@ -142,19 +133,10 @@ class DownloadDialog : DialogFragment() {
|
||||
}.toMutableList().also {
|
||||
it.add(0, getString(R.string.no_audio))
|
||||
}
|
||||
)
|
||||
|
||||
val subtitleArrayAdapter = ArrayAdapter(
|
||||
requireContext(),
|
||||
R.layout.dropdown_item,
|
||||
subtitles.map { it.name.orEmpty() }.toMutableList().also {
|
||||
binding.subtitleSpinner.items = subtitles.map { it.name.orEmpty() }.toMutableList().also {
|
||||
it.add(0, getString(R.string.no_subtitle))
|
||||
}
|
||||
)
|
||||
|
||||
binding.videoSpinner.adapter = videoArrayAdapter
|
||||
binding.audioSpinner.adapter = audioArrayAdapter
|
||||
binding.subtitleSpinner.adapter = subtitleArrayAdapter
|
||||
|
||||
restorePreviousSelections(binding, videoStreams, audioStreams, subtitles)
|
||||
|
||||
@ -232,18 +214,18 @@ class DownloadDialog : DialogFragment() {
|
||||
getSel(VIDEO_DOWNLOAD_QUALITY),
|
||||
getSel(VIDEO_DOWNLOAD_FORMAT)
|
||||
)?.let {
|
||||
binding.videoSpinner.setSelection(it + 1)
|
||||
binding.audioSpinner.selectedItemPosition = it + 1
|
||||
}
|
||||
getStreamSelection(
|
||||
audioStreams,
|
||||
getSel(AUDIO_DOWNLOAD_QUALITY),
|
||||
getSel(AUDIO_DOWNLOAD_FORMAT)
|
||||
)?.let {
|
||||
binding.audioSpinner.setSelection(it + 1)
|
||||
binding.audioSpinner.selectedItemPosition = it + 1
|
||||
}
|
||||
|
||||
subtitles.indexOfFirst { it.code == getSel(SUBTITLE_LANGUAGE) }.takeIf { it != -1 }?.let {
|
||||
binding.subtitleSpinner.setSelection(it + 1)
|
||||
binding.audioSpinner.selectedItemPosition = it + 1
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,12 +41,7 @@ class SubmitSegmentDialog : DialogFragment() {
|
||||
|
||||
binding.startTime.setText((currentPosition.toFloat() / 1000).toString())
|
||||
|
||||
val categoryNames = resources.getStringArray(R.array.sponsorBlockSegmentNames)
|
||||
binding.segmentCategory.adapter = ArrayAdapter(
|
||||
requireContext(),
|
||||
R.layout.dropdown_item,
|
||||
categoryNames
|
||||
)
|
||||
binding.segmentCategory.items = resources.getStringArray(R.array.sponsorBlockSegmentNames).toList()
|
||||
|
||||
return MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle(getString(R.string.sb_create_segment))
|
||||
|
@ -55,12 +55,10 @@ class VoteForSegmentDialog : DialogFragment() {
|
||||
?.uuid ?: return@setOnClickListener
|
||||
|
||||
// see https://wiki.sponsor.ajay.app/w/API_Docs#POST_/api/voteOnSponsorTime
|
||||
val score = if (binding.upvote.isChecked) {
|
||||
1
|
||||
} else if (binding.downvote.isChecked) {
|
||||
0
|
||||
} else {
|
||||
20
|
||||
val score = when {
|
||||
binding.upvote.isChecked -> 1
|
||||
binding.downvote.isChecked -> 0
|
||||
else -> 20
|
||||
}
|
||||
|
||||
dialog?.hide()
|
||||
@ -99,15 +97,13 @@ class VoteForSegmentDialog : DialogFragment() {
|
||||
return@withContext
|
||||
}
|
||||
|
||||
val segmentTexts = segments.map {
|
||||
binding.segmentsDropdown.items = segments.map {
|
||||
"${it.category} (${
|
||||
DateUtils.formatElapsedTime(it.segmentStartAndEnd.first.toLong())
|
||||
} - ${
|
||||
DateUtils.formatElapsedTime(it.segmentStartAndEnd.second.toLong())
|
||||
})"
|
||||
}
|
||||
binding.segmentsDropdown.adapter =
|
||||
ArrayAdapter(requireContext(), R.layout.dropdown_item, segmentTexts)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,13 +22,20 @@ class DropdownMenu(
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
var adapter: ArrayAdapter<String>
|
||||
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
|
||||
set(value) {
|
||||
private set(value) {
|
||||
binding.autoCompleteTextView.setAdapter(value)
|
||||
if (!value.isEmpty) binding.autoCompleteTextView.setText(value.getItem(0), false)
|
||||
}
|
||||
|
||||
val selectedItemPosition: Int
|
||||
var items: List<String>
|
||||
get() = (0 until adapter.count).mapNotNull { adapter.getItem(it) }
|
||||
set(value) {
|
||||
adapter = ArrayAdapter(context, R.layout.dropdown_item, value)
|
||||
}
|
||||
|
||||
var selectedItemPosition: Int
|
||||
get() = adapter.getPosition(binding.autoCompleteTextView.text.toString())
|
||||
set(index) = binding.autoCompleteTextView.setText(adapter.getItem(index), false)
|
||||
|
||||
init {
|
||||
context.obtainStyledAttributes(attributeSet, R.styleable.DropdownMenu, 0, 0).use {
|
||||
@ -39,12 +46,4 @@ class DropdownMenu(
|
||||
|
||||
adapter = ArrayAdapter(context, R.layout.dropdown_item)
|
||||
}
|
||||
|
||||
fun setItems(items: List<String>) {
|
||||
adapter = ArrayAdapter(context, R.layout.dropdown_item, items)
|
||||
}
|
||||
|
||||
fun setSelection(index: Int) {
|
||||
binding.autoCompleteTextView.setText(adapter.getItem(index), false)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user