Add done button for when creating new PINs (#742)

* Add done button for when creating new PINs

* Cleanup
This commit is contained in:
Luna712 2023-11-02 17:28:25 -06:00 committed by GitHub
parent 5b0cbbf09f
commit a6786aaf98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,18 +1,17 @@
package com.lagradost.cloudstream3.ui.account package com.lagradost.cloudstream3.ui.account
import android.content.Context import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.TextView import android.widget.TextView
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.databinding.LockPinDialogBinding import com.lagradost.cloudstream3.databinding.LockPinDialogBinding
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
import com.lagradost.cloudstream3.utils.UIHelper.showInputMethod
object AccountDialog { object AccountDialog {
// TODO add account creation dialog to allow creating accounts directly from AccountSelectActivity // TODO add account creation dialog to allow creating accounts directly from AccountSelectActivity
@ -21,14 +20,18 @@ object AccountDialog {
context: Context, context: Context,
currentPin: String?, currentPin: String?,
editAccount: Boolean, editAccount: Boolean,
errorText: String? = null,
callback: (String?) -> Unit callback: (String?) -> Unit
) { ) {
fun TextView.visibleWithText(@StringRes textRes: Int) { fun TextView.visibleWithText(@StringRes textRes: Int) {
visibility = View.VISIBLE isVisible = true
setText(textRes) setText(textRes)
} }
fun View.isVisible() = visibility == View.VISIBLE fun TextView.visibleWithText(text: String?) {
isVisible = true
setText(text)
}
val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context)) val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
@ -38,7 +41,9 @@ object AccountDialog {
val titleRes = if (isEditPin) R.string.enter_current_pin else R.string.enter_pin val titleRes = if (isEditPin) R.string.enter_current_pin else R.string.enter_pin
val dialog = AlertDialog.Builder(context, R.style.AlertDialogCustom) var isPinValid = false
val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
.setView(binding.root) .setView(binding.root)
.setTitle(titleRes) .setTitle(titleRes)
.setNegativeButton(R.string.cancel) { _, _ -> .setNegativeButton(R.string.cancel) { _, _ ->
@ -48,19 +53,35 @@ object AccountDialog {
callback.invoke(null) callback.invoke(null)
} }
.setOnDismissListener { .setOnDismissListener {
if (binding.pinEditTextError.isVisible()) { if (!isPinValid) {
callback.invoke(null) callback.invoke(null)
} }
} }
.create()
var isPinValid = false if (isNewPin) {
if (errorText != null) binding.pinEditTextError.visibleWithText(errorText)
builder.setPositiveButton(R.string.setup_done) { _, _ ->
if (!isPinValid) {
// If the done button is pressed and there is an error,
// ask again, and mention the error that caused this.
showPinInputDialog(
context = binding.root.context,
currentPin = null,
editAccount = true,
errorText = binding.pinEditTextError.text.toString(),
callback = callback
)
} else {
val enteredPin = binding.pinEditText.text.toString()
callback.invoke(enteredPin)
}
}
}
binding.pinEditText.addTextChangedListener(object : TextWatcher { val dialog = builder.create()
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { binding.pinEditText.doOnTextChanged { text, _, _, _ ->
val enteredPin = s.toString() val enteredPin = text.toString()
val isEnteredPinValid = enteredPin.length == 4 val isEnteredPinValid = enteredPin.length == 4
if (isEnteredPinValid) { if (isEnteredPinValid) {
@ -70,14 +91,14 @@ object AccountDialog {
binding.pinEditText.text = null binding.pinEditText.text = null
isPinValid = false isPinValid = false
} else { } else {
binding.pinEditTextError.visibility = View.GONE binding.pinEditTextError.isVisible = false
isPinValid = true isPinValid = true
callback.invoke(enteredPin) callback.invoke(enteredPin)
dialog.dismissSafe() dialog.dismissSafe()
} }
} else { } else {
binding.pinEditTextError.visibility = View.GONE binding.pinEditTextError.isVisible = false
isPinValid = true isPinValid = true
} }
} else if (isNewPin) { } else if (isNewPin) {
@ -86,9 +107,6 @@ object AccountDialog {
} }
} }
override fun afterTextChanged(s: Editable?) {}
})
// Detect IME_ACTION_DONE // Detect IME_ACTION_DONE
binding.pinEditText.setOnEditorActionListener { _, actionId, _ -> binding.pinEditText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE && isPinValid) { if (actionId == EditorInfo.IME_ACTION_DONE && isPinValid) {
@ -108,8 +126,7 @@ object AccountDialog {
// Auto focus on PIN input and show keyboard // Auto focus on PIN input and show keyboard
binding.pinEditText.requestFocus() binding.pinEditText.requestFocus()
binding.pinEditText.postDelayed({ binding.pinEditText.postDelayed({
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager showInputMethod(binding.pinEditText)
imm.showSoftInput(binding.pinEditText, InputMethodManager.SHOW_IMPLICIT)
}, 200) }, 200)
} }
} }