Skip to content
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

Wrong selected country in rewards #19983

Merged
merged 1 commit into from
Sep 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,32 @@

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);
public CountrySelectionSpinnerAdapter(Context context, String[] countries) {
super(context, android.R.layout.simple_spinner_item, countries);
}

@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);
}

@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);
}

private View createView(int position, View convertView, ViewGroup parent, boolean isDropdown) {
TextView textViewName = convertView.findViewById(android.R.id.text1);
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, null, parent);
TextView textViewName = view.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +40,9 @@
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;

Expand Down Expand Up @@ -171,30 +175,39 @@ public void onGetAvailableCountries(String[] countries) {
updateCountryList(countries);
}

@SuppressLint("ClickableViewAccessibility")
private void updateCountryList(String[] countries) {
shouldShowContinueProgress(false);
mContinueButton.setText(mActivity.getResources().getString(R.string.continue_text));
String defaultCountry = mBraveRewardsNativeWorker.getCountryCode() != null
? new Locale("", mBraveRewardsNativeWorker.getCountryCode()).getDisplayCountry()
: null;

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());
}

ArrayList<String> countryList = new ArrayList<String>();
Collections.sort(list, Collator.getInstance());
ArrayList<String> countryList = new ArrayList<>();
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);
}
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) {
Expand Down