0
0
mirror of https://github.com/mueller-ma/PrepaidBalance.git synced 2024-09-19 16:02:14 +02:00

Long-press to delete an entry (#253)

This commit is contained in:
mueller-ma 2023-11-05 12:39:11 +01:00 committed by GitHub
parent 9008c37044
commit 5b75669ab4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 4 deletions

View File

@ -1,6 +1,5 @@
package com.github.muellerma.prepaidbalance.ui
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.ColorRes
@ -8,21 +7,25 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.github.muellerma.prepaidbalance.R
import com.github.muellerma.prepaidbalance.databinding.ListBalanceBinding
import com.github.muellerma.prepaidbalance.room.AppDatabase
import com.github.muellerma.prepaidbalance.room.BalanceEntry
import com.github.muellerma.prepaidbalance.utils.formatAsCurrency
import com.github.muellerma.prepaidbalance.utils.formatAsDiff
import com.github.muellerma.prepaidbalance.utils.showToast
import com.github.muellerma.prepaidbalance.utils.timestampForUi
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class BalanceListAdapter(context: Context) :
class BalanceListAdapter(val activity: MainActivity) :
RecyclerView.Adapter<BalanceListViewHolder>() {
var balances = emptyList<BalanceEntry>()
set(value) {
field = value
notifyDataSetChanged()
}
private val inflater = LayoutInflater.from(context)
private val inflater = LayoutInflater.from(activity)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BalanceListViewHolder {
return BalanceListViewHolder(ListBalanceBinding.inflate(inflater, parent, false))
@ -55,6 +58,20 @@ class BalanceListAdapter(context: Context) :
.show()
}
}
holder.binding.card.setOnLongClickListener {
val balance = balances[position]
MaterialAlertDialogBuilder(it.context)
.setPositiveButton(R.string.delete) { _, _ ->
CoroutineScope(Dispatchers.IO).launch {
AppDatabase.get(it.context).balanceDao().delete(balance)
this@BalanceListAdapter.activity.updateBalanceList()
}
}
.setNegativeButton(android.R.string.cancel, null)
.setMessage(it.context.getString(R.string.delete_entry, balance.balance.formatAsCurrency()))
.show()
true
}
holder.binding.date.apply {
text = balances[position].timestamp.timestampForUi(context)

View File

@ -103,7 +103,7 @@ class MainActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListene
super.onResume()
}
private fun updateBalanceList() {
fun updateBalanceList() {
Log.d(TAG, "updateBalanceList()")
launch {
val lastOneYear = System.currentTimeMillis() - 12L * 30 * 24 * 60 * 60 * 1000

View File

@ -46,6 +46,8 @@
<string name="retry">Retry</string>
<string name="confirm_first_request">A USSD request will be made to %s</string>
<string name="close">Close</string>
<string name="delete">Delete</string>
<string name="delete_entry">Do you want do delete the entry \"%s\"?</string>
<string name="no_response_saved">No response saved</string>
<string name="last_update">Last update: %s</string>
<string name="widget_description">Show latest balance</string>