Backport #1006 New filter dialog gets closed if advanced dialog is edited before filter text

This commit is contained in:
Michel Roux 2020-04-27 15:35:05 +02:00
parent e2acc418d4
commit 9facb3076e
2 changed files with 20 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package org.mariotaku.twidere.extension
import android.app.Dialog import android.app.Dialog
import android.content.DialogInterface.* import android.content.DialogInterface.*
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.widget.Button
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import org.mariotaku.chameleon.Chameleon import org.mariotaku.chameleon.Chameleon
@ -55,3 +56,6 @@ fun <T : Dialog> T.onShow(action: (dialog: T) -> Unit) {
action(dialog as T) action(dialog as T)
} }
} }
inline val AlertDialog.positiveButton: Button get() = getButton(BUTTON_POSITIVE)
inline val AlertDialog.negativeButton: Button get() = getButton(BUTTON_NEGATIVE)

View File

@ -22,7 +22,6 @@ package org.mariotaku.twidere.fragment.filter
import android.accounts.AccountManager import android.accounts.AccountManager
import android.app.Dialog import android.app.Dialog
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AlertDialog 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.provider.TwidereDataStore.Filters
import org.mariotaku.twidere.util.premium.ExtraFeaturesService import org.mariotaku.twidere.util.premium.ExtraFeaturesService
class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListener { class AddEditItemFragment : BaseDialogFragment() {
private val contentUri: Uri private val contentUri: Uri
get() = arguments?.getParcelable(EXTRA_URI)!! get() = arguments?.getParcelable(EXTRA_URI)!!
@ -99,20 +98,19 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene
advancedCollapseIndicator.rotation = if (value) 90f else 0f advancedCollapseIndicator.rotation = if (value) 90f else 0f
} }
override fun onClick(dialog: DialogInterface, which: Int) { private fun handlePositiveClick(button: View) {
dialog as AlertDialog val scope = dialog?.scopes ?: return
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
val scope = dialog.scopes ?: return
if (!canEditValue) { if (!canEditValue) {
saveScopeOnly(scope) saveScopeOnly(scope)
} else { } else {
val value = dialog.value ?: return val value = dialog!!.value?.takeIf(String::isNotEmpty)
if (value == null) {
dialog!!.editText.error = getString(R.string.hint_error_field_required)
return
}
saveItem(value, scope) saveItem(value, scope)
} }
} dismiss()
}
} }
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@ -124,8 +122,8 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene
} else { } else {
builder.setTitle(R.string.action_add_filter_rule) builder.setTitle(R.string.action_add_filter_rule)
} }
builder.setPositiveButton(android.R.string.ok, this) builder.setPositiveButton(android.R.string.ok, null)
builder.setNegativeButton(android.R.string.cancel, this) builder.setNegativeButton(android.R.string.cancel, null)
val dialog = builder.create() val dialog = builder.create()
dialog.applyOnShow { dialog.applyOnShow {
applyTheme() applyTheme()
@ -143,6 +141,7 @@ class AddEditItemFragment : BaseDialogFragment(), DialogInterface.OnClickListene
advancedToggle.setOnClickListener { advancedToggle.setOnClickListener {
advancedExpanded = !advancedExpanded advancedExpanded = !advancedExpanded
} }
positiveButton.setOnClickListener(this@AddEditItemFragment::handlePositiveClick)
advancedContainer.children.filter { it is CheckBox }.forEach { advancedContainer.children.filter { it is CheckBox }.forEach {
val checkBox = it as CheckBox val checkBox = it as CheckBox
checkBox.setOnClickListener onClick@ { checkBox.setOnClickListener onClick@ {