Use MD3 dropdown menus in DownloadFragment

This commit is contained in:
Bnyro 2023-03-04 18:49:17 +01:00
parent 91f11b60b6
commit 1340de9d4a
6 changed files with 109 additions and 14 deletions

View File

@ -91,14 +91,16 @@ class DownloadDialog(
it.quality.getWhileDigit() it.quality.getWhileDigit()
} }
val subtitles = streams.subtitles.filter { !it.url.isNullOrEmpty() }.sortedBy { it.name } val subtitles = streams.subtitles
.filter { !it.url.isNullOrEmpty() && !it.name.isNullOrEmpty() }
.sortedBy { it.name }
if (subtitles.isEmpty()) binding.subtitleSpinner.visibility = View.GONE if (subtitles.isEmpty()) binding.subtitleSpinner.visibility = View.GONE
// initialize the video sources // initialize the video sources
val videoArrayAdapter = ArrayAdapter( val videoArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, R.layout.dropdown_item,
videoStreams.map { "${it.quality} ${it.format}" }.toMutableList().also { videoStreams.map { "${it.quality} ${it.format}" }.toMutableList().also {
it.add(0, getString(R.string.no_video)) it.add(0, getString(R.string.no_video))
} }
@ -106,7 +108,7 @@ class DownloadDialog(
val audioArrayAdapter = ArrayAdapter( val audioArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, R.layout.dropdown_item,
audioStreams.map { "${it.quality} ${it.format}" }.toMutableList().also { audioStreams.map { "${it.quality} ${it.format}" }.toMutableList().also {
it.add(0, getString(R.string.no_audio)) it.add(0, getString(R.string.no_audio))
} }
@ -114,15 +116,12 @@ class DownloadDialog(
val subtitleArrayAdapter = ArrayAdapter( val subtitleArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, R.layout.dropdown_item,
subtitles.map { it.name }.toMutableList().also { subtitles.map { it.name.orEmpty() }.toMutableList().also {
it.add(0, getString(R.string.no_subtitle)) it.add(0, getString(R.string.no_subtitle))
} }
) )
listOf(videoArrayAdapter, audioArrayAdapter, subtitleArrayAdapter).forEach {
it.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
}
binding.videoSpinner.adapter = videoArrayAdapter binding.videoSpinner.adapter = videoArrayAdapter
binding.audioSpinner.adapter = audioArrayAdapter binding.audioSpinner.adapter = audioArrayAdapter
binding.subtitleSpinner.adapter = subtitleArrayAdapter binding.subtitleSpinner.adapter = subtitleArrayAdapter

View File

@ -0,0 +1,51 @@
package com.github.libretube.ui.views
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ArrayAdapter
import android.widget.FrameLayout
import com.github.libretube.R
import com.github.libretube.databinding.DropdownMenuBinding
/**
* Exposed Dropdown Menu
*/
class DropdownMenu(
context: Context,
attributeSet: AttributeSet
) : FrameLayout(context, attributeSet) {
var binding: DropdownMenuBinding
@Suppress("UNCHECKED_CAST")
var adapter: ArrayAdapter<String>
get() = binding.autoCompleteTextView.adapter as ArrayAdapter<String>
set(value) {
binding.autoCompleteTextView.setAdapter(value)
binding.autoCompleteTextView.setText(value.getItem(0), false)
}
val selectedItemPosition: Int get() = adapter.getPosition(
binding.autoCompleteTextView.text.toString()
)
init {
val layoutInflater = LayoutInflater.from(context)
binding = DropdownMenuBinding.inflate(layoutInflater, this, true)
val ta = getContext().obtainStyledAttributes(attributeSet, R.styleable.DropdownMenu, 0, 0)
try {
binding.textInputLayout.hint = ta.getString(R.styleable.DropdownMenu_hint)
binding.textInputLayout.startIconDrawable = ta.getDrawable(
R.styleable.DropdownMenu_icon
)
} finally {
ta.recycle()
}
}
fun setSelection(index: Int) {
binding.autoCompleteTextView.setText(adapter.getItem(index))
}
}

View File

@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:orientation="vertical"
android:paddingHorizontal="20dp"> android:paddingHorizontal="20dp">
@ -25,23 +26,26 @@
android:inputType="text" /> android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<Spinner <com.github.libretube.ui.views.DropdownMenu
android:id="@+id/video_spinner" android:id="@+id/video_spinner"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginVertical="10dp" /> app:hint="@string/video"
app:icon="@drawable/ic_video" />
<Spinner <com.github.libretube.ui.views.DropdownMenu
android:id="@+id/audio_spinner" android:id="@+id/audio_spinner"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginVertical="10dp" /> app:hint="@string/audio"
app:icon="@drawable/ic_audio" />
<Spinner <com.github.libretube.ui.views.DropdownMenu
android:id="@+id/subtitle_spinner" android:id="@+id/subtitle_spinner"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginVertical="10dp" /> app:icon="@drawable/ic_caption"
app:hint="@string/captions" />
<Button <Button
android:id="@+id/download" android:id="@+id/download"

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textViewFeelings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="15dp"
android:paddingVertical="12dp"
android:textSize="18sp"
android:textStyle="bold"
tools:text="TextView" />

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingVertical="5dp">
<com.google.android.material.textfield.TextInputLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/textInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:startIconDrawable="@drawable/ic_audio"
tools:hint="Video">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="none"
tools:text="1080p MPEG_4" />
</com.google.android.material.textfield.TextInputLayout>
</FrameLayout>

View File

@ -17,4 +17,9 @@
<attr name="android:gravity" /> <attr name="android:gravity" />
</declare-styleable> </declare-styleable>
<declare-styleable name="DropdownMenu">
<attr name="hint" format="string" />
<attr name="icon" format="reference" />
</declare-styleable>
</resources> </resources>