Skip to content

Commit

Permalink
Android V2 Embedding migration
Browse files Browse the repository at this point in the history
  • Loading branch information
anoop4real committed Sep 25, 2021
1 parent 097b317 commit c1a05a8
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 72 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.

44 changes: 40 additions & 4 deletions .idea/workspace.xml

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

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,81 +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 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>())
}

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

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

class CountryDataStore private constructor() {

companion object{
companion object {

fun getIsoCountries(localeIdentifier: String = "" ) : 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"
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
// 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
}
}
}
}

12 changes: 6 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.8.1"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,7 +28,7 @@ packages:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -73,7 +73,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.0.0"
version: "2.0.1"
matcher:
dependency: transitive
description:
Expand All @@ -87,7 +87,7 @@ packages:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand All @@ -106,7 +106,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -141,7 +141,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.4.2"
typed_data:
dependency: transitive
description:
Expand Down

0 comments on commit c1a05a8

Please sign in to comment.