Skip to content

Fixed default locale and upload locales in descrptions #3166

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 68 additions & 34 deletions app/src/main/java/fr/free/nrw/commons/upload/DescriptionsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
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;
Expand All @@ -24,22 +20,82 @@
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;
}
Expand Down Expand Up @@ -76,11 +132,6 @@ public List<Description> getDescriptions() {
return descriptions;
}

public void addDescription(Description description) {
this.descriptions.add(description);
notifyItemInserted(descriptions.size());
}

public class ViewHolder extends RecyclerView.ViewHolder {

@Nullable
Expand Down Expand Up @@ -127,7 +178,7 @@ public void init(int position) {
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()) {
Expand All @@ -139,14 +190,14 @@ public void init(int position) {

/**
* 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);

Expand All @@ -169,25 +220,8 @@ public void onItemSelected(AdapterView<?> adapterView, View view, int position,
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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -101,7 +79,7 @@ public View getDropDownView(int position, @Nullable View convertView,
convertView = layoutInflater.inflate(resource, parent, false);
}
ViewHolder holder = new ViewHolder(convertView);
holder.init(position, true, savedLanguageValue);
holder.init(position, true);

dropDownClicked = true;
return convertView;
Expand All @@ -118,11 +96,10 @@ View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.init(position, false, savedLanguageValue);
holder.init(position, false);
return convertView;
}


public class ViewHolder {

@BindView(R.id.tv_language)
Expand All @@ -135,17 +112,12 @@ public ViewHolder(View itemView) {
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)
Expand All @@ -172,12 +144,4 @@ public void init(int position, boolean isDropDownView, String savedLanguageValue
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ public static class UploadItem {
this.createdTimestampSource = createdTimestampSource;
title = new Title();
descriptions = new ArrayList<>();
descriptions.add(new Description());
this.place = place;
this.mediaUri = mediaUri;
this.mimeType = mimeType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
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;
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;
Expand All @@ -55,10 +42,14 @@
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 {

Expand Down Expand Up @@ -222,12 +213,20 @@ private void initPresenter() {
* 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
Expand Down Expand Up @@ -395,16 +394,15 @@ public void onButtonCopyPreviousTitleDesc(){
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);
}

}