From 5870d9b9dad629e966082f04492e3cabf31735e0 Mon Sep 17 00:00:00 2001 From: Kishore Babu Date: Fri, 6 Jul 2018 10:32:42 +0530 Subject: [PATCH 1/3] Change: Use kotlin stdlib for sort --- .../java/com/patloew/countries/ui/detail/DetailViewModel.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt b/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt index 456c108..9d2bd9f 100644 --- a/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt +++ b/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt @@ -101,7 +101,7 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val languageList.add(Locale(language).getDisplayLanguage(BaseCountryViewModel.Companion.DISPLAY_LOCALE)) } - Collections.sort(languageList) + languageList.sort() return SpannableStringBuilder(ctx.getText(R.string.country_languages)).append(TextUtils.join(", ", languageList)) } @@ -113,7 +113,7 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val nameList.add(entry.value + " (" + Locale(entry.key).getDisplayLanguage(BaseCountryViewModel.Companion.DISPLAY_LOCALE) + ")") } - Collections.sort(nameList) + nameList.sort() return SpannableStringBuilder(ctx.getText(R.string.country_name_translations)).append(Html.fromHtml(TextUtils.join(", ", nameList))) } @@ -139,7 +139,7 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val } } - Collections.sort(currenciesList) + currenciesList.sort() return SpannableStringBuilder(ctx.getText(R.string.country_currencies)).append(TextUtils.join(", ", currenciesList)) } From ff9966d761d7073c43996226ad3056301ac8d94e Mon Sep 17 00:00:00 2001 From: Kishore Babu Date: Fri, 6 Jul 2018 17:13:00 +0530 Subject: [PATCH 2/3] Change: Use kotlin's foreach wherever appropriate for better readability --- .../countries/ui/detail/DetailViewModel.kt | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt b/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt index 9d2bd9f..64ec6e5 100644 --- a/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt +++ b/app/src/main/java/com/patloew/countries/ui/detail/DetailViewModel.kt @@ -97,8 +97,10 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val private fun calculateLanguages(): CharSequence { val languageList = ArrayList(country.languages!!.size) - for (language in country.languages!!) { - languageList.add(Locale(language).getDisplayLanguage(BaseCountryViewModel.Companion.DISPLAY_LOCALE)) + country.languages!!.forEach { + language -> + + languageList.add(Locale(language).getDisplayLanguage(BaseCountryViewModel.DISPLAY_LOCALE)) } languageList.sort() @@ -109,8 +111,8 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val private fun calculateNameTranslations(): CharSequence { val nameList = ArrayList(country.translations!!.size) - for (entry in country.translations!!) { - nameList.add(entry.value + " (" + Locale(entry.key).getDisplayLanguage(BaseCountryViewModel.Companion.DISPLAY_LOCALE) + ")") + country.translations!!.forEach { entry -> + nameList.add(entry.value + " (" + Locale(entry.key).getDisplayLanguage(BaseCountryViewModel.DISPLAY_LOCALE) + ")") } nameList.sort() @@ -121,15 +123,17 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val private fun calculateCurrencies(): CharSequence { val currenciesList = ArrayList(country.currencies!!.size) - for (currencyString in country.currencies!!) { + country.currencies!!.forEach { + currencyString -> + if (Build.VERSION.SDK_INT >= 19) { try { val currency = Currency.getInstance(currencyString) var currencySymbol = currency.symbol if (currencyString != currencySymbol) { - currencySymbol = currencyString + ", " + currencySymbol + currencySymbol = "$currencyString, $currencySymbol" } - currenciesList.add(currency.getDisplayName(BaseCountryViewModel.Companion.DISPLAY_LOCALE) + " (" + currencySymbol + ")") + currenciesList.add("""${currency.getDisplayName(BaseCountryViewModel.DISPLAY_LOCALE)} ($currencySymbol)""") } catch (ignore: IllegalArgumentException) { currenciesList.add(currencyString) } @@ -163,7 +167,9 @@ constructor(@AppContext context: Context, countryRepo: CountryRepo, private val val borderList = ArrayList(country.borders!!.size) val alpha3List = ArrayList(country.borders!!.size) - for (borderAlpha3CodeString in country.borders!!) { + + country.borders!!.forEach { + borderAlpha3CodeString -> alpha3List.add(borderAlpha3CodeString) } From aa24107256857a06db6bd68a6ed74395676887e1 Mon Sep 17 00:00:00 2001 From: Kishore Babu Date: Fri, 6 Jul 2018 17:13:50 +0530 Subject: [PATCH 3/3] Change: Use marginEnd/Start instead of marginRight/Left --- app/src/main/res/layout/activity_detail.xml | 8 ++++---- app/src/main/res/layout/card_country.xml | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/src/main/res/layout/activity_detail.xml b/app/src/main/res/layout/activity_detail.xml index add4e47..e018667 100644 --- a/app/src/main/res/layout/activity_detail.xml +++ b/app/src/main/res/layout/activity_detail.xml @@ -47,7 +47,7 @@ android:id="@+id/country_name" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:text="@{vm.name}" style="@style/TextAppearance.AppCompat.Headline" tools:text="Country"/> @@ -56,7 +56,7 @@ android:id="@+id/country_name_translations" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:paddingTop="4dp" android:text='@{vm.nameTranslations}' style="@style/TextAppearance.AppCompat" @@ -66,7 +66,7 @@ android:id="@+id/country_region" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:paddingTop="4dp" android:text='@{vm.region}' style="@style/TextAppearance.AppCompat" @@ -76,7 +76,7 @@ android:id="@+id/country_capital" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:paddingTop="4dp" android:visibility="@{vm.capitalVisible}" android:text='@{vm.capital}' diff --git a/app/src/main/res/layout/card_country.xml b/app/src/main/res/layout/card_country.xml index f4ddd60..e9c0f52 100755 --- a/app/src/main/res/layout/card_country.xml +++ b/app/src/main/res/layout/card_country.xml @@ -11,9 +11,9 @@ android:id="@+id/card_view" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginLeft="@dimen/card_outer_padding" + android:layout_marginStart="@dimen/card_outer_padding" android:layout_marginTop="@dimen/card_outer_padding" - android:layout_marginRight="@dimen/card_outer_padding" + android:layout_marginEnd="@dimen/card_outer_padding" android:layout_marginBottom='@{vm.cardBottomMargin}' android:foreground="?attr/selectableItemBackground" android:onClick="@{vm::onCardClick}" @@ -24,9 +24,9 @@ @@ -34,7 +34,7 @@ android:id="@+id/country_name" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:text="@{vm.name}" style="@style/TextAppearance.AppCompat.Headline" tools:text="Country"/> @@ -43,7 +43,7 @@ android:id="@+id/country_region" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginRight="40dp" + android:layout_marginEnd="40dp" android:paddingTop="4dp" android:text='@{vm.region}' style="@style/TextAppearance.AppCompat"