From 84cf0359bd70ff902c44793fd24495cb7b120e44 Mon Sep 17 00:00:00 2001 From: Michael Totschnig Date: Sun, 17 Nov 2024 11:11:44 +0100 Subject: [PATCH] Safe guard if context menu info is not set seen on Xiaomi Mi 10T Android 12 --- .../myexpenses/activity/ExpenseEdit.kt | 32 +++++++++++++------ .../activity/RoadmapVoteActivity.kt | 3 +- .../totschnig/myexpenses/fragment/TagList.kt | 9 +++--- .../myexpenses/ui/ContextAwareRecyclerView.kt | 4 +-- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/activity/ExpenseEdit.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/activity/ExpenseEdit.kt index 8623f3ad15..ead98f31f5 100755 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/activity/ExpenseEdit.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/activity/ExpenseEdit.kt @@ -621,7 +621,8 @@ open class ExpenseEdit : AmountActivity(), ContribIFac } override fun onContextItemSelected(item: MenuItem): Boolean { - val info = item.menuInfo as ContextAwareRecyclerView.RecyclerContextMenuInfo + val info = item.menuInfo as? ContextAwareRecyclerView.RecyclerContextMenuInfo + ?: return super.onContextItemSelected(item) return when (item.itemId) { R.id.EDIT_COMMAND -> { startActivityForResult(Intent(this, ExpenseEdit::class.java).apply { @@ -1430,8 +1431,12 @@ open class ExpenseEdit : AmountActivity(), ContribIFac if (!isSplitPartOrTemplate) { val criterionInfos = listOfNotNull( currentAccount!!.run { - val previousAmount = with(delegate) { passedInAmount?.takeIf { passedInAccountId == id } ?: 0 } - val previousTransferAmount = (delegate as? TransferDelegate)?.run { passedInTransferAmount?.takeIf { passedInTransferAccountId == id } } ?: 0 + val previousAmount = with(delegate) { + passedInAmount?.takeIf { passedInAccountId == id } ?: 0 + } + val previousTransferAmount = + (delegate as? TransferDelegate)?.run { passedInTransferAmount?.takeIf { passedInTransferAccountId == id } } + ?: 0 criterion?.let { CriterionInfo( id, @@ -1448,9 +1453,12 @@ open class ExpenseEdit : AmountActivity(), ContribIFac transferAccount?.run { val transaction = transaction as Transfer val delegate = delegate as TransferDelegate - val previousAmount = with(delegate) { passedInAmount?.takeIf { passedInAccountId == id } ?: 0 } - val previousTransferAmount = with(delegate) { passedInTransferAmount?.takeIf { passedInTransferAccountId == id } } - ?: 0 + val previousAmount = with(delegate) { + passedInAmount?.takeIf { passedInAccountId == id } ?: 0 + } + val previousTransferAmount = + with(delegate) { passedInTransferAmount?.takeIf { passedInTransferAccountId == id } } + ?: 0 criterion?.let { CriterionInfo( id, @@ -1465,19 +1473,23 @@ open class ExpenseEdit : AmountActivity(), ContribIFac } }?.takeIf { it.hasReached() } ) - when(criterionInfos.size) { + when (criterionInfos.size) { //if a transfer leads to a credit limit and a saving goal being hit at the same time //in two different accounts, we give a priority to the credit limit and show saving goal in toast - 2 -> criterionInfos.first { it.criterion < 0} + 2 -> criterionInfos.first { it.criterion < 0 } 1 -> criterionInfos.first() else -> null }?.let { CriterionReachedDialogFragment .newInstance(it, - if (criterionInfos.size == 2) with(criterionInfos.first { it.criterion > 0 }) { accountLabel + ": " + getString(dialogTitle) } else null + if (criterionInfos.size == 2) with(criterionInfos.first { it.criterion > 0 }) { + accountLabel + ": " + getString( + dialogTitle + ) + } else null ) .show(supportFragmentManager, "CRITERION") - if(!createNew) return + if (!createNew) return } } if (createNew) { diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/activity/RoadmapVoteActivity.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/activity/RoadmapVoteActivity.kt index 91b93aa750..cdf1101bed 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/activity/RoadmapVoteActivity.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/activity/RoadmapVoteActivity.kt @@ -263,7 +263,8 @@ class RoadmapVoteActivity : ProtectedFragmentActivity(), OnDialogResultListener } override fun onContextItemSelected(item: MenuItem): Boolean { - val info = item.menuInfo as RecyclerContextMenuInfo + val info = item.menuInfo as? RecyclerContextMenuInfo + ?: return super.onContextItemSelected(item) val itemId = item.itemId if (itemId == R.id.ROADMAP_DETAILS_COMMAND) { startActionView("https://github.com/mtotschnig/MyExpenses/issues/" + info.id) diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/fragment/TagList.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/fragment/TagList.kt index 0f959a36c9..708f4eb088 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/fragment/TagList.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/fragment/TagList.kt @@ -35,11 +35,10 @@ import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_ACCOUNTID import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_COLOR import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_LABEL import org.totschnig.myexpenses.provider.filter.KEY_SELECTION -import org.totschnig.myexpenses.ui.ContextAwareRecyclerView +import org.totschnig.myexpenses.ui.ContextAwareRecyclerView.RecyclerContextMenuInfo import org.totschnig.myexpenses.util.ui.setColor import org.totschnig.myexpenses.viewmodel.TagBaseViewModel.Companion.KEY_DELETED_IDS import org.totschnig.myexpenses.viewmodel.TagListViewModel -import org.totschnig.myexpenses.viewmodel.TagListViewModel.Companion.KEY_SELECTED_IDS import org.totschnig.myexpenses.viewmodel.data.Tag class TagList : Fragment(), OnDialogResultListener { @@ -84,9 +83,9 @@ class TagList : Fragment(), OnDialogResultListener { } override fun onContextItemSelected(item: MenuItem): Boolean { - val tag = adapter.getItem( - (item.menuInfo as ContextAwareRecyclerView.RecyclerContextMenuInfo).position - ) + val info = item.menuInfo as? RecyclerContextMenuInfo + ?: return super.onContextItemSelected(item) + val tag = adapter.getItem(info.position) return when (item.itemId) { R.id.DELETE_COMMAND -> { SimpleDialog.build() diff --git a/myExpenses/src/main/java/org/totschnig/myexpenses/ui/ContextAwareRecyclerView.kt b/myExpenses/src/main/java/org/totschnig/myexpenses/ui/ContextAwareRecyclerView.kt index 27e148744e..a1e108743c 100644 --- a/myExpenses/src/main/java/org/totschnig/myexpenses/ui/ContextAwareRecyclerView.kt +++ b/myExpenses/src/main/java/org/totschnig/myexpenses/ui/ContextAwareRecyclerView.kt @@ -15,8 +15,8 @@ class ContextAwareRecyclerView : RecyclerView { constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) - override fun getContextMenuInfo(): RecyclerContextMenuInfo { - return contextMenuInfo!! + override fun getContextMenuInfo(): RecyclerContextMenuInfo? { + return contextMenuInfo } override fun showContextMenuForChild(originalView: View): Boolean {