From 88923687829c9400f100f132c8f9d5926a177280 Mon Sep 17 00:00:00 2001 From: dankl Date: Thu, 31 Oct 2019 00:52:49 +0100 Subject: [PATCH] Implemented selected TAN procedure after successfully adding an account --- .../android/ui/MainWindowPresenter.kt | 55 +++++++++++++----- .../ui/adapter/TanProceduresAdapter.kt | 25 ++++++++ .../android/ui/dialogs/AddAccountDialog.kt | 57 +++++++++++++++---- .../res/layout/list_item_tan_procedure.xml | 10 ++++ .../view_successfully_added_account.xml | 35 ++++++++++++ .../src/main/res/values/dimens.xml | 4 ++ .../response/client/AddAccountResponse.kt | 8 +-- 7 files changed, 166 insertions(+), 28 deletions(-) create mode 100644 fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/TanProceduresAdapter.kt create mode 100644 fints4javaAndroidApp/src/main/res/layout/list_item_tan_procedure.xml create mode 100644 fints4javaAndroidApp/src/main/res/layout/view_successfully_added_account.xml diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt index 95d5054a..cb5e6db3 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/MainWindowPresenter.kt @@ -11,6 +11,7 @@ import net.dankito.fints.response.client.FinTsClientResponse import net.dankito.fints.response.client.GetTransactionsResponse import net.dankito.utils.IThreadPool import net.dankito.utils.ThreadPool +import java.math.BigDecimal import java.util.* import kotlin.collections.ArrayList @@ -28,9 +29,11 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected val accounts = mutableMapOf() - protected val bookedTransactions = mutableSetOf() // TODO: map by account + protected val bookedTransactions = mutableMapOf>() - protected val unbookedTransactions = mutableSetOf() + protected val unbookedTransactions = mutableMapOf>() + + protected val balances = mutableMapOf() protected val accountAddedListeners = mutableListOf<(BankData, CustomerData) -> Unit>() @@ -45,9 +48,11 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { if (response.isSuccessful) { accounts.put(customer, bank) - // TODO: show booked transactions of last 90 days in HomeFragment if available - callAccountAddedListeners(bank, customer) + + if (response.supportsRetrievingTransactionsOfLast90DaysWithoutTan) { + retrievedAccountTransactions(customer, response) + } } callback(response) @@ -65,21 +70,43 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { callback: (GetTransactionsResponse) -> Unit) { finTsClient.getTransactionsAsync(GetTransactionsParameter(true, fromDate), bank, customer) { response -> - if (response.isSuccessful) { - bookedTransactions.addAll(response.bookedTransactions) // TODO: does currently not work, overwrite equals() - unbookedTransactions.addAll(response.unbookedTransactions) - } + retrievedAccountTransactions(customer, response) callback(response) // TODO: does not return all booked transactions, only the newly retrieved ones! } } open fun updateAccountsTransactionsAsync(callback: (GetTransactionsResponse) -> Unit) { - val today = Date() // TODO: still don't know where this bug is coming from that bank returns a transaction dated at end of year - val lastRetrievedTransactionDate = bookedTransactions.firstOrNull { it.bookingDate <= today }?.bookingDate // TODO: make multi-account ready; currently if don't differentiate booked transactions by accounts - val fromDate = lastRetrievedTransactionDate?.let { Date(it.time - 24 * 60 * 60 * 1000) } // on day before last received transaction + accounts.forEach { entry -> + val customer = entry.key + val today = Date() // TODO: still don't know where this bug is coming from that bank returns a transaction dated at end of year + val lastRetrievedTransactionDate = bookedTransactions[customer]?.firstOrNull { it.bookingDate <= today }?.bookingDate + val fromDate = lastRetrievedTransactionDate?.let { Date(it.time - 24 * 60 * 60 * 1000) } // on day before last received transaction - accounts.forEach { entry -> getAccountTransactionsAsync(entry.value, entry.key, fromDate, callback) } // TODO: this is not a good solution for multiple accounts + getAccountTransactionsAsync(entry.value, customer, fromDate, callback) + } + } + + protected open fun retrievedAccountTransactions(customer: CustomerData, response: GetTransactionsResponse) { + if (response.isSuccessful) { + if (bookedTransactions.containsKey(customer) == false) { + bookedTransactions.put(customer, response.bookedTransactions.toMutableSet()) + } + else { + bookedTransactions[customer]?.addAll(response.bookedTransactions) // TODO: does currently not work, overwrite equals() + } + + if (unbookedTransactions.containsKey(customer) == false) { + unbookedTransactions.put(customer, response.unbookedTransactions.toMutableSet()) + } + else { + unbookedTransactions[customer]?.addAll(response.unbookedTransactions) + } + + response.balance?.let { + balances[customer] = it + } + } } @@ -105,10 +132,10 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { val queryLowercase = query.trim().toLowerCase() if (queryLowercase.isEmpty()) { - return bookedTransactions.toList() + return bookedTransactions.values.flatten().toList() } - return bookedTransactions.filter { + return bookedTransactions.values.flatten().filter { it.otherPartyName?.toLowerCase()?.contains(queryLowercase) == true || it.usage.toLowerCase().contains(queryLowercase) || it.bookingText?.toLowerCase()?.contains(queryLowercase) == true diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/TanProceduresAdapter.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/TanProceduresAdapter.kt new file mode 100644 index 00000000..6ca9bfb0 --- /dev/null +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/adapter/TanProceduresAdapter.kt @@ -0,0 +1,25 @@ +package net.dankito.banking.fints4java.android.ui.adapter + +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import net.dankito.banking.fints4java.android.R +import net.dankito.fints.model.TanProcedure +import net.dankito.utils.android.extensions.asActivity +import net.dankito.utils.android.ui.adapter.ListAdapter + + +open class TanProceduresAdapter : ListAdapter() { + + override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? { + val procedure = getItem(position) + + val view = convertView ?: parent?.context?.asActivity()?.layoutInflater?.inflate( + R.layout.list_item_tan_procedure, parent, false) + + view?.findViewById(R.id.txtTanProcedureDisplayName)?.text = procedure.displayName + + return view + } + +} \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt index 1fef98ce..0b4310ec 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt @@ -1,5 +1,6 @@ package net.dankito.banking.fints4java.android.ui.dialogs +import android.content.Context import android.content.DialogInterface import android.os.Bundle import android.support.v4.app.DialogFragment @@ -10,11 +11,15 @@ import android.text.TextWatcher import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.Spinner +import android.widget.TextView import kotlinx.android.synthetic.main.dialog_add_account.* import kotlinx.android.synthetic.main.dialog_add_account.view.* import net.dankito.banking.fints4java.android.R import net.dankito.banking.fints4java.android.ui.MainWindowPresenter import net.dankito.banking.fints4java.android.ui.adapter.BankListAdapter +import net.dankito.banking.fints4java.android.ui.adapter.TanProceduresAdapter import net.dankito.fints.model.BankInfo import net.dankito.fints.response.client.AddAccountResponse import net.dankito.utils.android.extensions.asActivity @@ -91,16 +96,7 @@ open class AddAccountDialog : DialogFragment() { if (response.isSuccessful) { this.dismiss() - val messageId = if (response.supportsRetrievingTransactionsOfLast90DaysWithoutTan) - R.string.dialog_add_account_message_successfully_added_account_support_retrieving_transactions_of_last_90_days_without_tan - else R.string.dialog_add_account_message_successfully_added_account - - AlertDialog.Builder(context) - .setMessage(messageId) - .setPositiveButton(R.string.yes) { dialog, _ -> retrieveAccountTransactionsAndDismiss(response, dialog) } - .setNeutralButton(R.string.later) { dialog, _ -> dialog.dismiss() } - .setNegativeButton(R.string.do_not_ask_anymore) { dialog, _ -> setDoNotAskAnymoreAndDismiss(dialog) } - .show() + showMessageForSuccessfullyAddedAccount(context, response) } else { AlertDialog.Builder(context) @@ -111,6 +107,47 @@ open class AddAccountDialog : DialogFragment() { } } + protected open fun showMessageForSuccessfullyAddedAccount(context: Context, response: AddAccountResponse) { + val view = createSuccessfullyAddedAccountView(context, response) + + AlertDialog.Builder(context) + .setView(view) + .setPositiveButton(R.string.yes) { dialog, _ -> retrieveAccountTransactionsAndDismiss(response, dialog) } + .setNeutralButton(R.string.later) { dialog, _ -> dialog.dismiss() } + .setNegativeButton(R.string.do_not_ask_anymore) { dialog, _ -> setDoNotAskAnymoreAndDismiss(dialog) } + .show() + } + + protected open fun createSuccessfullyAddedAccountView(context: Context, response: AddAccountResponse): View? { + + val messageId = if (response.supportsRetrievingTransactionsOfLast90DaysWithoutTan) + R.string.dialog_add_account_message_successfully_added_account_support_retrieving_transactions_of_last_90_days_without_tan + else R.string.dialog_add_account_message_successfully_added_account + + val view = context.asActivity()?.layoutInflater?.inflate(R.layout.view_successfully_added_account, null) + + val adapter = TanProceduresAdapter() + adapter.setItems(response.customer.supportedTanProcedures) + + view?.findViewById(R.id.txtSuccessfullyAddedAccountMessage)?.setText(messageId) + + view?.findViewById(R.id.spnTanProcedures)?.let { spinner -> + spinner.adapter = adapter + spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onNothingSelected(parent: AdapterView<*>?) {} + + override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + response.customer.selectedTanProcedure = adapter.getItem(position) + } + + } + + spinner.setSelection(adapter.getItems().indexOfFirst { it.displayName.contains("manuell", true) == false }) + } + + return view + } + protected open fun retrieveAccountTransactionsAndDismiss(response: AddAccountResponse, messageDialog: DialogInterface) { presenter.getAccountTransactionsAsync(response.bank, response.customer) { } // TODO: show error message if not successful. Here or in HomeFragment diff --git a/fints4javaAndroidApp/src/main/res/layout/list_item_tan_procedure.xml b/fints4javaAndroidApp/src/main/res/layout/list_item_tan_procedure.xml new file mode 100644 index 00000000..1f28489e --- /dev/null +++ b/fints4javaAndroidApp/src/main/res/layout/list_item_tan_procedure.xml @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/res/layout/view_successfully_added_account.xml b/fints4javaAndroidApp/src/main/res/layout/view_successfully_added_account.xml new file mode 100644 index 00000000..b25392a8 --- /dev/null +++ b/fints4javaAndroidApp/src/main/res/layout/view_successfully_added_account.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/res/values/dimens.xml b/fints4javaAndroidApp/src/main/res/values/dimens.xml index fdedbe7f..07db9497 100644 --- a/fints4javaAndroidApp/src/main/res/values/dimens.xml +++ b/fints4javaAndroidApp/src/main/res/values/dimens.xml @@ -29,6 +29,10 @@ 30dp 80dp + 12dp + 12dp + 40dp + 4dp 40dp 50dp diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/AddAccountResponse.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/AddAccountResponse.kt index 61c1acd0..fa5bd816 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/AddAccountResponse.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/response/client/AddAccountResponse.kt @@ -12,8 +12,8 @@ open class AddAccountResponse( val bank: BankData, val customer: CustomerData, val supportsRetrievingTransactionsOfLast90DaysWithoutTan: Boolean = false, - val bookedTransactionsOfLast90Days: List = listOf(), - val unbookedTransactionsOfLast90Days: List = listOf(), - val balance: BigDecimal? = null + bookedTransactionsOfLast90Days: List = listOf(), + unbookedTransactionsOfLast90Days: List = listOf(), + balance: BigDecimal? = null ) - : FinTsClientResponse(response) \ No newline at end of file + : GetTransactionsResponse(response, bookedTransactionsOfLast90Days, unbookedTransactionsOfLast90Days, balance) \ No newline at end of file