Moved filtering banks to MainWindowPresenter; added preloading banks

This commit is contained in:
dankl 2019-12-31 01:27:02 +01:00 committed by dankito
parent 149d409dbc
commit 57d7dac613
7 changed files with 37 additions and 23 deletions

View File

@ -154,16 +154,28 @@ open class MainWindowPresenter(protected val base64Service: IBase64Service,
}
open fun searchForBankAsync(enteredBankCode: String, callback: (List<BankInfo>) -> Unit) {
open fun preloadBanksAsync() {
searchBanksByBankCodeAsync("1") { }
}
open fun searchBanksByBankCodeAsync(enteredBankCode: String, callback: (List<BankInfo>) -> Unit) {
threadPool.runAsync {
callback(searchForBank(enteredBankCode))
callback(searchBanksByBankCode(enteredBankCode))
}
}
open fun searchForBank(enteredBankCode: String): List<BankInfo> {
open fun searchBanksByBankCode(enteredBankCode: String): List<BankInfo> {
return bankFinder.findBankByBankCode(enteredBankCode)
}
open fun searchBanksByNameBankCodeOrCity(query: String?): List<BankInfo> {
if (query == null || query.isEmpty()) {
return bankFinder.getBankList()
}
return bankFinder.findBankByNameBankCodeOrCity(query)
}
open fun searchAccountTransactions(query: String): List<AccountTransaction> {
val queryLowercase = query.trim().toLowerCase()

View File

@ -8,14 +8,14 @@ import android.widget.Filter
import android.widget.Filterable
import kotlinx.android.synthetic.main.list_item_bank_info.view.*
import net.dankito.banking.fints4java.android.R
import net.dankito.banking.fints4java.android.ui.MainWindowPresenter
import net.dankito.banking.fints4java.android.ui.adapter.filter.BankInfoFilter
import net.dankito.fints.banks.BankFinder
import net.dankito.fints.model.BankInfo
import net.dankito.utils.android.extensions.setTintColor
import net.dankito.utils.android.ui.adapter.ListAdapter
open class BankListAdapter(protected val bankFinder: BankFinder = BankFinder()) : ListAdapter<BankInfo>(), Filterable {
open class BankListAdapter(protected val presenter: MainWindowPresenter) : ListAdapter<BankInfo>(), Filterable {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
@ -46,7 +46,7 @@ open class BankListAdapter(protected val bankFinder: BankFinder = BankFinder())
override fun getFilter(): Filter {
return BankInfoFilter(bankFinder) {
return BankInfoFilter(presenter) {
this.setItems(it)
}
}

View File

@ -1,25 +1,19 @@
package net.dankito.banking.fints4java.android.ui.adapter.filter
import android.widget.Filter
import net.dankito.fints.banks.BankFinder
import net.dankito.banking.fints4java.android.ui.MainWindowPresenter
import net.dankito.fints.model.BankInfo
open class BankInfoFilter(protected val bankFinder: BankFinder,
open class BankInfoFilter(protected val presenter: MainWindowPresenter,
protected val publishResultsCallback: (List<BankInfo>) -> Unit) : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val filteredBanks = presenter.searchBanksByNameBankCodeOrCity(constraint?.toString())
val results = FilterResults()
constraint?.let {
results.values = bankFinder.findBankByNameBankCodeOrCity(it.toString())
}
?: run {
results.values = bankFinder.getBankList()
}
results.count = (results.values as List<*>).size
results.values = filteredBanks
return results
}

View File

@ -34,7 +34,7 @@ open class AddAccountDialog : DialogFragment() {
protected lateinit var presenter: MainWindowPresenter
protected val adapter = BankListAdapter() // TODO: set BankFinder
protected lateinit var adapter: BankListAdapter
protected var selectedBank: BankInfo? = null
@ -42,6 +42,9 @@ open class AddAccountDialog : DialogFragment() {
fun show(activity: AppCompatActivity, presenter: MainWindowPresenter, fullscreen: Boolean = false) {
this.presenter = presenter
presenter.preloadBanksAsync()
this.adapter = BankListAdapter(presenter)
val style = if(fullscreen) R.style.FullscreenDialogWithStatusBar else R.style.Dialog
setStyle(STYLE_NORMAL, style)

View File

@ -177,7 +177,7 @@ open class BankTransferDialog : DialogFragment() {
protected open fun tryToGetBicFromIban(enteredText: CharSequence) {
if (enteredText.length >= 12) { // first two characters are country code, 3rd and 4th character are checksum
if (enteredText.startsWith("DE", true)) {
presenter.searchForBankAsync(enteredText.substring(4)) { foundBanks ->
presenter.searchBanksByBankCodeAsync(enteredText.substring(4)) { foundBanks ->
if (foundBanks.isNotEmpty()) {
context?.asActivity()?.runOnUiThread {
edtxtRemitteeBic.setText(foundBanks.first().bic)

View File

@ -18,10 +18,7 @@
android:layout_width="match_parent"
android:layout_height="@dimen/dialog_add_account_edit_text_height"
android:inputType="text"
>
<requestFocus />
</AutoCompleteTextView>
/>
</android.support.design.widget.TextInputLayout>

View File

@ -16,10 +16,18 @@ open class BankFinder {
open fun findBankByBankCode(query: String): List<BankInfo> {
if (query.isEmpty()) {
return getBankList()
}
return getBankList().filter { it.bankCode.startsWith(query) }
}
open fun findBankByNameBankCodeOrCity(query: String): List<BankInfo> {
if (query.isEmpty()) {
return getBankList()
}
val queryLowerCase = query.toLowerCase()
return getBankList().filter {