From 9facb3076e33cec5e09ed5b0c7691b860dd15ee8 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Mon, 27 Apr 2020 15:35:05 +0200 Subject: [PATCH] Backport #1006 New filter dialog gets closed if advanced dialog is edited before filter text --- .../twidere/extension/DialogExtensions.kt | 6 +++- .../fragment/filter/AddEditItemFragment.kt | 31 +++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/DialogExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/DialogExtensions.kt index 9818bbdcf..5bc62bd74 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/DialogExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/DialogExtensions.kt @@ -3,6 +3,7 @@ package org.mariotaku.twidere.extension import android.app.Dialog import android.content.DialogInterface.* import android.content.res.ColorStateList +import android.widget.Button import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog import org.mariotaku.chameleon.Chameleon @@ -54,4 +55,7 @@ fun T.onShow(action: (dialog: T) -> Unit) { @Suppress("UNCHECKED_CAST") action(dialog as T) } -} \ No newline at end of file +} + +inline val AlertDialog.positiveButton: Button get() = getButton(BUTTON_POSITIVE) +inline val AlertDialog.negativeButton: Button get() = getButton(BUTTON_NEGATIVE) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/AddEditItemFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/AddEditItemFragment.kt index a41cf4363..0ce37e5e9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/AddEditItemFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/filter/AddEditItemFragment.kt @@ -22,7 +22,6 @@ package org.mariotaku.twidere.fragment.filter import android.accounts.AccountManager import android.app.Dialog import android.content.Context -import android.content.DialogInterface import android.net.Uri import android.os.Bundle import androidx.appcompat.app.AlertDialog @@ -50,7 +49,7 @@ import org.mariotaku.twidere.model.util.AccountUtils import org.mariotaku.twidere.provider.TwidereDataStore.Filters import org.mariotaku.twidere.util.premium.ExtraFeaturesService -class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListener { +class AddEditItemFragment : BaseDialogFragment() { private val contentUri: Uri get() = arguments?.getParcelable(EXTRA_URI)!! @@ -99,20 +98,19 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene advancedCollapseIndicator.rotation = if (value) 90f else 0f } - override fun onClick(dialog: DialogInterface, which: Int) { - dialog as AlertDialog - when (which) { - DialogInterface.BUTTON_POSITIVE -> { - val scope = dialog.scopes ?: return - if (!canEditValue) { - saveScopeOnly(scope) - } else { - val value = dialog.value ?: return - saveItem(value, scope) - } + private fun handlePositiveClick(button: View) { + val scope = dialog?.scopes ?: return + if (!canEditValue) { + saveScopeOnly(scope) + } else { + val value = dialog!!.value?.takeIf(String::isNotEmpty) + if (value == null) { + dialog!!.editText.error = getString(R.string.hint_error_field_required) + return } + saveItem(value, scope) } - + dismiss() } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -124,8 +122,8 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene } else { builder.setTitle(R.string.action_add_filter_rule) } - builder.setPositiveButton(android.R.string.ok, this) - builder.setNegativeButton(android.R.string.cancel, this) + builder.setPositiveButton(android.R.string.ok, null) + builder.setNegativeButton(android.R.string.cancel, null) val dialog = builder.create() dialog.applyOnShow { applyTheme() @@ -143,6 +141,7 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene advancedToggle.setOnClickListener { advancedExpanded = !advancedExpanded } + positiveButton.setOnClickListener(this@AddEditItemFragment::handlePositiveClick) advancedContainer.children.filter { it is CheckBox }.forEach { val checkBox = it as CheckBox checkBox.setOnClickListener onClick@ {