-
Notifications
You must be signed in to change notification settings - Fork 1.3k
#3524 Convert SpinnerLanguagesAdapter to kotlin - converted to kotlin #3528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
misaochan
merged 2 commits into
master
from
macgills/3524-spinner-adadpter-kotlin-conversion
Mar 17, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 0 additions & 173 deletions
173
app/src/main/java/fr/free/nrw/commons/upload/SpinnerLanguagesAdapter.java
This file was deleted.
Oops, something went wrong.
104 changes: 104 additions & 0 deletions
104
app/src/main/java/fr/free/nrw/commons/upload/SpinnerLanguagesAdapter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package fr.free.nrw.commons.upload | ||
|
|
||
| import android.content.Context | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.ArrayAdapter | ||
| import androidx.annotation.LayoutRes | ||
| import androidx.core.os.ConfigurationCompat | ||
| import fr.free.nrw.commons.R | ||
| import fr.free.nrw.commons.utils.BiMap | ||
| import fr.free.nrw.commons.utils.LangCodeUtils | ||
| import kotlinx.android.extensions.LayoutContainer | ||
| import kotlinx.android.synthetic.main.row_item_languages_spinner.* | ||
| import org.apache.commons.lang3.StringUtils | ||
| import java.util.* | ||
|
|
||
| /** | ||
| * This class handles the display of language spinners and their dropdown views for UploadMediaDetailFragment | ||
| * | ||
| * @property selectedLanguages - controls the enabled state of dropdown views | ||
| * | ||
| * @param context - required by super constructor | ||
| */ | ||
| class SpinnerLanguagesAdapter constructor( | ||
| context: Context, | ||
| private val selectedLanguages: BiMap<*, String> | ||
| ) : ArrayAdapter<Any?>(context, -1) { | ||
|
|
||
| private val languageNamesList: List<String> | ||
| private val languageCodesList: List<String> | ||
|
|
||
| init { | ||
| val sortedLanguages = Locale.getAvailableLocales() | ||
| .map(::Language) | ||
| .sortedBy { it.locale.displayName } | ||
| languageNamesList = sortedLanguages.map { it.locale.displayName } | ||
| languageCodesList = sortedLanguages.map { it.locale.language } | ||
| } | ||
|
|
||
| var selectedLangCode = "" | ||
|
|
||
| override fun isEnabled(position: Int) = languageCodesList[position].let { | ||
| it.isNotEmpty() && !selectedLanguages.containsKey(it) && it != selectedLangCode | ||
| } | ||
|
|
||
| override fun getCount() = languageNamesList.size | ||
|
|
||
| override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup) = | ||
| (convertView ?: parent.inflate(R.layout.row_item_languages_spinner).also { | ||
| it.tag = DropDownViewHolder(it) | ||
| }).apply { | ||
| (tag as DropDownViewHolder).init( | ||
| languageCodesList[position], | ||
| languageNamesList[position], | ||
| isEnabled(position) | ||
| ) | ||
| } | ||
|
|
||
| override fun getView(position: Int, convertView: View?, parent: ViewGroup) = | ||
| (convertView ?: parent.inflate(R.layout.row_item_languages_spinner).also { | ||
| it.tag = SpinnerViewHolder(it) | ||
| }).apply { (tag as SpinnerViewHolder).init(languageCodesList[position]) } | ||
|
|
||
| class SpinnerViewHolder(override val containerView: View) : LayoutContainer { | ||
| fun init(languageCode: String) { | ||
| LangCodeUtils.fixLanguageCode(languageCode).let { | ||
| tv_language.text = if (it.length > 2) it.take(2) else it | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class DropDownViewHolder(override val containerView: View) : LayoutContainer { | ||
| fun init(languageCode: String, languageName: String, enabled: Boolean) { | ||
| tv_language.isEnabled = enabled | ||
| if (languageCode.isEmpty()) { | ||
| tv_language.text = StringUtils.capitalize(languageName) | ||
| tv_language.textAlignment = View.TEXT_ALIGNMENT_CENTER | ||
| } else { | ||
| tv_language.text = | ||
| "${StringUtils.capitalize(languageName)}" + | ||
| " [${LangCodeUtils.fixLanguageCode(languageCode)}]" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fun getLanguageCode(position: Int): String { | ||
| return languageCodesList[position] | ||
| } | ||
|
|
||
| fun getIndexOfUserDefaultLocale(context: Context): Int { | ||
| return languageCodesList.indexOf(context.locale.language) | ||
| } | ||
|
|
||
| fun getIndexOfLanguageCode(languageCode: String): Int { | ||
| return languageCodesList.indexOf(languageCode) | ||
| } | ||
| } | ||
|
|
||
| private fun ViewGroup.inflate(@LayoutRes resId: Int) = | ||
| LayoutInflater.from(context).inflate(resId, this, false) | ||
|
|
||
| private val Context.locale: Locale | ||
| get() = ConfigurationCompat.getLocales(resources.configuration)[0] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.