Skip to content

Commit 90ba453

Browse files
committed
select defaultcurrencyCode.
1 parent 019ec65 commit 90ba453

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

transfermethodui/src/main/java/com/hyperwallet/android/ui/transfermethod/view/SelectTransferMethodPresenter.java

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public class SelectTransferMethodPresenter implements SelectTransferMethodContra
4848
private final SelectTransferMethodContract.View mView;
4949

5050
public SelectTransferMethodPresenter(SelectTransferMethodContract.View view,
51-
@NonNull final TransferMethodConfigurationRepository transferMethodConfigurationRepository,
52-
@NonNull final UserRepository userRepository) {
51+
@NonNull final TransferMethodConfigurationRepository transferMethodConfigurationRepository,
52+
@NonNull final UserRepository userRepository) {
5353
this.mView = view;
5454
this.mTransferMethodConfigurationRepository = transferMethodConfigurationRepository;
5555
this.mUserRepository = userRepository;
5656
}
5757

5858
@Override
5959
public void loadTransferMethodConfigurationKeys(final boolean forceUpdate, @Nullable final String countryCode,
60-
@Nullable final String currencyCode) {
60+
@Nullable final String currencyCode) {
6161

6262
mView.showProgressBar();
6363

@@ -150,11 +150,27 @@ public void onKeysLoaded(@Nullable final HyperwalletTransferMethodConfigurationK
150150
new ArrayList<>(key.getCurrencies(countryCode)) :
151151
new ArrayList<Currency>();
152152

153+
Country selectedCountry = null;
154+
for (Country country : key.getCountries()) {
155+
if (country.getCode().equals(countryCode)) {
156+
selectedCountry = country;
157+
break;
158+
}
159+
}
160+
// Attempt to get the default currency code for the selected country
161+
String selectedCurrencyCode = getDefaultCurrencyCode(selectedCountry, key, countryCode);
162+
163+
if (selectedCurrencyCode == null) {
164+
// If no default currency code is found, use the first currency in the list
165+
selectedCurrencyCode = currencies.get(0).getCode();
166+
}
167+
168+
// Show the selected country and currency in the UI
153169
mView.showTransferMethodCountry(countryCode);
154-
mView.showTransferMethodCurrency(currencies.get(0).getCode());
170+
mView.showTransferMethodCurrency(selectedCurrencyCode);
155171

156-
loadFeeAndProcessingTimeAndShowTransferMethods(countryCode, currencies.get(0).getCode(),
157-
user);
172+
// Load fee, processing time, and other transfer methods
173+
loadFeeAndProcessingTimeAndShowTransferMethods(countryCode, selectedCurrencyCode, user);
158174
}
159175

160176
@Override
@@ -180,7 +196,7 @@ private void showErrorLoadCurrency(@NonNull Errors errors) {
180196

181197
@Override
182198
public void loadTransferMethodTypes(final boolean forceUpdate,
183-
@NonNull final String countryCode, @NonNull final String currencyCode) {
199+
@NonNull final String countryCode, @NonNull final String currencyCode) {
184200
mView.showProgressBar();
185201

186202
if (forceUpdate) {
@@ -226,7 +242,7 @@ public void onError(@NonNull Errors errors) {
226242

227243
@Override
228244
public void openAddTransferMethod(@NonNull final String country, @NonNull final String currency,
229-
@NonNull final String transferMethodType, @NonNull final String profileType) {
245+
@NonNull final String transferMethodType, @NonNull final String profileType) {
230246
mView.showAddTransferMethod(country, currency, transferMethodType, profileType);
231247
}
232248

@@ -268,14 +284,28 @@ public void onKeysLoaded(@Nullable HyperwalletTransferMethodConfigurationKey key
268284
if (!mView.isActive()) {
269285
return;
270286
}
271-
272287
Set<Currency> currencyCodes = key.getCurrencies(countryCode) != null ?
273288
key.getCurrencies(countryCode) : new HashSet<Currency>();
289+
// Find the country based on the country code
290+
Country selectedCountry = null;
291+
for (Country country : key.getCountries()) {
292+
if (country.getCode().equals(countryCode)) {
293+
selectedCountry = country;
294+
break;
295+
}
296+
}
297+
// Attempt to get the default currency code for the selected country
298+
String selectedDefaultCurrencyCode = getDefaultCurrencyCode(selectedCountry, key, countryCode);
299+
300+
if (selectedDefaultCurrencyCode == null) {
301+
// If no default currency code is found, use the first currency in the list
302+
selectedDefaultCurrencyCode = currencyCodes.iterator().next().getCode(); // Get the first currency code
303+
}
274304

275305
TreeMap<String, String> currencyNameCodeMap = new TreeMap<>();
276-
String selectedCurrencyName = "";
306+
String selectedCurrencyName = null;
277307
for (Currency currency : currencyCodes) {
278-
if (currency.getCode().equals(currencyCode)) {
308+
if (currency.getCode().equals(selectedDefaultCurrencyCode)) {
279309
selectedCurrencyName = currency.getName();
280310
}
281311
currencyNameCodeMap.put(currency.getName(), currency.getCode());
@@ -293,6 +323,28 @@ public void onError(@NonNull final Errors errors) {
293323
});
294324
}
295325

326+
// Helper method to get the DefaultCurrencyCode
327+
private String getDefaultCurrencyCode(@NonNull Country country, @Nullable HyperwalletTransferMethodConfigurationKey keys,
328+
@NonNull String countryCode) {
329+
//Get the default currency code from the selected country
330+
String defaultCurrencyCode = country.getDefaultCurrency();
331+
// If the country has a default currency code, check if it's in the list of available currencies
332+
if (defaultCurrencyCode != null) {
333+
Set<Currency> currencies = keys.getCurrencies(countryCode);
334+
for (Currency currency : currencies) {
335+
if (currency.getCode().equals(defaultCurrencyCode)) {
336+
return currency.getCode(); // Return the country's currency code, if it exists in the list
337+
}
338+
}
339+
}
340+
// If the default currency is not found, return the first currency from the list (if available)
341+
if (!keys.getCurrencies(countryCode).isEmpty()) {
342+
return keys.getCurrencies(countryCode).iterator().next().getCode(); // Return the first currency code in the list
343+
}
344+
return null;
345+
}
346+
347+
296348
private List<TransferMethodSelectionItem> getTransferMethodSelectionItems(
297349
@NonNull final String countryCode, @NonNull final String currencyCode,
298350
@NonNull final String userProfileType,
@@ -309,7 +361,7 @@ private List<TransferMethodSelectionItem> getTransferMethodSelectionItems(
309361
}
310362

311363
private void loadFeeAndProcessingTimeAndShowTransferMethods(final String countryCode, final String currencyCode,
312-
final User user) {
364+
final User user) {
313365
mTransferMethodConfigurationRepository.getTransferMethodTypesFeeAndProcessingTime(countryCode, currencyCode,
314366
new TransferMethodConfigurationRepository.LoadKeysCallback() {
315367
@Override

0 commit comments

Comments
 (0)