mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Apply changes to AccountDialogs
This commit is contained in:
parent
6863cde785
commit
8b5a74b94a
2 changed files with 28 additions and 151 deletions
|
@ -1,115 +0,0 @@
|
||||||
package com.lagradost.cloudstream3.ui.account
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.text.Editable
|
|
||||||
import android.text.TextWatcher
|
|
||||||
import android.view.LayoutInflater
|
|
||||||
import android.view.View
|
|
||||||
import android.view.inputmethod.EditorInfo
|
|
||||||
import android.view.inputmethod.InputMethodManager
|
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.annotation.StringRes
|
|
||||||
import androidx.appcompat.app.AlertDialog
|
|
||||||
import com.lagradost.cloudstream3.R
|
|
||||||
import com.lagradost.cloudstream3.databinding.LockPinDialogBinding
|
|
||||||
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
|
|
||||||
|
|
||||||
object AccountDialog {
|
|
||||||
// TODO add account creation dialog to allow creating accounts directly from AccountSelectActivity
|
|
||||||
|
|
||||||
fun showPinInputDialog(
|
|
||||||
context: Context,
|
|
||||||
currentPin: String?,
|
|
||||||
editAccount: Boolean,
|
|
||||||
callback: (String?) -> Unit
|
|
||||||
) {
|
|
||||||
fun TextView.visibleWithText(@StringRes textRes: Int) {
|
|
||||||
visibility = View.VISIBLE
|
|
||||||
setText(textRes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun View.isVisible() = visibility == View.VISIBLE
|
|
||||||
|
|
||||||
val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
|
|
||||||
|
|
||||||
val isPinSet = currentPin != null
|
|
||||||
val isNewPin = editAccount && !isPinSet
|
|
||||||
val isEditPin = editAccount && isPinSet
|
|
||||||
|
|
||||||
val titleRes = if (isEditPin) R.string.enter_current_pin else R.string.enter_pin
|
|
||||||
|
|
||||||
val dialog = AlertDialog.Builder(context, R.style.AlertDialogCustom)
|
|
||||||
.setView(binding.root)
|
|
||||||
.setTitle(titleRes)
|
|
||||||
.setNegativeButton(R.string.cancel) { _, _ ->
|
|
||||||
callback.invoke(null)
|
|
||||||
}
|
|
||||||
.setOnCancelListener {
|
|
||||||
callback.invoke(null)
|
|
||||||
}
|
|
||||||
.setOnDismissListener {
|
|
||||||
if (binding.pinEditTextError.isVisible()) {
|
|
||||||
callback.invoke(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.create()
|
|
||||||
|
|
||||||
var isPinValid = false
|
|
||||||
|
|
||||||
binding.pinEditText.addTextChangedListener(object : TextWatcher {
|
|
||||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
|
||||||
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
val enteredPin = s.toString()
|
|
||||||
val isEnteredPinValid = enteredPin.length == 4
|
|
||||||
|
|
||||||
if (isEnteredPinValid) {
|
|
||||||
if (isPinSet) {
|
|
||||||
if (enteredPin != currentPin) {
|
|
||||||
binding.pinEditTextError.visibleWithText(R.string.pin_error_incorrect)
|
|
||||||
binding.pinEditText.text = null
|
|
||||||
isPinValid = false
|
|
||||||
} else {
|
|
||||||
binding.pinEditTextError.visibility = View.GONE
|
|
||||||
isPinValid = true
|
|
||||||
|
|
||||||
callback.invoke(enteredPin)
|
|
||||||
dialog.dismissSafe()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
binding.pinEditTextError.visibility = View.GONE
|
|
||||||
isPinValid = true
|
|
||||||
}
|
|
||||||
} else if (isNewPin) {
|
|
||||||
binding.pinEditTextError.visibleWithText(R.string.pin_error_length)
|
|
||||||
isPinValid = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun afterTextChanged(s: Editable?) {}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Detect IME_ACTION_DONE
|
|
||||||
binding.pinEditText.setOnEditorActionListener { _, actionId, _ ->
|
|
||||||
if (actionId == EditorInfo.IME_ACTION_DONE && isPinValid) {
|
|
||||||
val enteredPin = binding.pinEditText.text.toString()
|
|
||||||
callback.invoke(enteredPin)
|
|
||||||
dialog.dismissSafe()
|
|
||||||
}
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't want to accidentally have the dialog dismiss when clicking outside of it.
|
|
||||||
// That is what the cancel button is for.
|
|
||||||
dialog.setCanceledOnTouchOutside(false)
|
|
||||||
|
|
||||||
dialog.show()
|
|
||||||
|
|
||||||
// Auto focus on PIN input and show keyboard
|
|
||||||
binding.pinEditText.requestFocus()
|
|
||||||
binding.pinEditText.postDelayed({
|
|
||||||
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
||||||
imm.showSoftInput(binding.pinEditText, InputMethodManager.SHOW_IMPLICIT)
|
|
||||||
}, 200)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,10 +3,8 @@ package com.lagradost.cloudstream3.ui.account
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.DialogInterface
|
import android.content.DialogInterface
|
||||||
import android.text.Editable
|
import android.text.Editable
|
||||||
import android.text.TextWatcher
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
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
|
||||||
|
@ -24,6 +22,7 @@ import com.lagradost.cloudstream3.utils.DataStoreHelper
|
||||||
import com.lagradost.cloudstream3.utils.DataStoreHelper.getDefaultAccount
|
import com.lagradost.cloudstream3.utils.DataStoreHelper.getDefaultAccount
|
||||||
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
|
import com.lagradost.cloudstream3.utils.DataStoreHelper.setAccount
|
||||||
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
|
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
|
||||||
|
import com.lagradost.cloudstream3.utils.UIHelper.showInputMethod
|
||||||
|
|
||||||
object AccountDialogs {
|
object AccountDialogs {
|
||||||
fun showAccountEditDialog(
|
fun showAccountEditDialog(
|
||||||
|
@ -178,6 +177,8 @@ object AccountDialogs {
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
var isPinValid = false
|
||||||
|
|
||||||
val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
|
val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
|
||||||
.setView(binding.root)
|
.setView(binding.root)
|
||||||
.setTitle(titleRes)
|
.setTitle(titleRes)
|
||||||
|
@ -188,7 +189,7 @@ object AccountDialogs {
|
||||||
callback.invoke(null)
|
callback.invoke(null)
|
||||||
}
|
}
|
||||||
.setOnDismissListener {
|
.setOnDismissListener {
|
||||||
if (binding.pinEditTextError.isVisible) {
|
if (!isPinValid) {
|
||||||
callback.invoke(null)
|
callback.invoke(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,8 +197,8 @@ object AccountDialogs {
|
||||||
if (isNewPin) {
|
if (isNewPin) {
|
||||||
if (errorText != null) binding.pinEditTextError.visibleWithText(errorText)
|
if (errorText != null) binding.pinEditTextError.visibleWithText(errorText)
|
||||||
builder.setPositiveButton(R.string.setup_done) { _, _ ->
|
builder.setPositiveButton(R.string.setup_done) { _, _ ->
|
||||||
if (binding.pinEditTextError.isVisible) {
|
if (!isPinValid) {
|
||||||
// If the done button is pressed and there is a error,
|
// If the done button is pressed and there is an error,
|
||||||
// ask again, and mention the error that caused this.
|
// ask again, and mention the error that caused this.
|
||||||
showPinInputDialog(
|
showPinInputDialog(
|
||||||
context = binding.root.context,
|
context = binding.root.context,
|
||||||
|
@ -215,13 +216,8 @@ object AccountDialogs {
|
||||||
|
|
||||||
val dialog = builder.create()
|
val dialog = builder.create()
|
||||||
|
|
||||||
var isPinValid = false
|
binding.pinEditText.doOnTextChanged { text, _, _, _ ->
|
||||||
|
val enteredPin = text.toString()
|
||||||
binding.pinEditText.addTextChangedListener(object : TextWatcher {
|
|
||||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
|
||||||
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
||||||
val enteredPin = s.toString()
|
|
||||||
val isEnteredPinValid = enteredPin.length == 4
|
val isEnteredPinValid = enteredPin.length == 4
|
||||||
|
|
||||||
if (isEnteredPinValid) {
|
if (isEnteredPinValid) {
|
||||||
|
@ -247,9 +243,6 @@ object AccountDialogs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
@ -269,8 +262,7 @@ object AccountDialogs {
|
||||||
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue