diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/AccountTransactionAdapter.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/AccountTransactionAdapter.kt index b05970aa..2bf596d6 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/AccountTransactionAdapter.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/AccountTransactionAdapter.kt @@ -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) + } + } + } \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/BankTransferDialog.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/BankTransferDialog.kt index c05428bb..fc4af3cf 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/BankTransferDialog.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/BankTransferDialog.kt @@ -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 { diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt index f4a953fd..bd6a2a92 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/home/HomeFragment.kt @@ -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) diff --git a/fints4javaAndroidApp/src/main/res/menu/context_menu_account_transactions.xml b/fints4javaAndroidApp/src/main/res/menu/context_menu_account_transactions.xml new file mode 100644 index 00000000..b8a013c6 --- /dev/null +++ b/fints4javaAndroidApp/src/main/res/menu/context_menu_account_transactions.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/res/values/strings.xml b/fints4javaAndroidApp/src/main/res/values/strings.xml index 2cd1a72e..1bb0b20c 100644 --- a/fints4javaAndroidApp/src/main/res/values/strings.xml +++ b/fints4javaAndroidApp/src/main/res/values/strings.xml @@ -17,6 +17,7 @@ Update transactions Could not retrieve account transactions:\n%s + New bank transfer to %s Bank code: FinTS server address: