Skip to content

Commit

Permalink
Update with new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
anoop4real committed May 16, 2020
1 parent ecb343e commit 0c43473
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 350 deletions.
287 changes: 42 additions & 245 deletions .idea/workspace.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class IsoCountriesPlugin: MethodCallHandler {
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()
}
Expand All @@ -44,24 +55,46 @@ class CountryDataStore private constructor() {

companion object{

var countriesList = arrayListOf<HashMap<String, String>>()

fun getIsoCountries(localeIdentifer: String = "" ) : ArrayList<HashMap<String, String>> {
countriesList.clear()
fun getIsoCountries(localeIdentifier: String = "" ) : ArrayList<HashMap<String, String>> {
var countriesList = arrayListOf<HashMap<String, String>>()
for (countryCode in Locale.getISOCountries()) {
val locale = Locale(localeIdentifer,countryCode)
var countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeIdentifer))
lateinit var locale: Locale
// If no locale is passed, then take the locale from Current
if (localeIdentifier.isEmpty()) {
locale = Locale.getDefault()
} else {
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
}

// Get a country name from code
fun getCountryForCountryCode(code: String, localeIdentifier: String = "" ): HashMap<String, String> {
if (code.isEmpty()){
return hashMapOf<String, String>()
}
lateinit var locale: Locale
// If no locale is passed, then take the locale from Current
if (localeIdentifier.isEmpty()) {
locale = Locale.getDefault()
} else {
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
}
}
}

84 changes: 50 additions & 34 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,67 @@ def parse_KV_file(file, separator='=')
if !File.exists? file_abs_path
return [];
end
pods_ary = []
generated_key_values = {}
skip_line_start_symbols = ["#", "/"]
File.foreach(file_abs_path) { |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
pods_ary.push({:name => podname, :path => podpath});
else
puts "Invalid plugin specification: #{line}"
end
}
return pods_ary
File.foreach(file_abs_path) do |line|
next if skip_line_start_symbols.any? { |symbol| line =~ /^\s*#{symbol}/ }
plugin = line.split(pattern=separator)
if plugin.length == 2
podname = plugin[0].strip()
path = plugin[1].strip()
podpath = File.expand_path("#{path}", file_abs_path)
generated_key_values[podname] = podpath
else
puts "Invalid plugin specification: #{line}"
end
end
generated_key_values
end

target 'Runner' do
use_frameworks!
use_modular_headers!

# Flutter Pod

# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')
copied_flutter_dir = File.join(__dir__, 'Flutter')
copied_framework_path = File.join(copied_flutter_dir, 'Flutter.framework')
copied_podspec_path = File.join(copied_flutter_dir, 'Flutter.podspec')
unless File.exist?(copied_framework_path) && File.exist?(copied_podspec_path)
# Copy Flutter.framework and Flutter.podspec to Flutter/ to have something to link against if the xcode backend script has not run yet.
# That script will copy the correct debug/profile/release version of the framework based on the currently selected Xcode configuration.
# CocoaPods will not embed the framework on pod install (before any build phases can generate) if the dylib does not exist.

# Flutter Pods
generated_xcode_build_settings = parse_KV_file('./Flutter/Generated.xcconfig')
if generated_xcode_build_settings.empty?
puts "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first."
end
generated_xcode_build_settings.map { |p|
if p[:name] == 'FLUTTER_FRAMEWORK_DIR'
symlink = File.join('.symlinks', 'flutter')
File.symlink(File.dirname(p[:path]), symlink)
pod 'Flutter', :path => File.join(symlink, File.basename(p[:path]))
generated_xcode_build_settings_path = File.join(copied_flutter_dir, 'Generated.xcconfig')
unless File.exist?(generated_xcode_build_settings_path)
raise "Generated.xcconfig must exist. If you're running pod install manually, make sure flutter pub get is executed first"
end
generated_xcode_build_settings = parse_KV_file(generated_xcode_build_settings_path)
cached_framework_dir = generated_xcode_build_settings['FLUTTER_FRAMEWORK_DIR'];

unless File.exist?(copied_framework_path)
FileUtils.cp_r(File.join(cached_framework_dir, 'Flutter.framework'), copied_flutter_dir)
end
}
unless File.exist?(copied_podspec_path)
FileUtils.cp(File.join(cached_framework_dir, 'Flutter.podspec'), copied_flutter_dir)
end
end

# Keep pod path relative so it can be checked into Podfile.lock.
pod 'Flutter', :path => 'Flutter'

# Plugin Pods

# Prepare symlinks folder. We use symlinks to avoid having Podfile.lock
# referring to absolute paths on developers' machines.
system('rm -rf .symlinks')
system('mkdir -p .symlinks/plugins')
plugin_pods = parse_KV_file('../.flutter-plugins')
plugin_pods.map { |p|
symlink = File.join('.symlinks', 'plugins', p[:name])
File.symlink(p[:path], symlink)
pod p[:name], :path => File.join(symlink, 'ios')
}
plugin_pods.each do |name, path|
symlink = File.join('.symlinks', 'plugins', name)
File.symlink(path, symlink)
pod name, :path => File.join(symlink, 'ios')
end
end

# Prevent Cocoapods from embedding a second Flutter framework and causing an error with the new Xcode build system.
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ PODS:
- Flutter

DEPENDENCIES:
- Flutter (from `.symlinks/flutter/ios`)
- Flutter (from `Flutter`)
- iso_countries (from `.symlinks/plugins/iso_countries/ios`)

EXTERNAL SOURCES:
Flutter:
:path: ".symlinks/flutter/ios"
:path: Flutter
iso_countries:
:path: ".symlinks/plugins/iso_countries/ios"

SPEC CHECKSUMS:
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
iso_countries: eb09d40f388e4c65e291e0bb36a701dfe7de6c74

PODFILE CHECKSUM: b6a0a141693093b304368d08511b46cf3d1d0ac5
PODFILE CHECKSUM: 1b66dae606f75376c5f2135a8290850eeb09ae83

COCOAPODS: 1.8.4
85 changes: 55 additions & 30 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MyApp extends StatefulWidget {

class _MyAppState extends State<MyApp> {
List<Country> countryList;
Country country;
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -59,42 +60,66 @@ class _MyAppState extends State<MyApp> {
countryList = countries;
});
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> getCountryForCodeWithIdentifier(
String code, String localeIdentifier) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
country = await IsoCountries.iso_countries_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(() {
//this.country = country;
print(country.name);
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: prepareLocaleSpecificCountries,
child: Text("fr-fr"),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
FlatButton(
textColor: Colors.white,
onPressed: prepareDefaultCountries,
child: Text("Default"),
shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
),
],
title: const Text('Plugin example app'),
),
body: _buildListOfCountries()
),
appBar: AppBar(
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: prepareLocaleSpecificCountries,
child: Text("fr-fr"),
shape:
CircleBorder(side: BorderSide(color: Colors.transparent)),
),
FlatButton(
textColor: Colors.white,
onPressed: prepareDefaultCountries,
child: Text("Default"),
shape:
CircleBorder(side: BorderSide(color: Colors.transparent)),
),
],
title: const Text('Plugin example app'),
),
body: _buildListOfCountries()),
);
}

Widget _buildListOfCountries(){

return ListView.builder(itemBuilder: (BuildContext context, int index){
final Country country = countryList[index];
return ListTile(
title: Text(country.name),
subtitle: Text(country.countryCode),

);
},
itemCount: countryList != null ? countryList.length : 0,);
Widget _buildListOfCountries() {
return ListView.builder(
itemBuilder: (BuildContext context, int index) {
final Country country = countryList[index];
return ListTile(
title: Text(country.name),
subtitle: Text(country.countryCode),
onTap: () =>
getCountryForCodeWithIdentifier(country.countryCode, 'de-de'));
},
itemCount: countryList != null ? countryList.length : 0,
);
}
}
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: archive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.10"
version: "2.0.11"
args:
dependency: transitive
description:
Expand All @@ -21,7 +21,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
version: "2.4.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -87,21 +87,21 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "1.0.3"
matcher:
dependency: transitive
description:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.6"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.7"
version: "1.1.8"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -176,7 +176,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.5"
version: "0.2.11"
typed_data:
dependency: transitive
description:
Expand Down
Loading

0 comments on commit 0c43473

Please sign in to comment.