diff --git a/app/src/main/java/com/github/muellerma/prepaidbalance/ui/BalanceListAdapter.kt b/app/src/main/java/com/github/muellerma/prepaidbalance/ui/BalanceListAdapter.kt index c75f3326..11451d09 100644 --- a/app/src/main/java/com/github/muellerma/prepaidbalance/ui/BalanceListAdapter.kt +++ b/app/src/main/java/com/github/muellerma/prepaidbalance/ui/BalanceListAdapter.kt @@ -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() { var balances = emptyList() 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) diff --git a/app/src/main/java/com/github/muellerma/prepaidbalance/ui/MainActivity.kt b/app/src/main/java/com/github/muellerma/prepaidbalance/ui/MainActivity.kt index 95de751f..8eb91513 100644 --- a/app/src/main/java/com/github/muellerma/prepaidbalance/ui/MainActivity.kt +++ b/app/src/main/java/com/github/muellerma/prepaidbalance/ui/MainActivity.kt @@ -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 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 908706c0..9977093a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -46,6 +46,8 @@ Retry A USSD request will be made to %s Close + Delete + Do you want do delete the entry \"%s\"? No response saved Last update: %s Show latest balance