From 234def482a4bfcd023a294eb5b94cb4628d0130c Mon Sep 17 00:00:00 2001 From: dankl Date: Sun, 27 Oct 2019 21:56:46 +0100 Subject: [PATCH] Implemented updating account transactions --- .../android/ui/MainWindowPresenter.kt | 26 +++++++-- .../android/ui/home/HomeFragment.kt | 53 +++++++++++++++---- .../src/main/res/menu/menu_main.xml | 16 +++++- .../src/main/res/values/strings.xml | 4 ++ 4 files changed, 84 insertions(+), 15 deletions(-) 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 e5544447..51c6e36f 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 @@ -10,6 +10,8 @@ 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.util.* +import kotlin.collections.ArrayList open class MainWindowPresenter(callback: FinTsClientCallback) { @@ -23,6 +25,8 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { protected val bankDataMapper = BankDataMapper() + protected val accounts = mutableMapOf() + protected val bookedTransactions = mutableSetOf() // TODO: map by account protected val unbookedTransactions = mutableSetOf() @@ -38,6 +42,8 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { finTsClient.checkIfAccountExistsAsync(bank, customer) { response -> if (response.isSuccessful) { + accounts.put(customer, bank) + callAccountAddedListeners(bank, customer) } @@ -47,18 +53,32 @@ open class MainWindowPresenter(callback: FinTsClientCallback) { open fun getAccountTransactionsAsync(bank: BankData, customer: CustomerData, + callback: (GetTransactionsResponse) -> Unit) { + + getAccountTransactionsAsync(bank, customer, null, callback) + } + + open fun getAccountTransactionsAsync(bank: BankData, customer: CustomerData, fromDate: Date?, callback: (GetTransactionsResponse) -> Unit) { - finTsClient.getTransactionsAsync(GetTransactionsParameter(), bank, customer) { response -> + finTsClient.getTransactionsAsync(GetTransactionsParameter(true, fromDate), bank, customer) { response -> if (response.isSuccessful) { - bookedTransactions.addAll(response.bookedTransactions) + bookedTransactions.addAll(response.bookedTransactions) // TODO: does currently not work, overwrite equals() unbookedTransactions.addAll(response.unbookedTransactions) } - callback(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 -> getAccountTransactionsAsync(entry.value, entry.key, fromDate, callback) } // TODO: this is not a good solution for multiple accounts + } + open fun searchForBankAsync(enteredBankCode: String, callback: (List) -> Unit) { threadPool.runAsync { 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 4388c94d..f4a953fd 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 @@ -5,6 +5,8 @@ import android.arch.lifecycle.ViewModelProviders import android.content.Context import android.os.Bundle import android.support.v4.app.Fragment +import android.support.v7.app.AlertDialog +import android.support.v7.app.AppCompatActivity import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.support.v7.widget.SearchView @@ -15,25 +17,26 @@ import net.dankito.banking.fints4java.android.MainActivity import net.dankito.banking.fints4java.android.R import net.dankito.banking.fints4java.android.ui.MainWindowPresenter import net.dankito.banking.fints4java.android.ui.adapter.AccountTransactionAdapter +import net.dankito.banking.fints4java.android.ui.dialogs.BankTransferDialog import net.dankito.fints.model.BankData +import net.dankito.fints.model.BankTransferData import net.dankito.fints.model.CustomerData +import net.dankito.fints.response.client.GetTransactionsResponse import net.dankito.utils.android.extensions.asActivity -import org.slf4j.LoggerFactory +import java.math.BigDecimal class HomeFragment : Fragment() { - companion object { - private val log = LoggerFactory.getLogger(HomeFragment::class.java) - } - - private lateinit var homeViewModel: HomeViewModel private lateinit var mnitmBalance: MenuItem private lateinit var mnitmSearchTransactions: MenuItem + private lateinit var mnitmUpdateTransactions: MenuItem + + private val transactionAdapter = AccountTransactionAdapter() @@ -77,12 +80,24 @@ class HomeFragment : Fragment() { mnitmBalance = menu.findItem(R.id.mnitmBalance) mnitmSearchTransactions = menu.findItem(R.id.mnitmSearchTransactions) + mnitmUpdateTransactions = menu.findItem(R.id.mnitmUpdateTransactions) initSearchView() } } + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.mnitmUpdateTransactions -> { + updateAccountsTransactions() + return true + } + } + + return super.onOptionsItemSelected(item) + } + private fun initSearchView() { context?.asActivity()?.let { context -> (context.getSystemService(Context.SEARCH_SERVICE) as? SearchManager)?.let { searchManager -> @@ -115,16 +130,34 @@ class HomeFragment : Fragment() { private fun retrieveAccountTransactions(bank: BankData, customer: CustomerData) { presenter.getAccountTransactionsAsync(bank, customer) { response -> - context?.asActivity()?.runOnUiThread { + handleGetTransactionsResponse(response) + } + } + + private fun updateAccountsTransactions() { + presenter.updateAccountsTransactionsAsync { response -> + handleGetTransactionsResponse(response) + } + } + + private fun handleGetTransactionsResponse(response: GetTransactionsResponse) { + context?.asActivity()?.let { activity -> + activity.runOnUiThread { if (response.isSuccessful) { transactionAdapter.items = response.bookedTransactions + mnitmSearchTransactions.isVisible = response.bookedTransactions.isNotEmpty() + mnitmUpdateTransactions.isVisible = response.bookedTransactions.isNotEmpty() + response.balance?.let { mnitmBalance.title = it.toString() + mnitmBalance.setVisible(true) } - } - else { - // TODO: show error + } else { + AlertDialog.Builder(activity) + .setMessage(activity.getString(R.string.fragment_home_could_not_retrieve_account_transactions, response.exception ?: response.errorsToShowToUser.joinToString("\n"))) + .setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() } + .show() } } } diff --git a/fints4javaAndroidApp/src/main/res/menu/menu_main.xml b/fints4javaAndroidApp/src/main/res/menu/menu_main.xml index dfac23de..614d06db 100644 --- a/fints4javaAndroidApp/src/main/res/menu/menu_main.xml +++ b/fints4javaAndroidApp/src/main/res/menu/menu_main.xml @@ -5,14 +5,26 @@ + + + android:orderInCategory="500" + android:visible="false" + /> diff --git a/fints4javaAndroidApp/src/main/res/values/strings.xml b/fints4javaAndroidApp/src/main/res/values/strings.xml index 29615b69..2cd1a72e 100644 --- a/fints4javaAndroidApp/src/main/res/values/strings.xml +++ b/fints4javaAndroidApp/src/main/res/values/strings.xml @@ -14,6 +14,10 @@ Home + Update transactions + + Could not retrieve account transactions:\n%s + Bank code: FinTS server address: Customer Id: