Implemented context menu for account transactions RecyclerView to show BankTransferDialog for selected item
This commit is contained in:
parent
234def482a
commit
c57f8885e1
|
@ -1,9 +1,11 @@
|
|||
package net.dankito.banking.fints4java.android.ui.adapter
|
||||
|
||||
import android.view.ContextMenu
|
||||
import android.view.View
|
||||
import net.dankito.banking.fints4java.android.R
|
||||
import net.dankito.banking.fints4java.android.ui.adapter.viewholder.AccountTransactionViewHolder
|
||||
import net.dankito.fints.model.AccountTransaction
|
||||
import net.dankito.utils.android.extensions.asActivity
|
||||
import net.dankito.utils.android.extensions.setTextColorToColorResource
|
||||
import net.dankito.utils.android.ui.adapter.ListRecyclerAdapter
|
||||
import java.math.BigDecimal
|
||||
|
@ -18,10 +20,17 @@ open class AccountTransactionAdapter
|
|||
}
|
||||
|
||||
|
||||
var selectedTransaction: AccountTransaction? = null
|
||||
|
||||
|
||||
override fun getListItemLayoutId() = R.layout.list_item_account_transaction
|
||||
|
||||
override fun createViewHolder(itemView: View): AccountTransactionViewHolder {
|
||||
return AccountTransactionViewHolder(itemView)
|
||||
val viewHolder = AccountTransactionViewHolder(itemView)
|
||||
|
||||
itemView.setOnCreateContextMenuListener { menu, view, menuInfo -> createContextMenu(menu, view, menuInfo, viewHolder) }
|
||||
|
||||
return viewHolder
|
||||
}
|
||||
|
||||
override fun bindItemToView(viewHolder: AccountTransactionViewHolder, item: AccountTransaction) {
|
||||
|
@ -40,4 +49,19 @@ open class AccountTransactionAdapter
|
|||
viewHolder.txtvwAmount.setTextColorToColorResource(if (item.amount >= BigDecimal.ZERO) R.color.positiveAmount else R.color.negativeAmount)
|
||||
}
|
||||
|
||||
|
||||
protected open fun createContextMenu(menu: ContextMenu, view: View, menuInfo: ContextMenu.ContextMenuInfo?,
|
||||
viewHolder: AccountTransactionViewHolder) {
|
||||
|
||||
view.context.asActivity()?.menuInflater?.inflate(R.menu.context_menu_account_transactions, menu)
|
||||
|
||||
selectedTransaction = getItem(viewHolder.adapterPosition)
|
||||
|
||||
menu.findItem(R.id.mnitmShowBankTransferDialog)?.let { mnitmShowBankTransferDialog ->
|
||||
val remitteeName = selectedTransaction?.otherPartyName ?: ""
|
||||
|
||||
mnitmShowBankTransferDialog.title = view.context.getString(R.string.fragment_home_new_bank_transfer_to, remitteeName)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -12,7 +12,9 @@ import kotlinx.android.synthetic.main.dialog_bank_transfer.*
|
|||
import kotlinx.android.synthetic.main.dialog_bank_transfer.view.*
|
||||
import net.dankito.banking.fints4java.android.R
|
||||
import net.dankito.banking.fints4java.android.ui.MainWindowPresenter
|
||||
import net.dankito.fints.model.BankTransferData
|
||||
import net.dankito.utils.android.extensions.asActivity
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.math.BigDecimal
|
||||
|
||||
|
||||
|
@ -25,9 +27,16 @@ open class BankTransferDialog : DialogFragment() {
|
|||
|
||||
protected lateinit var presenter: MainWindowPresenter
|
||||
|
||||
protected var preselectedValues: BankTransferData? = null
|
||||
|
||||
|
||||
open fun show(activity: AppCompatActivity, presenter: MainWindowPresenter, fullscreen: Boolean = false) {
|
||||
show(activity, presenter, null, fullscreen)
|
||||
}
|
||||
|
||||
open fun show(activity: AppCompatActivity, presenter: MainWindowPresenter, preselectedValues: BankTransferData?, fullscreen: Boolean = false) {
|
||||
this.presenter = presenter
|
||||
this.preselectedValues = preselectedValues
|
||||
|
||||
val style = if(fullscreen) R.style.FullscreenDialogWithStatusBar else R.style.Dialog
|
||||
setStyle(STYLE_NORMAL, style)
|
||||
|
@ -45,6 +54,8 @@ open class BankTransferDialog : DialogFragment() {
|
|||
}
|
||||
|
||||
protected open fun setupUI(rootView: View) {
|
||||
setPreselectedValues(rootView)
|
||||
|
||||
// TODO: add autocompletion by searching for name in account entries
|
||||
rootView.edtxtRemitteeName.addTextChangedListener(otherEditTextChangedWatcher)
|
||||
|
||||
|
@ -58,6 +69,34 @@ open class BankTransferDialog : DialogFragment() {
|
|||
rootView.btnDoBankTransfer.setOnClickListener { dismiss() }
|
||||
}
|
||||
|
||||
protected open fun setPreselectedValues(rootView: View) {
|
||||
preselectedValues?.let { data ->
|
||||
rootView.edtxtRemitteeName.setText(data.creditorName)
|
||||
|
||||
rootView.edtxtRemitteeIban.setText(data.creditorIban)
|
||||
|
||||
rootView.edtxtRemitteeBic.setText(data.creditorBic)
|
||||
|
||||
focusEditTextAccordingToPreselectedValues(rootView, data)
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun focusEditTextAccordingToPreselectedValues(rootView: View, data: BankTransferData) {
|
||||
if (data.creditorName.trim().isNotEmpty()) {
|
||||
if (data.creditorIban.trim().isNotEmpty()) {
|
||||
if (data.creditorBic.trim().isNotEmpty()) {
|
||||
rootView.edtxtAmount.requestFocus()
|
||||
}
|
||||
else {
|
||||
rootView.edtxtRemitteeBic.requestFocus()
|
||||
}
|
||||
}
|
||||
else {
|
||||
rootView.edtxtRemitteeIban.requestFocus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected val otherEditTextChangedWatcher = object : TextWatcher {
|
||||
|
||||
|
|
|
@ -67,6 +67,8 @@ class HomeFragment : Fragment() {
|
|||
rcyvwAccountTransactions.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
||||
rcyvwAccountTransactions.adapter = transactionAdapter
|
||||
|
||||
registerForContextMenu(rcyvwAccountTransactions) // this is actually bad, splits code as context menu is created in AccountTransactionAdapter
|
||||
|
||||
initLogic()
|
||||
|
||||
return root
|
||||
|
@ -116,6 +118,18 @@ class HomeFragment : Fragment() {
|
|||
}
|
||||
|
||||
|
||||
override fun onContextItemSelected(item: MenuItem): Boolean {
|
||||
when (item.itemId) {
|
||||
R.id.mnitmShowBankTransferDialog -> {
|
||||
showBankTransferDialog()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return super.onContextItemSelected(item)
|
||||
}
|
||||
|
||||
|
||||
private fun initLogic() {
|
||||
|
||||
// TODO: this is such a bad code style
|
||||
|
@ -164,6 +178,27 @@ class HomeFragment : Fragment() {
|
|||
}
|
||||
|
||||
|
||||
private fun showBankTransferDialog() {
|
||||
(context as? AppCompatActivity)?.let { activity ->
|
||||
BankTransferDialog().show(activity, presenter, mapPreselectedValues())
|
||||
}
|
||||
}
|
||||
|
||||
private fun mapPreselectedValues(): BankTransferData? {
|
||||
transactionAdapter.selectedTransaction?.let { selectedTransaction ->
|
||||
return BankTransferData(
|
||||
selectedTransaction.otherPartyName ?: "",
|
||||
selectedTransaction.otherPartyAccountId ?: "",
|
||||
selectedTransaction.otherPartyBankCode ?: "",
|
||||
BigDecimal.ZERO,
|
||||
""
|
||||
)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
|
||||
private val searchAccountTransactionsTextListener: SearchView.OnQueryTextListener = object : SearchView.OnQueryTextListener {
|
||||
override fun onQueryTextChange(query: String): Boolean {
|
||||
searchAccountTransactions(query)
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:id="@+id/mnitmShowBankTransferDialog"
|
||||
android:title="@string/fragment_home_new_bank_transfer_to"
|
||||
/>
|
||||
|
||||
</menu>
|
|
@ -17,6 +17,7 @@
|
|||
<string name="menu_main_update_transactions">Update transactions</string>
|
||||
|
||||
<string name="fragment_home_could_not_retrieve_account_transactions">Could not retrieve account transactions:\n%s</string>
|
||||
<string name="fragment_home_new_bank_transfer_to">New bank transfer to %s</string>
|
||||
|
||||
<string name="dialog_add_account_enter_bank_code">Bank code:</string>
|
||||
<string name="dialog_add_account_enter_fints_server_address">FinTS server address:</string>
|
||||
|
|
Loading…
Reference in New Issue