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.

This commit is contained in:
Severiano Jaramillo 2019-02-11 12:16:56 -06:00
parent 42e0fc74b0
commit 21c6b776af
7 changed files with 14 additions and 18 deletions

View file

@ -9,6 +9,7 @@ import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.ConfigurationCompat
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList import androidx.recyclerview.widget.SortedList
@ -72,7 +73,7 @@ class TransfersDetailsAdapter(private val context: Context) :
private val timeFormat: SimpleDateFormat private val timeFormat: SimpleDateFormat
init { init {
val locale = context.resources.configuration.locale val locale = ConfigurationCompat.getLocales(context.resources.configuration)[0]
dateFormat = SimpleDateFormat("dd MMM", locale) dateFormat = SimpleDateFormat("dd MMM", locale)
timeFormat = SimpleDateFormat("HH:mm:ss z", locale) timeFormat = SimpleDateFormat("HH:mm:ss z", locale)
} }

View file

@ -16,7 +16,6 @@ import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel
import kotlinx.android.synthetic.main.fragment_balances.* import kotlinx.android.synthetic.main.fragment_balances.*
class BalancesFragment: Fragment() { class BalancesFragment: Fragment() {
private val TAG = this.javaClass.simpleName
private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel private lateinit var mBalanceDetailViewModel: BalanceDetailViewModel
@ -49,7 +48,7 @@ class BalancesFragment: Fragment() {
super.setUserVisibleHint(isVisibleToUser) super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) { if (isVisibleToUser) {
// TODO find a better way to recreate the fragment, that does it only when the theme has been changed // 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()
} }
} }
} }

View file

@ -9,6 +9,7 @@ import android.text.Html
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.view.* import android.view.*
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.os.ConfigurationCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
@ -48,7 +49,7 @@ class EReceiptFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
mLocale = resources.configuration.locale mLocale = ConfigurationCompat.getLocales(resources.configuration)[0]
val userId = PreferenceManager.getDefaultSharedPreferences(context) val userId = PreferenceManager.getDefaultSharedPreferences(context)
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") ?: "" .getString(Constants.KEY_CURRENT_ACCOUNT_ID, "") ?: ""
@ -63,9 +64,9 @@ class EReceiptFragment : Fragment() {
} }
private fun bindTransferDetail(transferDetail: TransferDetail) { 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 if(transferDetail.direction) R.color.colorReceive else R.color.colorSend
)) ))}
tvFrom.text = transferDetail.from ?: "" tvFrom.text = transferDetail.from ?: ""
tvTo.text = transferDetail.to ?: "" tvTo.text = transferDetail.to ?: ""

View file

@ -10,6 +10,7 @@ import android.view.View
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import android.widget.* import android.widget.*
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.os.ConfigurationCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -92,7 +93,7 @@ class FilterOptionsDialog : DialogFragment() {
private lateinit var mDatePickerHandler: DatePickerHandler private lateinit var mDatePickerHandler: DatePickerHandler
private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy", 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 startDate: Long = 0
private var endDate: Long = 0 private var endDate: Long = 0

View file

@ -19,8 +19,6 @@ import cy.agorise.bitsybitshareswallet.database.entities.UserAccount
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
import kotlinx.android.synthetic.main.fragment_home.* import kotlinx.android.synthetic.main.fragment_home.*
import android.os.Build
import android.content.res.ColorStateList
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -112,10 +110,9 @@ class HomeFragment : Fragment() {
var icon = tabLayout.getTabAt(2)?.icon var icon = tabLayout.getTabAt(2)?.icon
if (icon != null) { if (icon != null) {
val colors: ColorStateList = if (Build.VERSION.SDK_INT >= 23) { val colors = context?.let {context ->
resources.getColorStateList(R.color.tab_icon_selector, context?.theme) ContextCompat.getColorStateList(
} else { context, R.color.tab_icon_selector)
resources.getColorStateList(R.color.tab_icon_selector)
} }
icon = DrawableCompat.wrap(icon) icon = DrawableCompat.wrap(icon)

View file

@ -22,7 +22,7 @@ class NetWorthFragment: Fragment() {
super.setUserVisibleHint(isVisibleToUser) super.setUserVisibleHint(isVisibleToUser)
if (isVisibleToUser) { if (isVisibleToUser) {
// TODO find a better way to recreate the fragment, that does it only when the theme has been changed // 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()
} }
} }
} }

View file

@ -325,10 +325,7 @@ class ReceiveTransactionFragment : ConnectedFragment() {
} }
/** /**
* Updates the UI to show amount and account to send the payment * Updates the UI to show the amount and account to send the payment
*
* @param total Total Amount in crypto to be paid
* @param account Account to pay total
*/ */
private fun updateAmountAddressUI(assetAmount: Long, assetSymbol: String, assetPrecision: Int, account: String) { private fun updateAmountAddressUI(assetAmount: Long, assetSymbol: String, assetPrecision: Int, account: String) {
val txtAmount: String = if (assetAmount == 0L) { val txtAmount: String = if (assetAmount == 0L) {