Skip to content

Commit 1c3f52e

Browse files
committed
Extract AlertDialog extenstion functions to override click listener
1 parent e2bf8dc commit 1c3f52e

File tree

7 files changed

+164
-108
lines changed

7 files changed

+164
-108
lines changed

app/src/main/java/org/fcitx/fcitx5/android/ui/common/BaseDynamicListUi.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
2626
import com.google.android.material.snackbar.BaseTransientBottomBar
2727
import com.google.android.material.snackbar.Snackbar
2828
import org.fcitx.fcitx5.android.R
29+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
30+
import org.fcitx.fcitx5.android.utils.str
2931
import splitties.dimensions.dp
3032
import splitties.resources.drawable
3133
import splitties.resources.styledColor
@@ -256,25 +258,24 @@ abstract class BaseDynamicListUi<T>(
256258
rightOfParent(dp(20))
257259
})
258260
}
259-
val dialog = AlertDialog.Builder(ctx)
261+
AlertDialog.Builder(ctx)
260262
.setTitle(title)
261263
.setView(layout)
262-
.setPositiveButton(android.R.string.ok) { _, _ ->
263-
}
264-
.setNegativeButton(android.R.string.cancel) { dialog, _ ->
265-
dialog.cancel()
266-
}
264+
.setPositiveButton(android.R.string.ok, null)
265+
.setNegativeButton(android.R.string.cancel, null)
267266
.show()
268-
editText.requestFocus()
269-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
270-
val t = editText.editableText.toString()
271-
if (mode.validator(t)) {
272-
block(mode.converter(t))
273-
dialog.dismiss()
274-
} else {
275-
editText.error = ctx.getString(R.string.invalid_value)
267+
.onPositiveButtonClick {
268+
val str = editText.str
269+
if (mode.validator(str)) {
270+
editText.error = null
271+
block(mode.converter(str))
272+
true
273+
} else {
274+
editText.error = ctx.getString(R.string.invalid_value)
275+
false
276+
}
276277
}
277-
}
278+
editText.requestFocus()
278279
}
279280

