From fc94b47fca887d1f463c1e86f2bef288869f4f43 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 14 May 2020 00:02:51 +0200 Subject: [PATCH] Implemented closing Autocomplete popup on back button press --- .../android/ui/dialogs/AddAccountDialog.kt | 2 ++ .../android/ui/dialogs/TransferMoneyDialog.kt | 2 ++ .../ui/extensions/AutocompleteExtensions.kt | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/extensions/AutocompleteExtensions.kt diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt index 1ad53100..b2a5a393 100644 --- a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/AddAccountDialog.kt @@ -20,6 +20,7 @@ import kotlinx.android.synthetic.main.dialog_add_account.view.* import net.dankito.banking.fints4java.android.R import net.dankito.banking.fints4java.android.di.BankingComponent import net.dankito.banking.fints4java.android.ui.adapter.presenter.BankInfoPresenter +import net.dankito.banking.fints4java.android.ui.extensions.closePopupOnBackButtonPress import net.dankito.banking.fints4java.android.util.StandardAutocompleteCallback import net.dankito.banking.ui.model.responses.AddAccountResponse import net.dankito.banking.ui.presenter.BankingPresenter @@ -87,6 +88,7 @@ open class AddAccountDialog : DialogFragment() { .with(autocompleteCallback) .with(BankInfoPresenter(presenter, rootView.context)) .build() + .closePopupOnBackButtonPress(dialog) } protected open fun addAccount() { diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt index a3b2e6f7..6eead0df 100644 --- a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/dialogs/TransferMoneyDialog.kt @@ -20,6 +20,7 @@ import net.dankito.banking.fints4java.android.di.BankingComponent import net.dankito.banking.fints4java.android.ui.adapter.BankAccountsAdapter import net.dankito.banking.fints4java.android.ui.adapter.presenter.RemitteePresenter import net.dankito.banking.fints4java.android.ui.extensions.addEnterPressedListener +import net.dankito.banking.fints4java.android.ui.extensions.closePopupOnBackButtonPress import net.dankito.banking.fints4java.android.ui.listener.ListItemSelectedListener import net.dankito.banking.fints4java.android.util.StandardAutocompleteCallback import net.dankito.banking.fints4java.android.util.StandardTextWatcher @@ -182,6 +183,7 @@ open class TransferMoneyDialog : DialogFragment() { .with(autocompleteCallback) .with(RemitteePresenter(remitteeSearcher, edtxtRemitteeName.context)) .build() + .closePopupOnBackButtonPress(dialog) } diff --git a/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/extensions/AutocompleteExtensions.kt b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/extensions/AutocompleteExtensions.kt new file mode 100644 index 00000000..8ef1b7c4 --- /dev/null +++ b/ui/BankingAndroidApp/src/main/java/net/dankito/banking/fints4java/android/ui/extensions/AutocompleteExtensions.kt @@ -0,0 +1,28 @@ +package net.dankito.banking.fints4java.android.ui.extensions + +import android.app.Dialog +import android.view.KeyEvent +import com.otaliastudios.autocomplete.Autocomplete + + +/** + * This will not work if Dialog is null! + * + * Making the Dialog parameter nullable is just for convienience to be able to call + * Autocomplete + * .on<>(view) + * .build() + * .closePopupOnBackButtonPress(dialog) + */ +fun Autocomplete<*>.closePopupOnBackButtonPress(dialog: Dialog?) { + dialog?.setOnKeyListener { _, keyCode, _ -> + if (keyCode == KeyEvent.KEYCODE_BACK) { + if (this.isPopupShowing) { // close autocomplete popup on back button press + this.dismissPopup() + return@setOnKeyListener true + } + } + + false + } +} \ No newline at end of file