Skip to content

Commit

Permalink
Merge branch 'anoop4real:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Engel authored Oct 20, 2021
2 parents 5a0d921 + 6d93694 commit 27a9db6
Show file tree
Hide file tree
Showing 21 changed files with 578 additions and 664 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

326 changes: 68 additions & 258 deletions .idea/workspace.xml

Large diffs are not rendered by default.

40 changes: 39 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,42 @@ Minor changes

## 1.0.3
___
Fix imports
Fix imports

## 1.1.0
___
- Optimizations
- Added API to fetch a country by passing in a country code and optional languageKey.

## 1.1.1
___
Updated the readme

## 2.0.0
___
Upgrade SDK to >=2.12.0
Updated the readme
Updated the examples
Migrate to Android integration 2.0

## 2.0.1
___
Upgrade SDK to >=2.12.0 ,

Updated the readme ,

Updated the examples ,

Migrate to Android integration 2.0 ,

Null Safety.

## 2.1.0
___
Updated the readme ,

Migrate to Android integration V2 Embedding,

Bug Fixes,

Added Country comparator
72 changes: 57 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ Android: API level 21 required

![iso_countries](https://user-images.githubusercontent.com/6782228/69907084-a58ea080-13ce-11ea-957b-9068a49a6e19.gif)


#### Update

Version 2.1.0 Android Embedding V2

#### Update

Version 2.0.0 updated to support null safety

## Usage
For detailed use, see the example.
### Fetch Default (English)

```
List<Country> countries;
List<Country>? countries;
try {
countries = await IsoCountries.iso_countries;
} on PlatformException {
Expand All @@ -28,13 +37,13 @@ For detailed use, see the example.
### Fetch based on Language
```
List<Country> countries;
List<Country>? countries;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
// If you need country names in a specific language please pass language code sample
// fr-fr, en-en, de-de... IMPORTANT: In Android there seem to be some issue with case
// so passing fr-FR wont work
countries = await IsoCountries.iso_countries_for_locale("fr-fr");
countries = await IsoCountries.iso_countries_for_locale('fr-fr');
} on PlatformException {
countries = null;
}
Expand All @@ -45,7 +54,7 @@ For detailed use, see the example.
```
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> prepareDefaultCountries() async {
List<Country> countries;
List<Country>? countries;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
countries = await IsoCountries.iso_countries;
Expand All @@ -55,34 +64,67 @@ For detailed use, see the example.
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
countryList = countries;
});
}
if (!mounted) {
return;
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> prepareLocaleSpecificCountries() async {
List<Country> countries;
List<Country>? countries;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
// If you need country names in a specific language please pass language code sample
// fr-fr, en-en, de-de... IMPORTANT: In Android there seem to be some issue with case
// so passing fr-FR wont work
countries = await IsoCountries.iso_countries_for_locale("fr-fr");
countries = await IsoCountries.iso_countries_for_locale('fr-fr');
} on PlatformException {
countries = null;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
if (!mounted) {
return;
}
setState(() {
countryList = countries;
if (countries != null) {
countryList = countries;
}
});
}
```

#### New API Added

Passing in a country code( 2 letter) and an optional localeIdentifier( eg 'de-de', 'fr-fr'), you can get a country object with a translated name.

```
// Platform messages are asynchronous, so we initialize in an async method.
// IMPORTANT: Make sure the country code passed in is valid, in Android passing
// in a wrong country code, returns the country name as passed in country code not sure why.
Future<void> getCountryForCodeWithIdentifier(
String code, String localeIdentifier) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
country = await IsoCountries.iso_country_for_code_for_locale(code,
locale_identifier: localeIdentifier);
} on PlatformException {
country = null;
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) {
return;
}
setState(() {
print(country?.name);
});
}
```

### Note

If you are getting any error related to pods while running iOS example, then please delete the podfile and podfile.lock and re-run.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.anoop4real.iso_countries

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
Expand All @@ -9,59 +11,94 @@ import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

