diff --git a/.gitignore b/.gitignore
index 4396a95..426a199 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,14 @@
*.iml
.gradle
/local.properties
-/.idea
-/release
-/keystores
+/.idea/caches
+/.idea/libraries
+/.idea/modules.xml
+/.idea/workspace.xml
+/.idea/navEditor.xml
+/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
-.cxx
+.cxx
\ No newline at end of file
diff --git a/.gradle/5.4.1/executionHistory/executionHistory.bin b/.gradle/5.4.1/executionHistory/executionHistory.bin
index 140e5f0..54fac42 100644
Binary files a/.gradle/5.4.1/executionHistory/executionHistory.bin and b/.gradle/5.4.1/executionHistory/executionHistory.bin differ
diff --git a/.gradle/5.4.1/executionHistory/executionHistory.lock b/.gradle/5.4.1/executionHistory/executionHistory.lock
index 43b1436..4f5d670 100644
Binary files a/.gradle/5.4.1/executionHistory/executionHistory.lock and b/.gradle/5.4.1/executionHistory/executionHistory.lock differ
diff --git a/.gradle/5.4.1/fileHashes/fileHashes.bin b/.gradle/5.4.1/fileHashes/fileHashes.bin
index d6cd782..a1d3c8d 100644
Binary files a/.gradle/5.4.1/fileHashes/fileHashes.bin and b/.gradle/5.4.1/fileHashes/fileHashes.bin differ
diff --git a/.gradle/5.4.1/fileHashes/fileHashes.lock b/.gradle/5.4.1/fileHashes/fileHashes.lock
index d32a90a..8133e83 100644
Binary files a/.gradle/5.4.1/fileHashes/fileHashes.lock and b/.gradle/5.4.1/fileHashes/fileHashes.lock differ
diff --git a/.gradle/5.4.1/fileHashes/resourceHashesCache.bin b/.gradle/5.4.1/fileHashes/resourceHashesCache.bin
index ee281d8..0348b0d 100644
Binary files a/.gradle/5.4.1/fileHashes/resourceHashesCache.bin and b/.gradle/5.4.1/fileHashes/resourceHashesCache.bin differ
diff --git a/.gradle/5.4.1/javaCompile/classAnalysis.bin b/.gradle/5.4.1/javaCompile/classAnalysis.bin
index 862dad8..058bfe4 100644
Binary files a/.gradle/5.4.1/javaCompile/classAnalysis.bin and b/.gradle/5.4.1/javaCompile/classAnalysis.bin differ
diff --git a/.gradle/5.4.1/javaCompile/jarAnalysis.bin b/.gradle/5.4.1/javaCompile/jarAnalysis.bin
index 13e32db..8f9f942 100644
Binary files a/.gradle/5.4.1/javaCompile/jarAnalysis.bin and b/.gradle/5.4.1/javaCompile/jarAnalysis.bin differ
diff --git a/.gradle/5.4.1/javaCompile/javaCompile.lock b/.gradle/5.4.1/javaCompile/javaCompile.lock
index 411277c..efd73ce 100644
Binary files a/.gradle/5.4.1/javaCompile/javaCompile.lock and b/.gradle/5.4.1/javaCompile/javaCompile.lock differ
diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock
index 719947a..b026df5 100644
Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4f9d78d..f207a0a 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -9,7 +9,22 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -34,55 +49,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
@@ -90,8 +61,8 @@
-
-
+
+
@@ -100,28 +71,22 @@
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
@@ -142,12 +107,10 @@
- English
countryc
50dp
dp_50
40dp
- loadCountryDataFromXML
ai
as
bs
@@ -172,6 +135,8 @@
vi
mCountryPicked
mLanguageCode
+ mCountries
+ loadCountryDataFromXML
@@ -206,15 +171,16 @@
-
-
-
-
+
+
+
+
+
@@ -225,33 +191,11 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -293,7 +237,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -419,6 +385,7 @@
+
@@ -434,7 +401,7 @@
-
+
@@ -449,18 +416,6 @@
-
-
-
-
- file://$PROJECT_DIR$/app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt
- 18
-
-
-
-
-
-
@@ -657,71 +612,71 @@
-
+
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
-
-
-
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
-
+
-
-
+
+
-
-
-
diff --git a/app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt b/app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt
index 4b48547..fe3048c 100644
--- a/app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt
+++ b/app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt
@@ -4,11 +4,13 @@ import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.ccc.ccp.Country
+import com.ccc.ccp.CountryCodePicker
import com.ccc.ccp.OnCountryPickedListener
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
-class MainActivity : AppCompatActivity(), OnCountryPickedListener {
+class MainActivity : AppCompatActivity(), OnCountryPickedListener,
+ CountryCodePicker.OnRefreshDataCompleteListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -16,6 +18,10 @@ class MainActivity : AppCompatActivity(), OnCountryPickedListener {
countryCodePicker.setLanguageCode(Locale.getDefault().language)
countryCodePicker.refreshDefaultData()
+ countryCodePicker.setOnRefreshDataCompleteListener(this)
+ }
+
+ override fun onRefreshDataComplete() {
countryCodePicker.setCountryPicked(countryCodePicker.getCountry(Locale.getDefault().country))
}
diff --git a/ccp/src/main/java/com/ccc/ccp/CountryCodePicker.kt b/ccp/src/main/java/com/ccc/ccp/CountryCodePicker.kt
index dd4c267..510a478 100644
--- a/ccp/src/main/java/com/ccc/ccp/CountryCodePicker.kt
+++ b/ccp/src/main/java/com/ccc/ccp/CountryCodePicker.kt
@@ -3,6 +3,7 @@ package com.ccc.ccp
import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
+import android.util.Log
import android.view.Gravity
import android.view.ViewGroup
import android.widget.ImageView
@@ -10,6 +11,10 @@ import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.ccc.ccp.utils.DimensionUtils
+import io.reactivex.Single
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.observers.DisposableSingleObserver
+import io.reactivex.schedulers.Schedulers
import java.util.*
class CountryCodePicker : LinearLayout {
@@ -27,6 +32,8 @@ class CountryCodePicker : LinearLayout {
private var mFlagWidth: Float = DimensionUtils.getDimensionWithDensity(context, R.dimen.dp_24)
private var mCountries = ArrayList()
private var mCountryPicked: Country? = null
+ private var mOnLoadDataComplete: OnLoadDataCompleteListener? = null
+ private var mOnRefreshDataComplete: OnRefreshDataCompleteListener? = null
constructor(context: Context) : super(context)
@@ -68,13 +75,36 @@ class CountryCodePicker : LinearLayout {
context, attrs, defStyleAttr
)
+ fun setOnLoadDataCompleteListener(onLoadDataCompleteListener: OnLoadDataCompleteListener?) {
+ mOnLoadDataComplete = onLoadDataCompleteListener
+ }
+
+ fun setOnRefreshDataCompleteListener(onRefreshDataCompleteListener: OnRefreshDataCompleteListener?) {
+ mOnRefreshDataComplete = onRefreshDataCompleteListener
+ }
+
fun setLanguageCode(languageCode: String) {
mLanguageCode = languageCode
}
fun refreshDefaultData() {
mCountries.clear()
- mCountries.addAll(Country.loadCountryDataFromXML(context, mLanguageCode))
+ Single.create> {
+ it.onSuccess(
+ Country.loadCountryDataFromXML(context, mLanguageCode)
+ )
+ }.subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(object : DisposableSingleObserver>() {
+ override fun onSuccess(countries: List) {
+ mCountries.addAll(countries)
+ mOnRefreshDataComplete?.onRefreshDataComplete()
+ }
+
+ override fun onError(e: Throwable) {
+ Log.e(this::class.java.simpleName, "error", e)
+ }
+ })
}
fun updateData(countries: List) {
@@ -84,9 +114,6 @@ class CountryCodePicker : LinearLayout {
// Default country is Viet Nam
fun getDefaultCountryPicked(): Country {
- if (mCountries.isEmpty()) {
- mCountries.addAll(Country.loadCountryDataFromXML(context, mLanguageCode))
- }
mCountries.find { it.nameCode == "vn" }?.let {
return it
}
@@ -97,10 +124,8 @@ class CountryCodePicker : LinearLayout {
}
fun getCountry(nameCode: String): Country {
- if (mCountries.isEmpty()) {
- mCountries.addAll(Country.loadCountryDataFromXML(context, mLanguageCode))
- }
- return mCountries.find { it.nameCode == nameCode.toLowerCase(Locale.getDefault()) } ?: getDefaultCountryPicked()
+ return mCountries.find { it.nameCode == nameCode.toLowerCase(Locale.getDefault()) }
+ ?: getDefaultCountryPicked()
}
fun getCountryPicked(): Country? {
@@ -115,7 +140,22 @@ class CountryCodePicker : LinearLayout {
}
private fun initData() {
- mCountries.addAll(Country.loadCountryDataFromXML(context, mLanguageCode))
+ Single.create> {
+ it.onSuccess(
+ Country.loadCountryDataFromXML(context, mLanguageCode)
+ )
+ }.subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(object : DisposableSingleObserver>() {
+ override fun onSuccess(countries: List) {
+ mCountries.addAll(countries)
+ mOnLoadDataComplete?.onLoadDataComplete()
+ }
+
+ override fun onError(e: Throwable) {
+ Log.e(this::class.java.simpleName, "error", e)
+ }
+ })
}
private fun initViews() {
@@ -150,4 +190,12 @@ class CountryCodePicker : LinearLayout {
companion object {
private val TAG = this::class.java.simpleName
}
+
+ interface OnLoadDataCompleteListener {
+ fun onLoadDataComplete()
+ }
+
+ interface OnRefreshDataCompleteListener {
+ fun onRefreshDataComplete()
+ }
}
\ No newline at end of file
diff --git a/ccp/src/main/java/com/ccc/ccp/CountryCodePickerFragment.kt b/ccp/src/main/java/com/ccc/ccp/CountryCodePickerFragment.kt
index 46eda61..13d2f44 100644
--- a/ccp/src/main/java/com/ccc/ccp/CountryCodePickerFragment.kt
+++ b/ccp/src/main/java/com/ccc/ccp/CountryCodePickerFragment.kt
@@ -61,8 +61,8 @@ class CountryCodePickerFragment : DialogFragment(), CountryCodeAdapter.OnItemCli
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver>() {
- override fun onSuccess(data: List) {
- mAdapter.updateData(data)
+ override fun onSuccess(countries: List) {
+ mAdapter.updateData(countries)
}
override fun onError(e: Throwable) {