Skip to content

Commit

Permalink
Long-press to delete an entry (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
mueller-ma authored Nov 5, 2023
1 parent 9008c37 commit 5b75669
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.github.muellerma.prepaidbalance.ui

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.ColorRes
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))
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 5b75669

Please sign in to comment.