class IsoCountriesPlugin: MethodCallHandler {
companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.anoop4real.iso_countries")
channel.setMethodCallHandler(IsoCountriesPlugin())
class IsoCountriesPlugin : MethodCallHandler, FlutterPlugin {

private var channel: MethodChannel? = null

companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "com.anoop4real.iso_countries")
channel.setMethodCallHandler(IsoCountriesPlugin())
}
}
}

override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else if (call.method == "getISOCountries"){
result.success(CountryDataStore.getIsoCountries())
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else if (call.method == "getISOCountries") {
result.success(CountryDataStore.getIsoCountries())
} else if (call.method == "getISOCountriesForLocale") {
// TODO: Implement in a better way
val args = call.arguments as? HashMap<String, String>
if (args != null) {
val identifier = args.getOrElse("locale_identifier") { "en_US" }
result.success(CountryDataStore.getIsoCountries(identifier))
} else {
result.success(CountryDataStore.getIsoCountries())
}
} else if (call.method == "getCountryForCountryCodeWithLocaleIdentifier") {
val args = call.arguments as? HashMap<String, String>
if (args != null) {
val identifier = args.getOrElse("locale_identifier") { "" }
val code = args.getOrElse("countryCode") { "" }
result.success(CountryDataStore.getCountryForCountryCode(code, identifier))
} else {
// Return an empty hashmap if arguments are missing
result.success(hashMapOf<String, String>())
}
} else {
result.notImplemented()
}
}
else if (call.method == "getISOCountriesForLocale"){
// TODO: Implement in a better way
val args = call.arguments as? HashMap<String,String>
if (args !=null){
val identifier = args.getOrElse("locale_identifier"){"en_US"}
result.success(CountryDataStore.getIsoCountries(identifier))
}else{
result.success(CountryDataStore.getIsoCountries())
}

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
registerWith(binding.binaryMessenger)
}
else {
result.notImplemented()

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
}

private fun registerWith(messenger: BinaryMessenger) {
channel = MethodChannel(messenger, "com.anoop4real.iso_countries")
channel?.setMethodCallHandler(IsoCountriesPlugin())
}
}
}

class CountryDataStore private constructor() {

companion object{

var countriesList = arrayListOf<HashMap<String, String>>()
companion object {

fun getIsoCountries(localeIdentifer: String = "" ) : ArrayList<HashMap<String, String>> {
countriesList.clear()
for (countryCode in Locale.getISOCountries()) {
val locale = Locale(localeIdentifer,countryCode)
var countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifer))
if (countryName == null) {
countryName = "UnIdentified"
fun getIsoCountries(localeIdentifier: String = "en-US"): ArrayList<HashMap<String, String>> {
var countriesList = arrayListOf<HashMap<String, String>>()
for (countryCode in Locale.getISOCountries()) {
// If no locale is passed, then use "en_US"
val locale = Locale(localeIdentifier, countryCode)
var countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifier))
if (countryName == null) {
countryName = "UnIdentified"
}
val simpleCountry = hashMapOf("name" to countryName, "countryCode" to countryCode)
countriesList.add(simpleCountry)
}
countriesList = ArrayList(countriesList.sortedWith(compareBy { it["name"] }))
return countriesList
}
val simpleCountry = hashMapOf("name" to countryName, "countryCode" to countryCode)
countriesList.add(simpleCountry)
}
countriesList = ArrayList(countriesList.sortedWith(compareBy { it["name"] }))

return countriesList
// Get a country name from code
fun getCountryForCountryCode(code: String, localeIdentifier: String = ""): HashMap<String, String> {
if (code.isEmpty() || code.length > 2) {
return hashMapOf<String, String>()
}
val locale = Locale(localeIdentifier, code)
val countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifier))
if (countryName == null) {
return hashMapOf<String, String>()
}
val simpleCountry = hashMapOf("name" to countryName, "countryCode" to code)
return simpleCountry
}
}

}
}

Loading

0 comments on commit 27a9db6

Please sign in to comment.