From 21c6b776aff9d0899cafa3ca72ef60decb6575df Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Mon, 11 Feb 2019 12:16:56 -0600 Subject: [PATCH] Make use of Kotlin's null safe operations to avoid NullPointerException in various places in the app. Use of the ContextCompat and ConfigurationCompat classes in different screens to safely access app configuration and resources in a backwards compatible way. --- .../adapters/TransfersDetailsAdapter.kt | 3 ++- .../bitsybitshareswallet/fragments/BalancesFragment.kt | 3 +-- .../bitsybitshareswallet/fragments/EReceiptFragment.kt | 7 ++++--- .../fragments/FilterOptionsDialog.kt | 3 ++- .../bitsybitshareswallet/fragments/HomeFragment.kt | 9 +++------ .../bitsybitshareswallet/fragments/NetWorthFragment.kt | 2 +- .../fragments/ReceiveTransactionFragment.kt | 5 +---- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt index 8f5b5aa..04192b2 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/adapters/TransfersDetailsAdapter.kt @@ -9,6 +9,7 @@ import android.widget.LinearLayout import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat +import androidx.core.os.ConfigurationCompat import androidx.navigation.findNavController import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.SortedList @@ -72,7 +73,7 @@ class TransfersDetailsAdapter(private val context: Context) : private val timeFormat: SimpleDateFormat init { - val locale = context.resources.configuration.locale + val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0] dateFormat = SimpleDateFormat("dd MMM", locale) timeFormat = SimpleDateFormat("HH:mm:ss z", locale) } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt index 0f0ecd6..d33aa0c 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/BalancesFragment.kt @@ -16,7 +16,6 @@ import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel import kotlinx.android.synthetic.main.fragment_balances.* class BalancesFragment: Fragment() { - private val TAG = this.javaClass.simpleName private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel @@ -49,7 +48,7 @@ class BalancesFragment: Fragment() { super.setUserVisibleHint(isVisibleToUser) if (isVisibleToUser) { // TODO find a better way to recreate the fragment, that does it only when the theme has been changed - fragmentManager!!.beginTransaction().detach(this).attach(this).commit() + fragmentManager?.beginTransaction()?.detach(this)?.attach(this)?.commit() } } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt index dfd38cb..882d3c8 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt @@ -9,6 +9,7 @@ import android.text.Html import android.text.method.LinkMovementMethod import android.view.* import androidx.core.content.ContextCompat +import androidx.core.os.ConfigurationCompat import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders @@ -48,7 +49,7 @@ class EReceiptFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - mLocale = resources.configuration.locale + mLocale = ConfigurationCompat.getLocales(resources.configuration)[0] val userId = PreferenceManager.getDefaultSharedPreferences(context) .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") ?: "" @@ -63,9 +64,9 @@ class EReceiptFragment : Fragment() { } private fun bindTransferDetail(transferDetail: TransferDetail) { - vPaymentDirection.setBackgroundColor(resources.getColor( + context?.let { vPaymentDirection.setBackgroundColor(ContextCompat.getColor(it, if(transferDetail.direction) R.color.colorReceive else R.color.colorSend - )) + ))} tvFrom.text = transferDetail.from ?: "" tvTo.text = transferDetail.to ?: "" diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt index e83d7c7..fe0241f 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt @@ -10,6 +10,7 @@ import android.view.View import androidx.fragment.app.DialogFragment import android.widget.* import androidx.appcompat.app.AlertDialog +import androidx.core.os.ConfigurationCompat import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.Observer @@ -92,7 +93,7 @@ class FilterOptionsDialog : DialogFragment() { private lateinit var mDatePickerHandler: DatePickerHandler private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy", - Resources.getSystem().configuration.locale) + ConfigurationCompat.getLocales(Resources.getSystem().configuration)[0]) private var startDate: Long = 0 private var endDate: Long = 0 diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt index a0d7b98..23d9bef 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/HomeFragment.kt @@ -19,8 +19,6 @@ import cy.agorise.bitsybitshareswallet.database.entities.UserAccount import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel import kotlinx.android.synthetic.main.fragment_home.* -import android.os.Build -import android.content.res.ColorStateList import androidx.appcompat.app.AppCompatActivity @@ -112,10 +110,9 @@ class HomeFragment : Fragment() { var icon = tabLayout.getTabAt(2)?.icon if (icon != null) { - val colors: ColorStateList = if (Build.VERSION.SDK_INT >= 23) { - resources.getColorStateList(R.color.tab_icon_selector, context?.theme) - } else { - resources.getColorStateList(R.color.tab_icon_selector) + val colors = context?.let {context -> + ContextCompat.getColorStateList( + context, R.color.tab_icon_selector) } icon = DrawableCompat.wrap(icon) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/NetWorthFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/NetWorthFragment.kt index 114d84a..9fa2447 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/NetWorthFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/NetWorthFragment.kt @@ -22,7 +22,7 @@ class NetWorthFragment: Fragment() { super.setUserVisibleHint(isVisibleToUser) if (isVisibleToUser) { // TODO find a better way to recreate the fragment, that does it only when the theme has been changed - fragmentManager!!.beginTransaction().detach(this).attach(this).commit() + fragmentManager?.beginTransaction()?.detach(this)?.attach(this)?.commit() } } } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt index 26a6095..562323e 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt @@ -325,10 +325,7 @@ class ReceiveTransactionFragment : ConnectedFragment() { } /** - * Updates the UI to show amount and account to send the payment - * - * @param total Total Amount in crypto to be paid - * @param account Account to pay total + * Updates the UI to show the amount and account to send the payment */ private fun updateAmountAddressUI(assetAmount: Long, assetSymbol: String, assetPrecision: Int, account: String) { val txtAmount: String = if (assetAmount == 0L) {