280281
protected val recyclerView = recyclerView {

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/PinyinCustomPhraseFragment.kt

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.fcitx.fcitx5.android.ui.common.OnItemChangedListener
2727
import org.fcitx.fcitx5.android.ui.main.MainViewModel
2828
import org.fcitx.fcitx5.android.utils.NaiveDustman
2929
import org.fcitx.fcitx5.android.utils.materialTextInput
30+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
3031
import org.fcitx.fcitx5.android.utils.str
3132
import splitties.views.dsl.core.add
3233
import splitties.views.dsl.core.lParams
@@ -128,33 +129,33 @@ class PinyinCustomPhraseFragment : Fragment(), OnItemChangedListener<PinyinCusto
128129
add(orderLayout, lParams(matchParent))
129130
add(phraseLayout, lParams(matchParent))
130131
}
131-
val dialog = AlertDialog.Builder(context)
132+
AlertDialog.Builder(context)
132133
.setTitle(title)
133134
.setView(layout)
134135
.setPositiveButton(android.R.string.ok, null)
135136
.setNegativeButton(android.R.string.cancel, null)
136137
.show()
137-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener onClick@{
138-
val key = keyField.str
139-
if (key.isBlank()) {
140-
keyField.error = getString(R.string._cannot_be_empty, keyLabel)
141-
keyField.requestFocus()
142-
return@onClick
143-
} else {
144-
keyField.error = null
145-
}
146-
val order = orderField.str.toIntOrNull() ?: 1
147-
val phrase = phraseField.str
148-
if (phrase.isEmpty()) {
149-
phraseField.error = getString(R.string._cannot_be_empty, phraseLabel)
150-
phraseField.requestFocus()
151-
return@onClick
152-
} else {
153-
phraseField.error = null
138+
.onPositiveButtonClick onClick@{
139+
val key = keyField.str
140+
if (key.isBlank()) {
141+
keyField.error = getString(R.string._cannot_be_empty, keyLabel)
142+
keyField.requestFocus()
143+
return@onClick false
144+
} else {
145+
keyField.error = null
146+
}
147+
val order = orderField.str.toIntOrNull() ?: 1
148+
val phrase = phraseField.str
149+
if (phrase.isEmpty()) {
150+
phraseField.error = getString(R.string._cannot_be_empty, phraseLabel)
151+
phraseField.requestFocus()
152+
return@onClick false
153+
} else {
154+
phraseField.error = null
155+
}
156+
block(PinyinCustomPhrase(key, order, phrase))
157+
return@onClick true
154158
}
155-
block(PinyinCustomPhrase(key, order, phrase))
156-
dialog.dismiss()
157-
}
158159
}
159160
}
160161
ui.addOnItemChangedListener(this)

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/PunctuationEditorFragment.kt

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.fcitx.fcitx5.android.ui.common.BaseDynamicListUi
1616
import org.fcitx.fcitx5.android.ui.common.OnItemChangedListener
1717
import org.fcitx.fcitx5.android.utils.NaiveDustman
1818
import org.fcitx.fcitx5.android.utils.materialTextInput
19+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
1920
import org.fcitx.fcitx5.android.utils.str
2021
import splitties.views.dsl.core.add
2122
import splitties.views.dsl.core.lParams
@@ -104,32 +105,32 @@ class PunctuationEditorFragment : ProgressFragment(), OnItemChangedListener<Punc
104105
add(mappingLayout, lParams(matchParent))
105106
add(altMappingLayout, lParams(matchParent))
106107
}
107-
val dialog = AlertDialog.Builder(context)
108+
AlertDialog.Builder(context)
108109
.setTitle(title)
109110
.setView(layout)
110111
.setPositiveButton(android.R.string.ok, null)
111112
.setNegativeButton(android.R.string.cancel, null)
112113
.show()
113-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener onClick@{
114-
val key = keyField.str.trim()
115-
if (key.isBlank()) {
116-
keyField.error = getString(R.string._cannot_be_empty, keyDesc)
117-
keyField.requestFocus()
118-
return@onClick
119-
} else {
120-
keyField.error = null
114+
.onPositiveButtonClick onClick@{
115+
val key = keyField.str.trim()
116+
if (key.isBlank()) {
117+
keyField.error = getString(R.string._cannot_be_empty, keyDesc)
118+
keyField.requestFocus()
119+
return@onClick false
120+
} else {
121+
keyField.error = null
122+
}
123+
val mapping = mappingField.str
124+
if (mapping.isBlank()) {
125+
mappingField.error = getString(R.string._cannot_be_empty, mappingDesc)
126+
mappingField.requestFocus()
127+
return@onClick false
128+
} else {
129+
mappingField.error = null
130+
}
131+
block(PunctuationMapEntry(key, mapping, altMappingField.str))
132+
return@onClick true
121133
}
122-
val mapping = mappingField.str
123-
if (mapping.isBlank()) {
124-
mappingField.error = getString(R.string._cannot_be_empty, mappingDesc)
125-
mappingField.requestFocus()
126-
return@onClick
127-
} else {
128-
mappingField.error = null
129-
}
130-
block(PunctuationMapEntry(key, mapping, altMappingField.str))
131-
dialog.dismiss()
132-
}
133134
}
134135
}
135136
resetDustman()

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/QuickPhraseEditFragment.kt

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.fcitx.fcitx5.android.ui.common.BaseDynamicListUi
2121
import org.fcitx.fcitx5.android.ui.common.OnItemChangedListener
2222
import org.fcitx.fcitx5.android.utils.NaiveDustman
2323
import org.fcitx.fcitx5.android.utils.materialTextInput
24+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
2425
import org.fcitx.fcitx5.android.utils.serializable
2526
import org.fcitx.fcitx5.android.utils.str
2627
import splitties.views.dsl.core.add
@@ -75,42 +76,40 @@ class QuickPhraseEditFragment : ProgressFragment(), OnItemChangedListener<QuickP
7576
add(keywordLayout, lParams(matchParent))
7677
add(phraseLayout, lParams(matchParent))
7778
}
78-
val dialog = AlertDialog.Builder(context)
79+
AlertDialog.Builder(context)
7980
.setTitle(title)
8081
.setView(layout)
8182
.setPositiveButton(android.R.string.ok, null)
8283
.setNegativeButton(android.R.string.cancel, null)
8384
.show()
84-
// change OnClickListener after dialog is shown,
85-
// so we can control when to dismiss the dialog
86-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener onClick@{
87-
val keyword = keywordField.str.trim()
88-
// "keyword" cannot contain any black characters
89-
if (keyword.isBlank()) {
90-
keywordField.error = getString(
91-
R.string._cannot_be_empty,
92-
getString(R.string.quickphrase_keyword)
93-
)
94-
keywordField.requestFocus()
95-
return@onClick
96-
} else {
97-
keywordField.error = null
85+
.onPositiveButtonClick onClick@{
86+
val keyword = keywordField.str.trim()
87+
// "keyword" cannot contain any black characters
88+
if (keyword.isBlank()) {
89+
keywordField.error = getString(
90+
R.string._cannot_be_empty,
91+
getString(R.string.quickphrase_keyword)
92+
)
93+
keywordField.requestFocus()
94+
return@onClick false
95+
} else {
96+
keywordField.error = null
97+
}
98+
// "phrase" may contain blank characters
99+
val phrase = phraseField.str
100+
if (phrase.isEmpty()) {
101+
phraseField.error = getString(
102+
R.string._cannot_be_empty,
103+
getString(R.string.quickphrase_phrase)
104+
)
105+
phraseField.requestFocus()
106+
return@onClick false
107+
} else {
108+
phraseField.error = null
109+
}
110+
block(QuickPhraseEntry(keyword, phraseField.str))
111+
return@onClick true
98112
}
99-
// "phrase" may contain blank characters
100-
val phrase = phraseField.str
101-
if (phrase.isEmpty()) {
102-
phraseField.error = getString(
103-
R.string._cannot_be_empty,
104-
getString(R.string.quickphrase_phrase)
105-
)
106-
phraseField.requestFocus()
107-
return@onClick
108-
} else {
109-
phraseField.error = null
110-
}
111-
block(QuickPhraseEntry(keyword, phraseField.str))
112-
dialog.dismiss()
113-
}
114113
}
115114
}
116115
ui.addOnItemChangedListener(this)

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/QuickPhraseListFragment.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.fcitx.fcitx5.android.utils.NaiveDustman
3939
import org.fcitx.fcitx5.android.utils.errorDialog
4040
import org.fcitx.fcitx5.android.utils.materialTextInput
4141
import org.fcitx.fcitx5.android.utils.notificationManager
42+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
4243
import org.fcitx.fcitx5.android.utils.queryFileName
4344
import org.fcitx.fcitx5.android.utils.str
4445
import splitties.resources.drawable
@@ -174,25 +175,25 @@ class QuickPhraseListFragment : Fragment(), OnItemChangedListener<QuickPhrase> {
174175
setPaddingDp(20, 10, 20, 0)
175176
add(inputLayout, lParams(matchParent))
176177
}
177-
val dialog = AlertDialog.Builder(requireContext())
178+
AlertDialog.Builder(requireContext())
178179
.setTitle(R.string.create_new)
179180
.setView(layout)
180181
.setPositiveButton(android.R.string.ok, null)
181182
.setNegativeButton(android.R.string.cancel, null)
182183
.show()
183-
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener onClick@{
184-
val name = editText.str.trim()
185-
if (name.isBlank()) {
186-
editText.error =
187-
getString(R.string._cannot_be_empty, getString(R.string.name))
188-
editText.requestFocus()
189-
return@onClick
190-
} else {
191-
editText.error = null
184+
.onPositiveButtonClick onClick@{
185+
val name = editText.str.trim()
186+
if (name.isBlank()) {
187+
editText.error =
188+
getString(R.string._cannot_be_empty, getString(R.string.name))
189+
editText.requestFocus()
190+
return@onClick false
191+
} else {
192+
editText.error = null
193+
}
194+
ui.addItem(item = QuickPhraseManager.newEmpty(name))
195+
return@onClick true
192196
}
193-
ui.addItem(item = QuickPhraseManager.newEmpty(name))
194-
dialog.dismiss()
195-
}
196197
}
197198

