|
| 1 | +package fr.free.nrw.commons.upload |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.LayoutInflater |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import android.widget.ArrayAdapter |
| 8 | +import androidx.annotation.LayoutRes |
| 9 | +import androidx.core.os.ConfigurationCompat |
| 10 | +import fr.free.nrw.commons.R |
| 11 | +import fr.free.nrw.commons.utils.BiMap |
| 12 | +import fr.free.nrw.commons.utils.LangCodeUtils |
| 13 | +import kotlinx.android.extensions.LayoutContainer |
| 14 | +import kotlinx.android.synthetic.main.row_item_languages_spinner.* |
| 15 | +import org.apache.commons.lang3.StringUtils |
| 16 | +import java.util.* |
| 17 | + |
| 18 | +/** |
| 19 | + * This class handles the display of language spinners and their dropdown views for UploadMediaDetailFragment |
| 20 | + * |
| 21 | + * @property selectedLanguages - controls the enabled state of dropdown views |
| 22 | + * |
| 23 | + * @param context - required by super constructor |
| 24 | + */ |
| 25 | +class SpinnerLanguagesAdapter constructor( |
| 26 | + context: Context, |
| 27 | + private val selectedLanguages: BiMap<*, String> |
| 28 | +) : ArrayAdapter<Any?>(context, -1) { |
| 29 | + |
| 30 | + private val languageNamesList: List<String> |
| 31 | + private val languageCodesList: List<String> |
| 32 | + |
| 33 | + init { |
| 34 | + val sortedLanguages = Locale.getAvailableLocales() |
| 35 | + .map(::Language) |
| 36 | + .sortedBy { it.locale.displayName } |
| 37 | + languageNamesList = sortedLanguages.map { it.locale.displayName } |
| 38 | + languageCodesList = sortedLanguages.map { it.locale.language } |
| 39 | + } |
| 40 | + |
| 41 | + var selectedLangCode = "" |
| 42 | + |
| 43 | + override fun isEnabled(position: Int) = languageCodesList[position].let { |
| 44 | + it.isNotEmpty() && !selectedLanguages.containsKey(it) && it != selectedLangCode |
| 45 | + } |
| 46 | + |
| 47 | + override fun getCount() = languageNamesList.size |
| 48 | + |
| 49 | + override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup) = |
| 50 | + (convertView ?: parent.inflate(R.layout.row_item_languages_spinner).also { |
| 51 | + it.tag = DropDownViewHolder(it) |
| 52 | + }).apply { |
| 53 | + (tag as DropDownViewHolder).init( |
| 54 | + languageCodesList[position], |
| 55 | + languageNamesList[position], |
| 56 | + isEnabled(position) |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override fun getView(position: Int, convertView: View?, parent: ViewGroup) = |
| 61 | + (convertView ?: parent.inflate(R.layout.row_item_languages_spinner).also { |
| 62 | + it.tag = SpinnerViewHolder(it) |
| 63 | + }).apply { (tag as SpinnerViewHolder).init(languageCodesList[position]) } |
| 64 | + |
| 65 | + class SpinnerViewHolder(override val containerView: View) : LayoutContainer { |
| 66 | + fun init(languageCode: String) { |
| 67 | + LangCodeUtils.fixLanguageCode(languageCode).let { |
| 68 | + tv_language.text = if (it.length > 2) it.take(2) else it |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + class DropDownViewHolder(override val containerView: View) : LayoutContainer { |
| 74 | + fun init(languageCode: String, languageName: String, enabled: Boolean) { |
| 75 | + tv_language.isEnabled = enabled |
| 76 | + if (languageCode.isEmpty()) { |
| 77 | + tv_language.text = StringUtils.capitalize(languageName) |
| 78 | + tv_language.textAlignment = View.TEXT_ALIGNMENT_CENTER |
| 79 | + } else { |
| 80 | + tv_language.text = |
| 81 | + "${StringUtils.capitalize(languageName)}" + |
| 82 | + " [${LangCodeUtils.fixLanguageCode(languageCode)}]" |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + fun getLanguageCode(position: Int): String { |
| 88 | + return languageCodesList[position] |
| 89 | + } |
| 90 | + |
| 91 | + fun getIndexOfUserDefaultLocale(context: Context): Int { |
| 92 | + return languageCodesList.indexOf(context.locale.language) |
| 93 | + } |
| 94 | + |
| 95 | + fun getIndexOfLanguageCode(languageCode: String): Int { |
| 96 | + return languageCodesList.indexOf(languageCode) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +private fun ViewGroup.inflate(@LayoutRes resId: Int) = |
| 101 | + LayoutInflater.from(context).inflate(resId, this, false) |
| 102 | + |
| 103 | +private val Context.locale: Locale |
| 104 | + get() = ConfigurationCompat.getLocales(resources.configuration)[0] |
0 commit comments