add invisible view to block language switch when language has more than one layout

This commit is contained in:
Helium314 2023-10-20 13:44:14 +02:00
parent b8b0ea7dbb
commit a5fb604d2e
3 changed files with 47 additions and 28 deletions

View file

@ -11,7 +11,10 @@ import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
@ -48,7 +51,7 @@ class LanguageFilterListFakePreference(searchField: EditText, recyclerView: Recy
}
class LanguageAdapter(list: List<MutableList<SubtypeInfo>> = listOf(), context: Context) :
private class LanguageAdapter(list: List<MutableList<SubtypeInfo>> = listOf(), context: Context) :
RecyclerView.Adapter<LanguageAdapter.ViewHolder>() {
var onlySystemLocales = false
private val prefs = DeviceProtectedUtils.getSharedPreferences(context)
@ -107,8 +110,8 @@ class LanguageAdapter(list: List<MutableList<SubtypeInfo>> = listOf(), context:
else isVisible = true
}
view.findViewById<Switch>(R.id.language_switch).apply {
isEnabled = !onlySystemLocales && infos.size == 1
view.findViewById<SwitchCompat>(R.id.language_switch).apply {
isEnabled = !onlySystemLocales
// take care: isChecked changes if the language is scrolled out of view and comes back!
// disable the change listener when setting the checked status on scroll
// so it's only triggered on user interactions
@ -116,26 +119,26 @@ class LanguageAdapter(list: List<MutableList<SubtypeInfo>> = listOf(), context:
isChecked = onlySystemLocales || infos.any { it.isEnabled }
setOnCheckedChangeListener { _, b ->
if (b) {
if (infos.size == 1) {
if (!infos.first().hasDictionary)
showMissingDictionaryDialog(context, infos.first().subtype.locale().toLocale())
addEnabledSubtype(prefs, infos.first().subtype)
infos.single().isEnabled = true
} else {
// currently switch is disabled in this case
LanguageSettingsDialog(view.context, infos, fragment, onlySystemLocales, { setupDetailsTextAndSwitch() }).show()
}
if (!infos.first().hasDictionary)
showMissingDictionaryDialog(context, infos.first().subtype.locale().toLocale())
addEnabledSubtype(prefs, infos.first().subtype)
infos.first().isEnabled = true
} else {
if (infos.size == 1) {
removeEnabledSubtype(prefs, infos.first().subtype)
infos.single().isEnabled = false
} else {
// currently switch is disabled in this case
LanguageSettingsDialog(view.context, infos, fragment, onlySystemLocales, { setupDetailsTextAndSwitch() }).show()
}
removeEnabledSubtype(prefs, infos.first().subtype)
infos.first().isEnabled = false
}
}
}
view.findViewById<TextView>(R.id.blocker).apply {
if (infos.size < 2) {
isGone = true
return@apply
}
isVisible = true
setOnClickListener {
LanguageSettingsDialog(view.context, infos, fragment, onlySystemLocales, { setupDetailsTextAndSwitch() }).show()
}
}
}
view.findViewById<TextView>(R.id.language_name).text = infos.first().displayName

View file

@ -8,8 +8,11 @@ import android.net.Uri
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import android.widget.ImageView
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.SwitchCompat
import androidx.core.view.get
import androidx.core.view.isGone
import androidx.core.view.isVisible
@ -99,7 +102,7 @@ class LanguageSettingsDialog(
SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype.subtype)
?: SubtypeLocaleUtils.getSubtypeDisplayNameInSystemLocale(subtype.subtype)
row.findViewById<View>(R.id.language_details).isGone = true
row.findViewById<Switch>(R.id.language_switch).apply {
row.findViewById<SwitchCompat>(R.id.language_switch).apply {
isChecked = subtype.isEnabled
isEnabled = !onlySystemLocales
setOnCheckedChangeListener { _, b ->
@ -115,7 +118,7 @@ class LanguageSettingsDialog(
}
}
if (isAdditionalSubtype(subtype.subtype)) {
row.findViewById<Switch>(R.id.language_switch).isEnabled = true
row.findViewById<SwitchCompat>(R.id.language_switch).isEnabled = true
row.findViewById<ImageView>(R.id.delete_button).apply {
isVisible = true
setOnClickListener {

View file

@ -35,11 +35,24 @@
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/language_switch"
android:padding="6dp"
android:layout_gravity="center_vertical"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="wrap_content" >
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/language_switch"
android:padding="6dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/blocker"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/language_switch"
android:layout_alignStart="@id/language_switch"
android:layout_alignBottom="@id/language_switch"
android:layout_alignTop="@id/language_switch" />
</RelativeLayout>
</LinearLayout>