198199
override fun updateFAB() {

app/src/main/java/org/fcitx/fcitx5/android/ui/main/settings/TableInputMethodFragment.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import org.fcitx.fcitx5.android.ui.main.MainViewModel
3434
import org.fcitx.fcitx5.android.utils.NaiveDustman
3535
import org.fcitx.fcitx5.android.utils.errorDialog
3636
import org.fcitx.fcitx5.android.utils.notificationManager
37+
import org.fcitx.fcitx5.android.utils.onPositiveButtonClick
38+
import org.fcitx.fcitx5.android.utils.positiveButton
3739
import org.fcitx.fcitx5.android.utils.queryFileName
3840
import splitties.resources.drawable
3941
import splitties.resources.styledDrawable
@@ -183,20 +185,17 @@ class TableInputMethodFragment : Fragment(), OnItemChangedListener<TableBasedInp
183185
.setOnDismissListener {
184186
(filesSelectionUi.root.parent as? ViewGroup)?.removeView(filesSelectionUi.root)
185187
}
186-
.show().apply {
187-
getButton(AlertDialog.BUTTON_POSITIVE).apply {
188-
// override default button handler to prevent dialog close on click
189-
setOnClickListener {
190-
importConfAndDictUri()
191-
}
192-
isEnabled = false
193-
}
188+
.show()
189+
.onPositiveButtonClick {
190+
positiveButton.isEnabled = false
191+
importConfAndDictUri()
192+
false
194193
}
195194
}
196195

197196
private fun updateFilesSelectionDialogButton(importing: Boolean = false) {
198197
filesSelectionDialog?.apply {
199-
getButton(AlertDialog.BUTTON_POSITIVE).isEnabled =
198+
positiveButton.isEnabled =
200199
if (importing) false else (confUri != null && dictUri != null)
201200
}
202201
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* SPDX-FileCopyrightText: Copyright 2024 Fcitx5 for Android Contributors
4+
*/
5+
6+
package org.fcitx.fcitx5.android.utils
7+
8+
import android.app.AlertDialog
9+
import android.widget.Button
10+
11+
val AlertDialog.positiveButton: Button
12+
get() = getButton(AlertDialog.BUTTON_POSITIVE)
13+
14+
val AlertDialog.negativeButton: Button
15+
get() = getButton(AlertDialog.BUTTON_NEGATIVE)
16+
17+
val AlertDialog.neutralButton: Button
18+
get() = getButton(AlertDialog.BUTTON_NEUTRAL)
19+
20+
/**
21+
* Change positive button listener **AFTER** [AlertDialog.show] has been called.
22+
*
23+
* In the listener: `true` to dismiss the dialog; `false` to keep the dialog open.
24+
*/
25+
fun AlertDialog.onPositiveButtonClick(l: AlertDialog.() -> Boolean?): AlertDialog {
26+
positiveButton.setOnClickListener {
27+
if (l.invoke(this) == true) dismiss()
28+
}
29+
return this
30+
}
31+
32+
/**
33+
* Change negative button listener **AFTER** [AlertDialog.show] has been called.
34+
*
35+
* In the listener: `true` to dismiss the dialog; `false` to keep the dialog open.
36+
*/
37+
fun AlertDialog.onNegativeButtonClick(l: AlertDialog.() -> Boolean): AlertDialog {
38+
negativeButton.setOnClickListener {
39+
if (l.invoke(this)) dismiss()
40+
}
41+
return this
42+
}
43+
44+
/**
45+
* Change neutral button listener **AFTER** [AlertDialog.show] has been called.
46+
*
47+
* In the listener: `true` to dismiss the dialog; `false` to keep the dialog open.
48+
*/
49+
fun AlertDialog.onNeutralButtonClick(l: AlertDialog.() -> Boolean): AlertDialog {
50+
neutralButton.setOnClickListener {
51+
if (l.invoke(this)) dismiss()
52+
}
53+
return this
54+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy