-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathInputAlertDialog.kt
124 lines (112 loc) · 4.7 KB
/
InputAlertDialog.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Infomaniak Mail - Android
* Copyright (C) 2023-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.alertDialogs
import android.content.Context
import android.widget.Button
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.doAfterTextChanged
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.infomaniak.lib.core.utils.context
import com.infomaniak.lib.core.utils.hideKeyboard
import com.infomaniak.lib.core.utils.showKeyboard
import com.infomaniak.mail.R
import com.infomaniak.mail.databinding.DialogInputBinding
import com.infomaniak.mail.di.IoDispatcher
import com.infomaniak.mail.utils.extensions.trimmedText
import dagger.hilt.android.qualifiers.ActivityContext
import dagger.hilt.android.scopes.ActivityScoped
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.invoke
import kotlinx.coroutines.launch
import javax.inject.Inject
import com.infomaniak.lib.core.R as RCore
@ActivityScoped
open class InputAlertDialog @Inject constructor(
@ActivityContext private val activityContext: Context,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : BaseAlertDialog(activityContext) {
val binding by lazy { DialogInputBinding.inflate(activity.layoutInflater) }
override var alertDialog: AlertDialog = initDialog()
private var errorJob: Job? = null
private var onErrorCheck: (suspend (CharSequence) -> String?)? = null
private var onPositiveButtonClicked: ((String) -> Unit)? = null
private fun initDialog() = with(binding) {
fun Button.checkValidation(text: CharSequence) {
errorJob?.cancel()
errorJob = activity.lifecycleScope.launch(ioDispatcher) {
val error = onErrorCheck?.invoke(text.trim())
if (errorJob?.isActive == true) Dispatchers.Main {
textInputLayout.error = error
isEnabled = error == null
}
}
}
fun AlertDialog.setupOnShowListener() = apply {
setOnShowListener {
showKeyboard()
positiveButton.apply {
initProgress()
isEnabled = false
textInput.doAfterTextChanged {
if (it.isNullOrBlank()) {
errorJob?.cancel()
isEnabled = false
textInputLayout.error = null
} else {
checkValidation(it.trim())
}
}
// We are forced to override the ClickListener to prevent the default one to automatically dismiss the Alert
setOnClickListener {
onPositiveButtonClicked?.invoke(binding.textInput.trimmedText)
positiveButton.hideKeyboard()
startLoading()
}
}
}
}
MaterialAlertDialogBuilder(context)
.setView(root)
.setPositiveButton(R.string.buttonCreate, null)
.setNegativeButton(RCore.string.buttonCancel, null)
.setOnDismissListener {
errorJob?.cancel()
binding.textInput.text?.clear()
}
.create()
.setupOnShowListener()
}
override fun resetCallbacks() {
onPositiveButtonClicked = null
onErrorCheck = null
}
fun show(@StringRes title: Int, @StringRes hint: Int, @StringRes confirmButtonText: Int) = with(binding) {
alertDialog.show()
dialogTitle.setText(title)
textInputLayout.setHint(hint)
positiveButton.setText(confirmButtonText)
}
fun setCallbacks(onPositiveButtonClicked: (String) -> Unit, onErrorCheck: suspend (CharSequence) -> String?) {
this.onPositiveButtonClicked = onPositiveButtonClicked
this.onErrorCheck = onErrorCheck
}
}