Merge pull request #19983 from brave/32273_wrong_country_selected

Wrong selected country in rewards
This commit is contained in:
Sujit Acharya
2023-09-25 22:30:14 +05:30
committed by GitHub
2 changed files with 37 additions and 54 deletions
@@ -9,62 +9,32 @@ package org.chromium.chrome.browser.rewards.onboarding;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.chromium.chrome.browser.BraveRewardsNativeWorker;
import java.util.ArrayList;
import java.util.Locale;
public class CountrySelectionSpinnerAdapter extends ArrayAdapter<String> {
public CountrySelectionSpinnerAdapter(Context context, ArrayList<String> countryList) {
super(context, 0, countryList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(android.R.layout.simple_spinner_item, parent, false);
}
return createView(position, convertView, parent, false);
public CountrySelectionSpinnerAdapter(Context context, String[] countries) {
super(context, android.R.layout.simple_spinner_item, countries);
}
@Override
public View getDropDownView(
int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView =
LayoutInflater.from(getContext())
.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
}
return createView(position, convertView, parent, true);
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, null, parent);
TextView textViewName = view.findViewById(android.R.id.text1);
private View createView(int position, View convertView, ViewGroup parent, boolean isDropdown) {
TextView textViewName = convertView.findViewById(android.R.id.text1);
String defaultCountry = BraveRewardsNativeWorker.getInstance().getCountryCode() != null
? new Locale("", BraveRewardsNativeWorker.getInstance().getCountryCode())
.getDisplayCountry()
: null;
String currentItem = getItem(position);
if (isDropdown) {
String defaultCountry = BraveRewardsNativeWorker.getInstance().getCountryCode() != null
? new Locale("", BraveRewardsNativeWorker.getInstance().getCountryCode())
.getDisplayCountry()
: null;
if (defaultCountry != null && currentItem.equals(defaultCountry)) {
textViewName.setTypeface(textViewName.getTypeface(), Typeface.BOLD);
}
if (defaultCountry != null && currentItem.equals(defaultCountry)) {
textViewName.setTypeface(textViewName.getTypeface(), Typeface.BOLD);
}
if (currentItem != null) {
textViewName.setText(currentItem);
}
return convertView;
return view;
}
}
@@ -7,12 +7,14 @@
package org.chromium.chrome.browser.rewards.onboarding;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
@@ -38,7 +40,9 @@ import org.chromium.chrome.browser.util.TabUtils;
import org.chromium.ui.permissions.PermissionConstants;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;
import java.util.TreeMap;
@@ -171,30 +175,39 @@ public class RewardsOnboarding implements BraveRewardsObserver {
updateCountryList(countries);
}
@SuppressLint("ClickableViewAccessibility")
private void updateCountryList(String[] countries) {
shouldShowContinueProgress(false);
mContinueButton.setText(mActivity.getResources().getString(R.string.continue_text));
TreeMap<String, String> sortedCountryMap = new TreeMap<String, String>();
for (String countryCode : countries) {
sortedCountryMap.put(new Locale("", countryCode).getDisplayCountry(), countryCode);
}
ArrayList<String> countryList = new ArrayList<String>();
countryList.add(mActivity.getResources().getString(R.string.select_your_country_title));
countryList.addAll(sortedCountryMap.keySet());
String defaultCountry = mBraveRewardsNativeWorker.getCountryCode() != null
? new Locale("", mBraveRewardsNativeWorker.getCountryCode()).getDisplayCountry()
: null;
if (defaultCountry != null && countryList.contains(defaultCountry)) {
countryList.remove(defaultCountry);
countryList.add(1, defaultCountry);
TreeMap<String, String> sortedCountryMap = new TreeMap<String, String>();
ArrayList<String> list = new ArrayList<>();
for (String countryCode : countries) {
sortedCountryMap.put(new Locale("", countryCode).getDisplayCountry(), countryCode);
list.add(new Locale("", countryCode).getDisplayCountry());
}
Collections.sort(list, Collator.getInstance());
ArrayList<String> countryList = new ArrayList<>();
countryList.add(mActivity.getResources().getString(R.string.select_your_country_title));
countryList.addAll(list);
String[] countryArray = countryList.toArray(new String[countryList.size()]);
CountrySelectionSpinnerAdapter countrySelectionSpinnerAdapter =
new CountrySelectionSpinnerAdapter(mActivity, countryList);
new CountrySelectionSpinnerAdapter(mActivity, countryArray);
countrySelectionSpinnerAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
mCountrySpinner.setAdapter(countrySelectionSpinnerAdapter);
mCountrySpinner.setOnTouchListener((view, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
mCountrySpinner.setSelection(
countrySelectionSpinnerAdapter.getPosition(defaultCountry));
}
return false;
});
mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {