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
|
return@repeatOnLifecycle
|
||||||
}.filter { !it.name.isNullOrEmpty() }
|
}.filter { !it.name.isNullOrEmpty() }
|
||||||
|
|
||||||
binding.playlistsSpinner.setItems(playlists.map { it.name!! })
|
binding.playlistsSpinner.items = playlists.map { it.name!! }
|
||||||
|
|
||||||
if (response.isEmpty()) return@repeatOnLifecycle
|
if (response.isEmpty()) return@repeatOnLifecycle
|
||||||
|
|
||||||
// select the last used playlist
|
// select the last used playlist
|
||||||
viewModel.lastSelectedPlaylistId?.let { id ->
|
viewModel.lastSelectedPlaylistId?.let { id ->
|
||||||
val latestIndex = response
|
binding.playlistsSpinner.selectedItemPosition = response
|
||||||
.indexOfFirst { it.id == id }
|
.indexOfFirst { it.id == id }
|
||||||
.takeIf { it >= 0 } ?: 0
|
.takeIf { it >= 0 } ?: 0
|
||||||
binding.playlistsSpinner.setSelection(latestIndex)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import android.os.Bundle
|
|||||||
import android.text.InputFilter
|
import android.text.InputFilter
|
||||||
import android.text.format.Formatter
|
import android.text.format.Formatter
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.ArrayAdapter
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.fragment.app.DialogFragment
|
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.parcelable.DownloadData
|
||||||
import com.github.libretube.util.TextUtils
|
import com.github.libretube.util.TextUtils
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import java.io.IOException
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
class DownloadDialog : DialogFragment() {
|
class DownloadDialog : DialogFragment() {
|
||||||
private lateinit var videoId: String
|
private lateinit var videoId: String
|
||||||
@ -117,44 +116,27 @@ class DownloadDialog : DialogFragment() {
|
|||||||
|
|
||||||
if (subtitles.isEmpty()) binding.subtitleSpinner.isGone = true
|
if (subtitles.isEmpty()) binding.subtitleSpinner.isGone = true
|
||||||
|
|
||||||
// initialize the video sources
|
binding.videoSpinner.items = videoStreams.map {
|
||||||
val videoArrayAdapter = ArrayAdapter(
|
val fileSize = Formatter.formatShortFileSize(context, it.contentLength)
|
||||||
requireContext(),
|
"${it.quality} ${it.codec} ($fileSize)"
|
||||||
R.layout.dropdown_item,
|
}.toMutableList().also {
|
||||||
videoStreams.map {
|
it.add(0, getString(R.string.no_video))
|
||||||
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(
|
binding.audioSpinner.items = audioStreams.map {
|
||||||
requireContext(),
|
val fileSize = it.contentLength
|
||||||
R.layout.dropdown_item,
|
.takeIf { l -> l > 0 }
|
||||||
audioStreams.map {
|
?.let { cl -> Formatter.formatShortFileSize(context, cl) }
|
||||||
val fileSize = it.contentLength
|
val infoStr = listOfNotNull(it.audioTrackLocale, fileSize)
|
||||||
.takeIf { l -> l > 0 }
|
.joinToString(", ")
|
||||||
?.let { cl -> Formatter.formatShortFileSize(context, cl) }
|
"${it.quality} ${it.format} ($infoStr)"
|
||||||
val infoStr = listOfNotNull(it.audioTrackLocale, fileSize)
|
}.toMutableList().also {
|
||||||
.joinToString(", ")
|
it.add(0, getString(R.string.no_audio))
|
||||||
"${it.quality} ${it.format} ($infoStr)"
|
}
|
||||||
}.toMutableList().also {
|
|
||||||
it.add(0, getString(R.string.no_audio))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
val subtitleArrayAdapter = ArrayAdapter(
|
binding.subtitleSpinner.items = subtitles.map { it.name.orEmpty() }.toMutableList().also {
|
||||||
requireContext(),
|
it.add(0, getString(R.string.no_subtitle))
|
||||||
R.layout.dropdown_item,
|
}
|
||||||
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)
|
restorePreviousSelections(binding, videoStreams, audioStreams, subtitles)
|
||||||
|
|
||||||
@ -232,18 +214,18 @@ class DownloadDialog : DialogFragment() {
|
|||||||
getSel(VIDEO_DOWNLOAD_QUALITY),
|
getSel(VIDEO_DOWNLOAD_QUALITY),
|
||||||
getSel(VIDEO_DOWNLOAD_FORMAT)
|
getSel(VIDEO_DOWNLOAD_FORMAT)
|
||||||
)?.let {
|
)?.let {
|
||||||
binding.videoSpinner.setSelection(it + 1)
|
binding.audioSpinner.selectedItemPosition = it + 1
|
||||||
}
|
}
|
||||||
getStreamSelection(
|
getStreamSelection(
|
||||||
audioStreams,
|
audioStreams,
|
||||||
getSel(AUDIO_DOWNLOAD_QUALITY),
|
getSel(AUDIO_DOWNLOAD_QUALITY),
|
||||||
getSel(AUDIO_DOWNLOAD_FORMAT)
|
getSel(AUDIO_DOWNLOAD_FORMAT)
|
||||||
)?.let {
|
)?.let {
|
||||||
binding.audioSpinner.setSelection(it + 1)
|
binding.audioSpinner.selectedItemPosition = it + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
subtitles.indexOfFirst { it.code == getSel(SUBTITLE_LANGUAGE) }.takeIf { it != -1 }?.let {
|
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())
|
binding.startTime.setText((currentPosition.toFloat() / 1000).toString())
|
||||||
|
|
||||||
val categoryNames = resources.getStringArray(R.array.sponsorBlockSegmentNames)
|
binding.segmentCategory.items = resources.getStringArray(R.array.sponsorBlockSegmentNames).toList()
|
||||||
binding.segmentCategory.adapter = ArrayAdapter(
|
|
||||||
requireContext(),
|
|
||||||
R.layout.dropdown_item,
|
|
||||||
categoryNames
|
|
||||||
)
|
|
||||||
|
|
||||||
return MaterialAlertDialogBuilder(requireContext())
|
return MaterialAlertDialogBuilder(requireContext())
|
||||||
.setTitle(getString(R.string.sb_create_segment))
|
.setTitle(getString(R.string.sb_create_segment))
|
||||||
|
@ -55,12 +55,10 @@ class VoteForSegmentDialog : DialogFragment() {
|
|||||||
?.uuid ?: return@setOnClickListener
|
?.uuid ?: return@setOnClickListener
|
||||||
|
|
||||||
// see https://wiki.sponsor.ajay.app/w/API_Docs#POST_/api/voteOnSponsorTime
|
// see https://wiki.sponsor.ajay.app/w/API_Docs#POST_/api/voteOnSponsorTime
|
||||||
val score = if (binding.upvote.isChecked) {
|
val score = when {
|
||||||
1
|
binding.upvote.isChecked -> 1
|
||||||
} else if (binding.downvote.isChecked) {
|
binding.downvote.isChecked -> 0
|
||||||
0
|
else -> 20
|
||||||
} else {
|
|
||||||
20
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dialog?.hide()
|
dialog?.hide()
|
||||||
@ -99,15 +97,13 @@ class VoteForSegmentDialog : DialogFragment() {
|
|||||||
return@withContext
|
return@withContext
|
||||||
}
|
}
|
||||||
|
|
||||||
val segmentTexts = segments.map {
|
binding.segmentsDropdown.items = segments.map {
|
||||||
"${it.category} (${
|
"${it.category} (${
|
||||||
DateUtils.formatElapsedTime(it.segmentStartAndEnd.first.toLong())
|
DateUtils.formatElapsedTime(it.segmentStartAndEnd.first.toLong())
|
||||||
} - ${
|
} - ${
|
||||||
DateUtils.formatElapsedTime(it.segmentStartAndEnd.second.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")
|
@Suppress("UNCHECKED_CAST")
|
||||||
var adapter: ArrayAdapter<String>
|
var adapter: ArrayAdapter<String>
|
||||||
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
|
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
|
||||||
set(value) {
|
private set(value) {
|
||||||
binding.autoCompleteTextView.setAdapter(value)
|
binding.autoCompleteTextView.setAdapter(value)
|
||||||
if (!value.isEmpty) binding.autoCompleteTextView.setText(value.getItem(0), false)
|
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())
|
get() = adapter.getPosition(binding.autoCompleteTextView.text.toString())
|
||||||
|
set(index) = binding.autoCompleteTextView.setText(adapter.getItem(index), false)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
context.obtainStyledAttributes(attributeSet, R.styleable.DropdownMenu, 0, 0).use {
|
context.obtainStyledAttributes(attributeSet, R.styleable.DropdownMenu, 0, 0).use {
|
||||||
@ -39,12 +46,4 @@ class DropdownMenu(
|
|||||||
|
|
||||||
adapter = ArrayAdapter(context, R.layout.dropdown_item)
|
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