Select source domains using AutoCompleteTextView

This commit is contained in:
Koitharu
2022-04-23 14:45:45 +03:00
parent c220bd5517
commit 7bffb5f22d
6 changed files with 136 additions and 20 deletions

View File

@@ -65,7 +65,7 @@ android {
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation('com.github.nv95:kotatsu-parsers:c1aa8a45dc') {
implementation('com.github.nv95:kotatsu-parsers:1d171d41fe') {
exclude group: 'org.json', module: 'json'
}

View File

@@ -1,15 +1,15 @@
package org.koitharu.kotatsu.settings
import android.view.inputmethod.EditorInfo
import androidx.preference.*
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
import org.koitharu.kotatsu.parsers.config.ConfigKey
import org.koitharu.kotatsu.settings.utils.AutoCompleteTextViewPreference
import org.koitharu.kotatsu.settings.utils.EditTextBindListener
import org.koitharu.kotatsu.settings.utils.EditTextDefaultSummaryProvider
import org.koitharu.kotatsu.utils.ext.setDefaultValueCompat
private const val KEY_DOMAIN = "domain"
fun PreferenceFragmentCompat.addPreferencesFromRepository(repository: RemoteMangaRepository) {
val configKeys = repository.getConfigKeys()
@@ -19,23 +19,17 @@ fun PreferenceFragmentCompat.addPreferencesFromRepository(repository: RemoteMang
is ConfigKey.Domain -> {
val presetValues = key.presetValues
if (presetValues.isNullOrEmpty()) {
EditTextPreference(requireContext()).apply {
summaryProvider = EditTextDefaultSummaryProvider(key.defaultValue)
setOnBindEditTextListener(
EditTextBindListener(
inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI,
hint = key.defaultValue,
)
)
}
EditTextPreference(requireContext())
} else {
DropDownPreference(requireContext()).apply {
entries = presetValues
entryValues = entries
summaryProvider = ListPreference.SimpleSummaryProvider.getInstance()
setDefaultValueCompat(key.defaultValue)
}
AutoCompleteTextViewPreference(requireContext()).apply { entries = presetValues }
}.apply {
summaryProvider = EditTextDefaultSummaryProvider(key.defaultValue)
setOnBindEditTextListener(
EditTextBindListener(
inputType = EditorInfo.TYPE_CLASS_TEXT or EditorInfo.TYPE_TEXT_VARIATION_URI,
hint = key.defaultValue,
)
)
setTitle(R.string.domain)
setDialogTitle(R.string.domain)
}

View File

@@ -0,0 +1,58 @@
package org.koitharu.kotatsu.settings.utils
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.EditText
import androidx.annotation.ArrayRes
import androidx.annotation.AttrRes
import androidx.annotation.StyleRes
import androidx.preference.EditTextPreference
import org.koitharu.kotatsu.R
class AutoCompleteTextViewPreference @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
@AttrRes defStyleAttr: Int = R.attr.autoCompleteTextViewPreferenceStyle,
@StyleRes defStyleRes: Int = R.style.Preference_AutoCompleteTextView,
) : EditTextPreference(context, attrs, defStyleAttr, defStyleRes) {
private val autoCompleteBindListener = AutoCompleteBindListener()
var entries: Array<String> = emptyArray()
init {
super.setOnBindEditTextListener(autoCompleteBindListener)
}
fun setEntries(@ArrayRes arrayResId: Int) {
this.entries = context.resources.getStringArray(arrayResId)
}
fun setEntries(entries: Collection<String>) {
this.entries = entries.toTypedArray()
}
override fun setOnBindEditTextListener(onBindEditTextListener: OnBindEditTextListener?) {
autoCompleteBindListener.delegate = onBindEditTextListener
}
private inner class AutoCompleteBindListener : OnBindEditTextListener {
var delegate: OnBindEditTextListener? = null
override fun onBindEditText(editText: EditText) {
delegate?.onBindEditText(editText)
if (editText !is AutoCompleteTextView || entries.isEmpty()) {
return
}
editText.threshold = 0
editText.setAdapter(ArrayAdapter(editText.context, android.R.layout.simple_spinner_dropdown_item, entries))
(editText.parent as? ViewGroup)?.findViewById<View>(R.id.dropdown)?.setOnClickListener {
editText.showDropDown()
}
}
}
}

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:overScrollMode="ifContentScrolls">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@android:id/message"
style="?android:attr/textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="48dp"
android:labelFor="@android:id/edit"
android:textColor="?android:attr/textColorSecondary"
tools:ignore="LabelFor" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
<AutoCompleteTextView
android:id="@android:id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:minHeight="48dp" />
<ImageView
android:id="@+id/dropdown"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center_vertical|end"
android:background="?selectableItemBackgroundBorderless"
android:paddingBottom="2dp"
android:scaleType="center"
android:src="@drawable/ic_expand_more"
tools:ignore="ContentDescription" />
</FrameLayout>
</LinearLayout>
</ScrollView>

View File

@@ -3,6 +3,7 @@
<attr name="sliderPreferenceStyle" />
<attr name="multiAutoCompleteTextViewPreferenceStyle" />
<attr name="autoCompleteTextViewPreferenceStyle" />
<attr name="listItemTextViewStyle" />
<declare-styleable name="Theme">

View File

@@ -156,4 +156,8 @@
<item name="android:dialogLayout">@layout/preference_dialog_multiautocompletetextview</item>
</style>
<style name="Preference.AutoCompleteTextView" parent="Preference.DialogPreference.EditTextPreference.Material">
<item name="android:dialogLayout">@layout/preference_dialog_autocompletetextview</item>
</style>
</resources>