From c1a05a86c1845fef14e5338644efa991c2829263 Mon Sep 17 00:00:00 2001 From: Anoop M Date: Sat, 25 Sep 2021 12:32:21 +0200 Subject: [PATCH] Android V2 Embedding migration --- .idea/misc.xml | 3 + .idea/workspace.xml | 44 +++++- .../iso_countries/IsoCountriesPlugin.kt | 139 ++++++++++-------- example/pubspec.lock | 12 +- 4 files changed, 126 insertions(+), 72 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 5c94cb2..c333ca1 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 82a407b..337fb97 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,14 @@ - - - + + + + + + + + @@ -63,6 +69,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -73,7 +109,7 @@ file://$PROJECT_DIR$/android/src/main/kotlin/com/anoop4real/iso_countries/IsoCountriesPlugin.kt - 79 + 94 diff --git a/android/src/main/kotlin/com/anoop4real/iso_countries/IsoCountriesPlugin.kt b/android/src/main/kotlin/com/anoop4real/iso_countries/IsoCountriesPlugin.kt index 12eb23a..a7fd7f3 100644 --- a/android/src/main/kotlin/com/anoop4real/iso_countries/IsoCountriesPlugin.kt +++ b/android/src/main/kotlin/com/anoop4real/iso_countries/IsoCountriesPlugin.kt @@ -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 @@ -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 + 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 + 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()) + } + } else { + result.notImplemented() + } } - else if (call.method == "getISOCountriesForLocale"){ - // TODO: Implement in a better way - val args = call.arguments as? HashMap - 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 - 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()) - } + + 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> { - var countriesList = arrayListOf>() - 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> { + var countriesList = arrayListOf>() + 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 { - if (code.isEmpty() || code.length > 2){ - return hashMapOf() - } - val locale = Locale(localeIdentifier,code) - val countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifier)) - if (countryName == null) { - return hashMapOf() - } - val simpleCountry = hashMapOf("name" to countryName, "countryCode" to code) - return simpleCountry + // Get a country name from code + fun getCountryForCountryCode(code: String, localeIdentifier: String = ""): HashMap { + if (code.isEmpty() || code.length > 2) { + return hashMapOf() + } + val locale = Locale(localeIdentifier, code) + val countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifier)) + if (countryName == null) { + return hashMapOf() + } + val simpleCountry = hashMapOf("name" to countryName, "countryCode" to code) + return simpleCountry + } } - } } diff --git a/example/pubspec.lock b/example/pubspec.lock index dfb6134..62afcdf 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -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: @@ -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: @@ -73,7 +73,7 @@ packages: path: ".." relative: true source: path - version: "2.0.0" + version: "2.0.1" matcher: dependency: transitive description: @@ -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: @@ -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: @@ -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: