mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Fixed default locale and upload locales in descrptions (#3166)
This commit is contained in:
parent
55edc34dee
commit
533ba60fae
4 changed files with 103 additions and 108 deletions
|
|
@ -10,10 +10,6 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.AdapterView.OnItemSelectedListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatEditText;
|
||||
|
|
@ -24,22 +20,82 @@ import butterknife.ButterKnife;
|
|||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.utils.AbstractTextWatcher;
|
||||
import fr.free.nrw.commons.utils.BiMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class DescriptionsAdapter extends RecyclerView.Adapter<DescriptionsAdapter.ViewHolder> {
|
||||
|
||||
private final String userDefaultLocale;
|
||||
private List<Description> descriptions;
|
||||
private Callback callback;
|
||||
|
||||
private BiMap<AdapterView, String> selectedLanguages;
|
||||
private String savedLanguageValue;
|
||||
private List<String> languageCodesList=new ArrayList<>();
|
||||
private List<String> languageNamesList=new ArrayList<>();
|
||||
|
||||
public DescriptionsAdapter(String savedLanguageValue) {
|
||||
public DescriptionsAdapter(String savedLanguageValue, String userDefaultLocale) {
|
||||
descriptions = new ArrayList<>();
|
||||
selectedLanguages = new BiMap<>();
|
||||
prepareLanguages();
|
||||
this.userDefaultLocale=userDefaultLocale;
|
||||
this.savedLanguageValue = savedLanguageValue;
|
||||
}
|
||||
|
||||
private void prepareLanguages() {
|
||||
List<Language> languages = getLocaleSupportedByDevice();
|
||||
|
||||
for(Language language: languages) {
|
||||
if(!languageCodesList.contains(language.getLocale().getLanguage())) {
|
||||
languageNamesList.add(language.getLocale().getDisplayName());
|
||||
languageCodesList.add(language.getLocale().getLanguage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Language> getLocaleSupportedByDevice() {
|
||||
List<Language> languages = new ArrayList<>();
|
||||
Locale[] localesArray = Locale.getAvailableLocales();
|
||||
for (Locale locale : localesArray) {
|
||||
languages.add(new Language(locale));
|
||||
}
|
||||
|
||||
Collections.sort(languages, (language, t1) -> language.getLocale().getDisplayName()
|
||||
.compareTo(t1.getLocale().getDisplayName()));
|
||||
return languages;
|
||||
}
|
||||
|
||||
int getIndexOfLanguageCode(String languageCode) {
|
||||
return languageCodesList.indexOf(languageCode);
|
||||
}
|
||||
|
||||
public void addDescription(Description description) {
|
||||
if (description.getSelectedLanguageIndex() == -1) {
|
||||
int localeIndex = 0;
|
||||
String languageValue;
|
||||
if (!TextUtils.isEmpty(savedLanguageValue)) {
|
||||
// If user has chosen a default language from settings activity savedLanguageValue is not null
|
||||
localeIndex = getIndexOfLanguageCode(savedLanguageValue);
|
||||
languageValue = savedLanguageValue;
|
||||
} else {
|
||||
if (descriptions.isEmpty()) {//If this is the first description, lets let him add the description of his locale
|
||||
localeIndex =
|
||||
getIndexOfLanguageCode(
|
||||
userDefaultLocale);
|
||||
}
|
||||
languageValue = languageCodesList.get(localeIndex);
|
||||
}
|
||||
description.setSelectedLanguageIndex(localeIndex);
|
||||
description.setLanguageCode(languageValue);
|
||||
}
|
||||
this.descriptions.add(description);
|
||||
notifyItemInserted(descriptions.size());
|
||||
}
|
||||
|
||||
|
||||
public void setCallback(Callback callback) {
|
||||
this.callback = callback;
|
||||
}
|
||||
|
|
@ -76,11 +132,6 @@ public class DescriptionsAdapter extends RecyclerView.Adapter<DescriptionsAdapte
|
|||
return descriptions;
|
||||
}
|
||||
|
||||
public void addDescription(Description description) {
|
||||
this.descriptions.add(description);
|
||||
notifyItemInserted(descriptions.size());
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
@Nullable
|
||||
|
|
@ -127,7 +178,7 @@ public class DescriptionsAdapter extends RecyclerView.Adapter<DescriptionsAdapte
|
|||
descItemEditText.addTextChangedListener(new AbstractTextWatcher(
|
||||
descriptionText -> descriptions.get(position)
|
||||
.setDescriptionText(descriptionText)));
|
||||
initLanguageSpinner(position, description);
|
||||
initLanguageSpinner(description);
|
||||
|
||||
//If the description was manually added by the user, it deserves focus, if not, let the user decide
|
||||
if (description.isManuallyAdded()) {
|
||||
|
|
@ -139,14 +190,14 @@ public class DescriptionsAdapter extends RecyclerView.Adapter<DescriptionsAdapte
|
|||
|
||||
/**
|
||||
* Extracted out the function to init the language spinner with different system supported languages
|
||||
* @param position
|
||||
* @param description
|
||||
*/
|
||||
private void initLanguageSpinner(int position, Description description) {
|
||||
private void initLanguageSpinner(Description description) {
|
||||
SpinnerLanguagesAdapter languagesAdapter = new SpinnerLanguagesAdapter(
|
||||
spinnerDescriptionLanguages.getContext(),
|
||||
R.layout.row_item_languages_spinner, selectedLanguages,
|
||||
savedLanguageValue);
|
||||
R.layout.row_item_languages_spinner, selectedLanguages);
|
||||
languagesAdapter.setLanguageCodes(languageCodesList);
|
||||
languagesAdapter.setLanguageNames(languageNamesList);
|
||||
languagesAdapter.notifyDataSetChanged();
|
||||
spinnerDescriptionLanguages.setAdapter(languagesAdapter);
|
||||
|
||||
|
|
@ -169,25 +220,8 @@ public class DescriptionsAdapter extends RecyclerView.Adapter<DescriptionsAdapte
|
|||
public void onNothingSelected(AdapterView<?> adapterView) {
|
||||
}
|
||||
});
|
||||
|
||||
if (description.getSelectedLanguageIndex() == -1) {
|
||||
if (savedLanguageValue != null) {
|
||||
// If user has chosen a default language from settings activity savedLanguageValue is not null
|
||||
spinnerDescriptionLanguages.setSelection(languagesAdapter.getIndexOfLanguageCode(savedLanguageValue));
|
||||
} else {
|
||||
if (position == 0) {
|
||||
int defaultLocaleIndex = languagesAdapter
|
||||
.getIndexOfUserDefaultLocale(spinnerDescriptionLanguages.getContext());
|
||||
spinnerDescriptionLanguages.setSelection(defaultLocaleIndex, true);
|
||||
} else {
|
||||
spinnerDescriptionLanguages.setSelection(0);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
spinnerDescriptionLanguages.setSelection(description.getSelectedLanguageIndex());
|
||||
selectedLanguages.put(spinnerDescriptionLanguages, description.getLanguageCode());
|
||||
}
|
||||
spinnerDescriptionLanguages.setSelection(description.getSelectedLanguageIndex());
|
||||
selectedLanguages.put(spinnerDescriptionLanguages, description.getLanguageCode());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -37,49 +37,27 @@ public class SpinnerLanguagesAdapter extends ArrayAdapter {
|
|||
private List<String> languageCodesList;
|
||||
private final BiMap<AdapterView, String> selectedLanguages;
|
||||
public String selectedLangCode="";
|
||||
private Context context;
|
||||
private boolean dropDownClicked;
|
||||
private String savedLanguageValue;
|
||||
|
||||
|
||||
|
||||
public SpinnerLanguagesAdapter(@NonNull Context context,
|
||||
int resource,
|
||||
BiMap<AdapterView, String> selectedLanguages,
|
||||
String savedLanguageValue) {
|
||||
BiMap<AdapterView, String> selectedLanguages) {
|
||||
super(context, resource);
|
||||
this.resource = resource;
|
||||
this.layoutInflater = LayoutInflater.from(context);
|
||||
languageNamesList = new ArrayList<>();
|
||||
languageCodesList = new ArrayList<>();
|
||||
prepareLanguages();
|
||||
this.selectedLanguages = selectedLanguages;
|
||||
this.context = context;
|
||||
this.dropDownClicked = false;
|
||||
this.savedLanguageValue = savedLanguageValue;
|
||||
}
|
||||
|
||||
private void prepareLanguages() {
|
||||
List<Language> languages = getLocaleSupportedByDevice();
|
||||
|
||||
for(Language language: languages) {
|
||||
if(!languageCodesList.contains(language.getLocale().getLanguage())) {
|
||||
languageNamesList.add(language.getLocale().getDisplayName());
|
||||
languageCodesList.add(language.getLocale().getLanguage());
|
||||
}
|
||||
}
|
||||
public void setLanguageCodes(List<String> languageCodesList) {
|
||||
this.languageCodesList=languageCodesList;
|
||||
}
|
||||
|
||||
private List<Language> getLocaleSupportedByDevice() {
|
||||
List<Language> languages = new ArrayList<>();
|
||||
Locale[] localesArray = Locale.getAvailableLocales();
|
||||
for (Locale locale : localesArray) {
|
||||
languages.add(new Language(locale));
|
||||
}
|
||||
|
||||
Collections.sort(languages, (language, t1) -> language.getLocale().getDisplayName()
|
||||
.compareTo(t1.getLocale().getDisplayName()));
|
||||
return languages;
|
||||
public void setLanguageNames(List<String> languageNamesList) {
|
||||
this.languageNamesList = languageNamesList;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -101,7 +79,7 @@ public class SpinnerLanguagesAdapter extends ArrayAdapter {
|
|||
convertView = layoutInflater.inflate(resource, parent, false);
|
||||
}
|
||||
ViewHolder holder = new ViewHolder(convertView);
|
||||
holder.init(position, true, savedLanguageValue);
|
||||
holder.init(position, true);
|
||||
|
||||
dropDownClicked = true;
|
||||
return convertView;
|
||||
|
|
@ -118,11 +96,10 @@ public class SpinnerLanguagesAdapter extends ArrayAdapter {
|
|||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
holder.init(position, false, savedLanguageValue);
|
||||
holder.init(position, false);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
|
||||
public class ViewHolder {
|
||||
|
||||
@BindView(R.id.tv_language)
|
||||
|
|
@ -135,17 +112,12 @@ public class SpinnerLanguagesAdapter extends ArrayAdapter {
|
|||
ButterKnife.bind(this, itemView);
|
||||
}
|
||||
|
||||
public void init(int position, boolean isDropDownView, String savedLanguageValue) {
|
||||
public void init(int position, boolean isDropDownView) {
|
||||
String languageCode = LangCodeUtils.fixLanguageCode(languageCodesList.get(position));
|
||||
final String languageName = StringUtils.capitalize(languageNamesList.get(position));
|
||||
|
||||
if(savedLanguageValue.equals("")){
|
||||
savedLanguageValue = Locale.getDefault().getLanguage();
|
||||
}
|
||||
|
||||
if (!isDropDownView) {
|
||||
if( !dropDownClicked){
|
||||
languageCode = LangCodeUtils.fixLanguageCode(savedLanguageValue);
|
||||
languageCode = LangCodeUtils.fixLanguageCode(languageCode);
|
||||
}
|
||||
view.setVisibility(View.GONE);
|
||||
if (languageCode.length() > 2)
|
||||
|
|
@ -172,12 +144,4 @@ public class SpinnerLanguagesAdapter extends ArrayAdapter {
|
|||
String getLanguageCode(int position) {
|
||||
return languageCodesList.get(position);
|
||||
}
|
||||
|
||||
int getIndexOfUserDefaultLocale(Context context) {
|
||||
return languageCodesList.indexOf(context.getResources().getConfiguration().locale.getLanguage());
|
||||
}
|
||||
|
||||
int getIndexOfLanguageCode(String languageCode) {
|
||||
return languageCodesList.indexOf(languageCode);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -264,7 +264,6 @@ public class UploadModel {
|
|||
this.createdTimestampSource = createdTimestampSource;
|
||||
title = new Title();
|
||||
descriptions = new ArrayList<>();
|
||||
descriptions.add(new Description());
|
||||
this.place = place;
|
||||
this.mediaUri = mediaUri;
|
||||
this.mimeType = mimeType;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
package fr.free.nrw.commons.upload.mediaDetails;
|
||||
|
||||
import static fr.free.nrw.commons.utils.ImageUtils.getErrorMessageForResult;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
|
@ -13,30 +13,17 @@ import android.view.ViewGroup;
|
|||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.AppCompatButton;
|
||||
import androidx.appcompat.widget.AppCompatImageButton;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.github.chrisbanes.photoview.OnScaleChangedListener;
|
||||
import com.github.chrisbanes.photoview.PhotoView;
|
||||
import com.jakewharton.rxbinding2.widget.RxTextView;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import com.github.chrisbanes.photoview.PhotoView;
|
||||
import com.jakewharton.rxbinding2.widget.RxTextView;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.Utils;
|
||||
import fr.free.nrw.commons.filepicker.UploadableFile;
|
||||
|
|
@ -55,10 +42,14 @@ import fr.free.nrw.commons.utils.DialogUtil;
|
|||
import fr.free.nrw.commons.utils.ImageUtils;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static fr.free.nrw.commons.utils.ImageUtils.getErrorMessageForResult;
|
||||
|
||||
public class UploadMediaDetailFragment extends UploadBaseFragment implements
|
||||
UploadMediaDetailsContract.View {
|
||||
|
||||
|
|
@ -222,12 +213,20 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
|
|||
* init the recycler veiw
|
||||
*/
|
||||
private void initRecyclerView() {
|
||||
descriptionsAdapter = new DescriptionsAdapter(defaultKvStore.getString(Prefs.KEY_LANGUAGE_VALUE,""));
|
||||
descriptionsAdapter = new DescriptionsAdapter(defaultKvStore.getString(Prefs.KEY_LANGUAGE_VALUE,""),getUserDefaultLocale());
|
||||
descriptionsAdapter.setCallback(this::showInfoAlert);
|
||||
rvDescriptions.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
rvDescriptions.setAdapter(descriptionsAdapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the default locale value of the user's device
|
||||
* @return
|
||||
*/
|
||||
private String getUserDefaultLocale() {
|
||||
return getContext().getResources().getConfiguration().locale.getLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* show dialog with info
|
||||
* @param titleStringID
|
||||
|
|
@ -395,16 +394,15 @@ public class UploadMediaDetailFragment extends UploadBaseFragment implements
|
|||
presenter.fetchPreviousTitleAndDescription(callback.getIndexInViewFlipper(this));
|
||||
}
|
||||
|
||||
private void setDescriptionsInAdapter(List<Description> descriptions){
|
||||
if(descriptions==null){
|
||||
descriptions=new ArrayList<>();
|
||||
private void setDescriptionsInAdapter(List<Description> descriptions) {
|
||||
if (descriptions == null) {
|
||||
descriptions = new ArrayList<>();
|
||||
}
|
||||
|
||||
if(descriptions.size()==0){
|
||||
descriptions.add(new Description());
|
||||
if (descriptions.size() == 0) {
|
||||
descriptionsAdapter.addDescription(new Description());
|
||||
} else {
|
||||
descriptionsAdapter.setItems(descriptions);
|
||||
}
|
||||
|
||||
descriptionsAdapter.setItems(descriptions);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue