Skip to content

Commit

Permalink
Merge pull request #25 from onstonboy/develop
Browse files Browse the repository at this point in the history
1.0.9
  • Loading branch information
onstonboy authored Oct 9, 2019
2 parents 6cafb86 + bf54100 commit 5bd9ffb
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 154 deletions.
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
Binary file modified .gradle/5.4.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/5.4.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/classAnalysis.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/jarAnalysis.bin
Binary file not shown.
Binary file modified .gradle/5.4.1/javaCompile/javaCompile.lock
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
231 changes: 93 additions & 138 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion app/src/main/java/com/ccc/countrycodepicker/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ 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)
setContentView(R.layout.activity_main)

countryCodePicker.setLanguageCode(Locale.getDefault().language)
countryCodePicker.refreshDefaultData()
countryCodePicker.setOnRefreshDataCompleteListener(this)
}

override fun onRefreshDataComplete() {
countryCodePicker.setCountryPicked(countryCodePicker.getCountry(Locale.getDefault().country))
}

Expand Down
66 changes: 57 additions & 9 deletions ccp/src/main/java/com/ccc/ccp/CountryCodePicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ 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
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 {
Expand All @@ -27,6 +32,8 @@ class CountryCodePicker : LinearLayout {
private var mFlagWidth: Float = DimensionUtils.getDimensionWithDensity(context, R.dimen.dp_24)
private var mCountries = ArrayList<Country>()
private var mCountryPicked: Country? = null
private var mOnLoadDataComplete: OnLoadDataCompleteListener? = null
private var mOnRefreshDataComplete: OnRefreshDataCompleteListener? = null

constructor(context: Context) : super(context)

Expand Down Expand Up @@ -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<List<Country>> {
it.onSuccess(
Country.loadCountryDataFromXML(context, mLanguageCode)
)
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver<List<Country>>() {
override fun onSuccess(countries: List<Country>) {
mCountries.addAll(countries)
mOnRefreshDataComplete?.onRefreshDataComplete()
}

override fun onError(e: Throwable) {
Log.e(this::class.java.simpleName, "error", e)
}
})
}

fun updateData(countries: List<Country>) {
Expand All @@ -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
}
Expand All @@ -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? {
Expand All @@ -115,7 +140,22 @@ class CountryCodePicker : LinearLayout {
}

private fun initData() {
mCountries.addAll(Country.loadCountryDataFromXML(context, mLanguageCode))
Single.create<List<Country>> {
it.onSuccess(
Country.loadCountryDataFromXML(context, mLanguageCode)
)
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver<List<Country>>() {
override fun onSuccess(countries: List<Country>) {
mCountries.addAll(countries)
mOnLoadDataComplete?.onLoadDataComplete()
}

override fun onError(e: Throwable) {
Log.e(this::class.java.simpleName, "error", e)
}
})
}

private fun initViews() {
Expand Down Expand Up @@ -150,4 +190,12 @@ class CountryCodePicker : LinearLayout {
companion object {
private val TAG = this::class.java.simpleName
}

interface OnLoadDataCompleteListener {
fun onLoadDataComplete()
}

interface OnRefreshDataCompleteListener {
fun onRefreshDataComplete()
}
}
4 changes: 2 additions & 2 deletions ccp/src/main/java/com/ccc/ccp/CountryCodePickerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class CountryCodePickerFragment : DialogFragment(), CountryCodeAdapter.OnItemCli
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : DisposableSingleObserver<List<Country>>() {
override fun onSuccess(data: List<Country>) {
mAdapter.updateData(data)
override fun onSuccess(countries: List<Country>) {
mAdapter.updateData(countries)
}

override fun onError(e: Throwable) {
Expand Down

0 comments on commit 5bd9ffb

Please sign in to comment.