Skip to content

Commit

Permalink
Helper added
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrounger committed Oct 8, 2017
1 parent 3d8b2cc commit 5d65d0c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Step 2\. Add the dependency

```java
dependencies {
compile 'com.github.scrounger:CountryCurrencyPicker:1.0.2'
compile 'com.github.scrounger:CountryCurrencyPicker:1.0.3'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -77,6 +76,16 @@ public Currency getCurrency() {
public void setCurrency(Currency currency) {
this.currency = currency;
}

private Locale locale;

public Locale getLocale() {
if (locale == null) {
return new Locale("", code);
}

return locale;
}
//endregion

//region Constructor
Expand All @@ -93,7 +102,7 @@ public static Country getCountry(String countryCode, Context context) {

return new Country(countryCode,
locale.getDisplayName(),
getFlagDrawableId(countryCode, context));
Helper.getFlagDrawableId(countryCode, context));
}

@Nullable
Expand Down Expand Up @@ -166,23 +175,10 @@ private static void sortList(ArrayList<Country> list) {
Collections.sort(list, new Comparator<Country>() {
@Override
public int compare(Country ccItem, Country ccItem2) {
return removeAccents(ccItem.getName()).toLowerCase().compareTo(removeAccents(ccItem2.getName()).toLowerCase());
return Helper.removeAccents(ccItem.getName()).toLowerCase().compareTo(Helper.removeAccents(ccItem2.getName()).toLowerCase());
}
});
}

@NonNull
@DrawableRes
private static Integer getFlagDrawableId(String countryCode, Context context) {
String drawableName = "flag_" + countryCode.toLowerCase();
return context.getResources()
.getIdentifier(drawableName, "drawable", context.getPackageName());
}

private static String removeAccents(String str) {
return Normalizer.normalize(str, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
//endregion

//region Parcelable
Expand All @@ -197,16 +193,18 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
dest.writeValue(this.flagId);
dest.writeParcelable(this.currency, flags);
dest.writeSerializable(this.locale);
}

private Country(Parcel in) {
this.code = in.readString();
this.name = in.readString();
this.flagId = (Integer) in.readValue(Integer.class.getClassLoader());
this.currency = in.readParcelable(Currency.class.getClassLoader());
this.locale = (Locale) in.readSerializable();
}

public static final Parcelable.Creator<Country> CREATOR = new Parcelable.Creator<Country>() {
public static final Creator<Country> CREATOR = new Creator<Country>() {
@Override
public Country createFromParcel(Parcel source) {
return new Country(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;

import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand Down Expand Up @@ -94,7 +93,6 @@ public ArrayList<String> getCountriesNames() {
public void setCountriesNames(ArrayList<String> countriesNames) {
this.countriesNames = countriesNames;
}

//endregion

//region Constructor
Expand Down Expand Up @@ -125,7 +123,7 @@ public static Currency getCurrency(String countryCode, Context context) {
currency.getCurrencyCode(),
currency.getDisplayName(),
currency.getSymbol(),
getFlagDrawableId(currency.getCurrencyCode(), context));
Helper.getFlagDrawableId(currency.getCurrencyCode(), context));
}
return null;
}
Expand All @@ -135,7 +133,7 @@ public static Currency getCurrency(java.util.Currency currency, Context context)
currency.getCurrencyCode(),
currency.getDisplayName(),
currency.getSymbol(),
getFlagDrawableId(currency.getCurrencyCode(), context));
Helper.getFlagDrawableId(currency.getCurrencyCode(), context));
}

private static ArrayList<Country> tmpCountries;
Expand Down Expand Up @@ -228,23 +226,12 @@ private static void sortList(ArrayList<Currency> list) {
Collections.sort(list, new Comparator<Currency>() {
@Override
public int compare(Currency ccItem, Currency ccItem2) {
return removeAccents(ccItem.getName()).toLowerCase().compareTo(removeAccents(ccItem2.getName()).toLowerCase());
return Helper.removeAccents(ccItem.getName()).toLowerCase().compareTo(Helper.removeAccents(ccItem2.getName()).toLowerCase());
}
});
}

@NonNull
@DrawableRes
private static Integer getFlagDrawableId(String currencyCode, Context context) {
String drawableName = "flag_" + currencyCode.toLowerCase();
return context.getResources()
.getIdentifier(drawableName, "drawable", context.getPackageName());
}

private static String removeAccents(String str) {
return Normalizer.normalize(str, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
//endregion

//region Parcelable
Expand All @@ -260,14 +247,16 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.symbol);
dest.writeValue(this.flagId);
dest.writeTypedList(this.countries);
dest.writeStringList(this.countriesNames);
}

protected Currency(Parcel in) {
private Currency(Parcel in) {
this.code = in.readString();
this.name = in.readString();
this.symbol = in.readString();
this.flagId = (Integer) in.readValue(Integer.class.getClassLoader());
this.countries = in.createTypedArrayList(Country.CREATOR);
this.countriesNames = in.createStringArrayList();
}

public static final Creator<Currency> CREATOR = new Creator<Currency>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2017 Scrounger
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scrounger.countrycurrencypicker.library;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;

import java.text.Normalizer;

public class Helper {

@NonNull
@DrawableRes
public static Integer getFlagDrawableId(String code, Context context) {
String drawableName = "flag_" + code.toLowerCase();
return context.getResources()
.getIdentifier(drawableName, "drawable", context.getPackageName());
}

public static String removeAccents(String str) {
return Normalizer.normalize(str, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}

}

0 comments on commit 5d65d0c

Please sign in to comment.