Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long-press to delete an entry #253

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
[email protected]()
}
}
.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