mirror of
https://github.com/FossifyOrg/Keyboard.git
synced 2026-04-26 17:01:41 -04:00
added ability to filter/select languages (#19)
* added ability to filter/select languages * fixed string casing * fixed: dvorak missing from init of dialog * First step to moving to mutableSet for selected languages * use MutableSet to store selected languages * Added callback to selectedLanguagesToToggle to invoke the changes * Simplify code by saving languages in a mutable set of Ints instead of strings * switched to a recycler view for the language selection dialog * Rename preference and format code - Renamed 'Selected keyboard languages' to 'Manage keyboard languages' - Simplify/format code --------- Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com> Co-authored-by: Naveen Singh <snaveen935@gmail.com>
This commit is contained in:
@@ -8,9 +8,10 @@ import org.fossify.commons.helpers.NavigationIcon
|
||||
import org.fossify.commons.helpers.isTiramisuPlus
|
||||
import org.fossify.commons.models.RadioItem
|
||||
import org.fossify.keyboard.databinding.ActivitySettingsBinding
|
||||
import org.fossify.keyboard.dialogs.ManageKeyboardLanguagesDialog
|
||||
import org.fossify.keyboard.extensions.config
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguageText
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguages
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguagesRadioItems
|
||||
import org.fossify.keyboard.helpers.*
|
||||
import java.util.Locale
|
||||
import kotlin.system.exitProcess
|
||||
@@ -41,6 +42,7 @@ class SettingsActivity : SimpleActivity() {
|
||||
setupVibrateOnKeypress()
|
||||
setupShowPopupOnKeypress()
|
||||
setupShowKeyBorders()
|
||||
setupManageKeyboardLanguages()
|
||||
setupKeyboardLanguage()
|
||||
setupKeyboardHeightMultiplier()
|
||||
setupShowClipboardContent()
|
||||
@@ -134,11 +136,25 @@ class SettingsActivity : SimpleActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupManageKeyboardLanguages() {
|
||||
binding.apply {
|
||||
settingsManageKeyboardLanguagesHolder.setOnClickListener {
|
||||
ManageKeyboardLanguagesDialog(this@SettingsActivity) { selectedLanguages ->
|
||||
config.selectedLanguages = selectedLanguages
|
||||
if (config.keyboardLanguage !in selectedLanguages) {
|
||||
config.keyboardLanguage = selectedLanguages.first()
|
||||
}
|
||||
settingsKeyboardLanguage.text = getKeyboardLanguageText(config.keyboardLanguage)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupKeyboardLanguage() {
|
||||
binding.apply {
|
||||
settingsKeyboardLanguage.text = getKeyboardLanguageText(config.keyboardLanguage)
|
||||
settingsKeyboardLanguageHolder.setOnClickListener {
|
||||
val items = getKeyboardLanguages()
|
||||
val items = getKeyboardLanguagesRadioItems()
|
||||
RadioGroupDialog(this@SettingsActivity, items, config.keyboardLanguage) {
|
||||
config.keyboardLanguage = it as Int
|
||||
settingsKeyboardLanguage.text = getKeyboardLanguageText(config.keyboardLanguage)
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.fossify.keyboard.adapters
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.fossify.commons.views.MyAppCompatCheckbox
|
||||
import org.fossify.keyboard.R
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguageText
|
||||
import org.fossify.keyboard.helpers.Config
|
||||
|
||||
internal class ManageKeyboardLanguagesAdapter(
|
||||
private val config: Config,
|
||||
private var languagesList: List<Int>,
|
||||
) : RecyclerView.Adapter<ManageKeyboardLanguagesAdapter.MyViewHolder>() {
|
||||
private val selectedLanguages = config.selectedLanguages
|
||||
|
||||
internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||
var languageCheckboxItem: MyAppCompatCheckbox = view.findViewById(R.id.language_checkbox)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
|
||||
val itemView = LayoutInflater.from(parent.context)
|
||||
.inflate(R.layout.language_checkbox_item, parent, false)
|
||||
return MyViewHolder(itemView)
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
|
||||
val item = languagesList[position]
|
||||
holder.languageCheckboxItem.apply {
|
||||
text = config.context.getKeyboardLanguageText(item)
|
||||
isChecked = selectedLanguages.contains(item)
|
||||
|
||||
setOnClickListener {
|
||||
if (isChecked) {
|
||||
selectedLanguages.add(item)
|
||||
} else {
|
||||
selectedLanguages.remove(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return languagesList.size
|
||||
}
|
||||
|
||||
fun getSelectedLanguages(): MutableSet<Int> {
|
||||
val defaultLang = config.getDefaultLanguage()
|
||||
if (selectedLanguages.size == 0) {
|
||||
selectedLanguages.add(defaultLang)
|
||||
}
|
||||
return selectedLanguages
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package org.fossify.keyboard.dialogs
|
||||
|
||||
import android.view.View
|
||||
import org.fossify.keyboard.extensions.config
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguages
|
||||
import org.fossify.keyboard.extensions.getKeyboardLanguagesRadioItems
|
||||
|
||||
class ChangeLanguagePopup(
|
||||
inputView: View,
|
||||
@@ -12,7 +12,7 @@ class ChangeLanguagePopup(
|
||||
private val config = context.config
|
||||
|
||||
init {
|
||||
val items = context.getKeyboardLanguages()
|
||||
val items = context.getKeyboardLanguagesRadioItems()
|
||||
KeyboardRadioGroupDialog(inputView, items, config.keyboardLanguage) {
|
||||
config.keyboardLanguage = it as Int
|
||||
onSelect.invoke()
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.fossify.keyboard.dialogs
|
||||
|
||||
import org.fossify.commons.activities.BaseSimpleActivity
|
||||
import org.fossify.commons.extensions.getAlertDialogBuilder
|
||||
import org.fossify.commons.extensions.setupDialogStuff
|
||||
import org.fossify.keyboard.R
|
||||
import org.fossify.keyboard.adapters.ManageKeyboardLanguagesAdapter
|
||||
import org.fossify.keyboard.databinding.DialogManageKeyboardLanguagesBinding
|
||||
import org.fossify.keyboard.extensions.config
|
||||
import org.fossify.keyboard.helpers.SUPPORTED_LANGUAGES
|
||||
|
||||
class ManageKeyboardLanguagesDialog(
|
||||
private val activity: BaseSimpleActivity,
|
||||
private val callback: (MutableSet<Int>) -> Unit
|
||||
) {
|
||||
init {
|
||||
val binding = DialogManageKeyboardLanguagesBinding.inflate(activity.layoutInflater)
|
||||
val adapter = ManageKeyboardLanguagesAdapter(activity.config, SUPPORTED_LANGUAGES)
|
||||
binding.keyboardLanguageList.adapter = adapter
|
||||
|
||||
activity.getAlertDialogBuilder()
|
||||
.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
|
||||
.setPositiveButton(R.string.ok) { _, _ -> callback(adapter.getSelectedLanguages()) }
|
||||
.apply {
|
||||
activity.setupDialogStuff(binding.root, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,30 +167,14 @@ fun Context.setupKeyboardDialogStuff(
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.getKeyboardLanguages(): ArrayList<RadioItem> {
|
||||
return arrayListOf(
|
||||
RadioItem(LANGUAGE_BENGALI, getKeyboardLanguageText(LANGUAGE_BENGALI)),
|
||||
RadioItem(LANGUAGE_BULGARIAN, getKeyboardLanguageText(LANGUAGE_BULGARIAN)),
|
||||
RadioItem(LANGUAGE_DANISH, getKeyboardLanguageText(LANGUAGE_DANISH)),
|
||||
RadioItem(LANGUAGE_ENGLISH_QWERTY, getKeyboardLanguageText(LANGUAGE_ENGLISH_QWERTY)),
|
||||
RadioItem(LANGUAGE_ENGLISH_QWERTZ, getKeyboardLanguageText(LANGUAGE_ENGLISH_QWERTZ)),
|
||||
RadioItem(LANGUAGE_ENGLISH_DVORAK, getKeyboardLanguageText(LANGUAGE_ENGLISH_DVORAK)),
|
||||
RadioItem(LANGUAGE_FRENCH_AZERTY, getKeyboardLanguageText(LANGUAGE_FRENCH_AZERTY)),
|
||||
RadioItem(LANGUAGE_FRENCH_BEPO, getKeyboardLanguageText(LANGUAGE_FRENCH_BEPO)),
|
||||
RadioItem(LANGUAGE_GERMAN, getKeyboardLanguageText(LANGUAGE_GERMAN)),
|
||||
RadioItem(LANGUAGE_GREEK, getKeyboardLanguageText(LANGUAGE_GREEK)),
|
||||
RadioItem(LANGUAGE_LITHUANIAN, getKeyboardLanguageText(LANGUAGE_LITHUANIAN)),
|
||||
RadioItem(LANGUAGE_NORWEGIAN, getKeyboardLanguageText(LANGUAGE_NORWEGIAN)),
|
||||
RadioItem(LANGUAGE_POLISH, getKeyboardLanguageText(LANGUAGE_POLISH)),
|
||||
RadioItem(LANGUAGE_ROMANIAN, getKeyboardLanguageText(LANGUAGE_ROMANIAN)),
|
||||
RadioItem(LANGUAGE_RUSSIAN, getKeyboardLanguageText(LANGUAGE_RUSSIAN)),
|
||||
RadioItem(LANGUAGE_SLOVENIAN, getKeyboardLanguageText(LANGUAGE_SLOVENIAN)),
|
||||
RadioItem(LANGUAGE_SPANISH, getKeyboardLanguageText(LANGUAGE_SPANISH)),
|
||||
RadioItem(LANGUAGE_SWEDISH, getKeyboardLanguageText(LANGUAGE_SWEDISH)),
|
||||
RadioItem(LANGUAGE_TURKISH_Q, getKeyboardLanguageText(LANGUAGE_TURKISH_Q)),
|
||||
RadioItem(LANGUAGE_UKRAINIAN, getKeyboardLanguageText(LANGUAGE_UKRAINIAN)),
|
||||
RadioItem(LANGUAGE_VIETNAMESE_TELEX, getKeyboardLanguageText(LANGUAGE_VIETNAMESE_TELEX)),
|
||||
)
|
||||
fun Context.getKeyboardLanguagesRadioItems(): ArrayList<RadioItem> {
|
||||
val selectedLanguagesRadioItems = arrayListOf<RadioItem>()
|
||||
|
||||
for (lang in config.selectedLanguages) {
|
||||
selectedLanguagesRadioItems.add(RadioItem(lang, getKeyboardLanguageText(lang)))
|
||||
}
|
||||
|
||||
return selectedLanguagesRadioItems
|
||||
}
|
||||
|
||||
fun Context.getKeyboardLanguageText(language: Int): String {
|
||||
|
||||
@@ -51,7 +51,18 @@ class Config(context: Context) : BaseConfig(context) {
|
||||
}
|
||||
set(showNumbersRow) = prefs.edit().putBoolean(SHOW_NUMBERS_ROW, showNumbersRow).apply()
|
||||
|
||||
private fun getDefaultLanguage(): Int {
|
||||
var selectedLanguages: MutableSet<Int>
|
||||
get() {
|
||||
val defaultLanguage = getDefaultLanguage().toString()
|
||||
val stringSet = prefs.getStringSet(SELECTED_LANGUAGES, hashSetOf(defaultLanguage))!!
|
||||
return stringSet.map { it.toInt() }.toMutableSet()
|
||||
}
|
||||
set(selectedLanguages) {
|
||||
val stringSet = selectedLanguages.map { it.toString() }.toSet()
|
||||
prefs.edit().putStringSet(SELECTED_LANGUAGES, stringSet).apply()
|
||||
}
|
||||
|
||||
fun getDefaultLanguage(): Int {
|
||||
val conf = context.resources.configuration
|
||||
return if (conf.locale.toString().lowercase(Locale.getDefault()).startsWith("ru_")) {
|
||||
LANGUAGE_RUSSIAN
|
||||
|
||||
@@ -20,6 +20,7 @@ const val KEYBOARD_LANGUAGE = "keyboard_language"
|
||||
const val HEIGHT_PERCENTAGE = "height_percentage"
|
||||
const val SHOW_CLIPBOARD_CONTENT = "show_clipboard_content"
|
||||
const val SHOW_NUMBERS_ROW = "show_numbers_row"
|
||||
const val SELECTED_LANGUAGES = "selected_languages"
|
||||
|
||||
// differentiate current and pinned clips at the keyboards' Clipboard section
|
||||
const val ITEM_SECTION_LABEL = 0
|
||||
@@ -47,6 +48,31 @@ const val LANGUAGE_VIETNAMESE_TELEX = 18
|
||||
const val LANGUAGE_POLISH = 19
|
||||
const val LANGUAGE_UKRAINIAN = 20
|
||||
|
||||
// Keep this sorted
|
||||
val SUPPORTED_LANGUAGES = listOf(
|
||||
LANGUAGE_BENGALI,
|
||||
LANGUAGE_BULGARIAN,
|
||||
LANGUAGE_DANISH,
|
||||
LANGUAGE_ENGLISH_QWERTY,
|
||||
LANGUAGE_ENGLISH_QWERTZ,
|
||||
LANGUAGE_ENGLISH_DVORAK,
|
||||
LANGUAGE_FRENCH_AZERTY,
|
||||
LANGUAGE_FRENCH_BEPO,
|
||||
LANGUAGE_GERMAN,
|
||||
LANGUAGE_GREEK,
|
||||
LANGUAGE_LITHUANIAN,
|
||||
LANGUAGE_NORWEGIAN,
|
||||
LANGUAGE_POLISH,
|
||||
LANGUAGE_ROMANIAN,
|
||||
LANGUAGE_RUSSIAN,
|
||||
LANGUAGE_SLOVENIAN,
|
||||
LANGUAGE_SPANISH,
|
||||
LANGUAGE_SWEDISH,
|
||||
LANGUAGE_TURKISH_Q,
|
||||
LANGUAGE_UKRAINIAN,
|
||||
LANGUAGE_VIETNAMESE_TELEX
|
||||
)
|
||||
|
||||
// keyboard height percentage options
|
||||
const val KEYBOARD_HEIGHT_70_PERCENT = 70
|
||||
const val KEYBOARD_HEIGHT_80_PERCENT = 80
|
||||
|
||||
@@ -221,6 +221,21 @@
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/settings_manage_keyboard_languages_holder"
|
||||
style="@style/SettingsHolderTextViewStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<org.fossify.commons.views.MyTextView
|
||||
android:id="@+id/settings_manage_keyboard_languages"
|
||||
style="@style/SettingsTextLabelStyle"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/manage_keyboard_languages" />
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/settings_keyboard_language_holder"
|
||||
style="@style/SettingsHolderTextViewStyle"
|
||||
|
||||
16
app/src/main/res/layout/dialog_manage_keyboard_languages.xml
Normal file
16
app/src/main/res/layout/dialog_manage_keyboard_languages.xml
Normal file
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/keyboard_language_list_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/keyboard_language_list"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layoutManager="org.fossify.commons.views.MyLinearLayoutManager"
|
||||
tools:listitem="@layout/language_checkbox_item" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
23
app/src/main/res/layout/language_checkbox_item.xml
Normal file
23
app/src/main/res/layout/language_checkbox_item.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RelativeLayout
|
||||
style="@style/SettingsHolderCheckboxStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/selectableItemBackground"
|
||||
android:clickable="false"
|
||||
android:focusable="false">
|
||||
|
||||
<org.fossify.commons.views.MyAppCompatCheckbox
|
||||
android:id="@+id/language_checkbox"
|
||||
style="@style/SettingsCheckboxStyle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:text="English" />
|
||||
</RelativeLayout>
|
||||
</LinearLayout>
|
||||
@@ -14,6 +14,7 @@
|
||||
<string name="text_pinned">تم تثبيت النص</string>
|
||||
<string name="keycode_mode_change">تغيير نوع لوحة المفاتيح</string>
|
||||
<string name="keycode_space">شريط المسافة</string>
|
||||
<string name="manage_keyboard_languages">إدارة لغات لوحة المفاتيح</string>
|
||||
<string name="show_clipboard_content">إظهار محتوى الحافظة إذا كان متوفرا</string>
|
||||
<string name="show_popup">إظهار نافذة منبثقة عند الضغط على المفاتيح</string>
|
||||
<string name="vibrate_on_keypress">يهتز عند ضغط الزر</string>
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
<string name="clipboard_current">Current</string>
|
||||
<string name="clipboard_pinned">Pinned</string>
|
||||
<string name="add_new_item">Add a new item</string>
|
||||
<string name="manage_keyboard_languages">Manage keyboard languages</string>
|
||||
<string name="manage_clips">You can manage or add clips here for quick access.</string>
|
||||
<string name="clip_text">Clip text</string>
|
||||
<string name="pin_text">Pin text</string>
|
||||
|
||||
Reference in New Issue
Block a user