xcarpentier / react-native-country-picker-modal

🇦🇶 Country picker provides a modal allowing a user to select a country from a list. It display a flag next to each country name.
https://reactnative.gallery/xcarpentier/country-picker
MIT License
1.08k stars 802 forks source link

world country data #22

Closed pcarion closed 8 years ago

pcarion commented 8 years ago

Let me expose here a concern I have with the memory usage of the CountryPicker component and I am OK to tackle this issue and submit a PR if you think it is a valid concern.

Currently, you load the countries.json file from the world_country npm module, to get extra data from the selected country.

This file is big (444K) and it may be a waste of space considering that very few piece of information is needed from the file:

You already have a configuration file of yours (countryFlags.js) to store the image of the flags.

My suggestion would be to parse (offline) the countries.json and generate a local countryInfo.js file, that would be stored in your repo (with proper attribution to the author of world_country) with only the information needed in the context of your app.

This would require a few changes in the code (such as orderCountryList and getCountry) but nothing major and the footprint of the component in memory would be much smaller...

sprmn commented 8 years ago

I agree, the country data file is huge and a local copy of the file containing only the necessary data would be a big improvement. Maybe we could add some functionality to provide your own data as well. Not everyone needs the currency or all translations for example. Some might not even need all countries (Antartica for example doesn't even have a calling code if I'm not mistaken).

pcarion commented 8 years ago

I started to work on such a converter... I am generating a file like this:

'use strict';
module.exports = {
  "AD": {
    "flagImage": "data:image/png;base64,iVBORw <from countries.json> Jggg==",
    "currency": "EUR",
    "callingCode": "376",
    "name": {
      "cym": "Andorra",
      "deu": "Andorra",
      "fra": "Andorre",
      "hrv": "Andora",
      "ita": "Andorra",
      "jpn": "アンドラ",
      "nld": "Andorra",
      "por": "Andorra",
      "rus": "Андорра",
      "spa": "Andorra",
      "fin": "Andorra"
    }
  },

Trying to see how I could escape the names to be more robust.

I also think that a country code of 2 characters instead of 3 for the translations would be more useful...

arthurvi commented 8 years ago

Good point. Speed and efficiency is very important for a basic component like a country picker. I think we should default include the bare minimum but make extra features possible. Can we use the unicode flags instead of the large base64 encoded images?

+1 for a 2 char country code for translation.

sprmn commented 8 years ago

Unicode flags would be a nice improvement. However, on Android, unicode flags are only supported since Lollipop. Half of the Android users still use older versions, hence I don't think unicode flags are a solid solution at the moment.

arthurvi commented 8 years ago

Ah ok, but we can then use Platform.version to discover this and include base64 strings as legacy option?

xcarpentier commented 8 years ago

Hello @pcarion, thank you and I agree ! Do you have a js script file to make the new countries.js file ? If so, could you make a PR with your script ?

xcarpentier commented 8 years ago

See my PR #29 :)

xcarpentier commented 8 years ago

Improve here #29