mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Fix some things and cleanup
This commit is contained in:
parent
6deaf120a2
commit
fc5f116d81
1 changed files with 37 additions and 36 deletions
|
@ -7,6 +7,8 @@ import android.text.TextWatcher
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.annotation.StringRes
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
|
@ -399,19 +401,39 @@ object DataStoreHelper {
|
||||||
editAccount: Boolean,
|
editAccount: Boolean,
|
||||||
callback: (String?) -> Unit
|
callback: (String?) -> Unit
|
||||||
) {
|
) {
|
||||||
val binding: LockPinDialogBinding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
|
fun TextView.visibleWithText(@StringRes textRes: Int) {
|
||||||
val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
|
visibility = View.VISIBLE
|
||||||
.setView(binding.root)
|
setText(textRes)
|
||||||
|
}
|
||||||
|
|
||||||
val dialog = builder.create()
|
fun View.isVisible() = visibility == View.VISIBLE
|
||||||
|
|
||||||
if (editAccount && currentPin != null) {
|
val binding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
|
||||||
dialog.setTitle(R.string.enter_current_pin)
|
|
||||||
} else dialog.setTitle(R.string.enter_pin)
|
|
||||||
|
|
||||||
binding.pinEditTextError.visibility = View.GONE
|
binding.pinEditTextError.visibility = View.GONE
|
||||||
|
|
||||||
// A flag to track if the PIN is valid
|
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
|
var isPinValid = false
|
||||||
|
|
||||||
binding.pinEditText.addTextChangedListener(object : TextWatcher {
|
binding.pinEditText.addTextChangedListener(object : TextWatcher {
|
||||||
|
@ -419,11 +441,12 @@ object DataStoreHelper {
|
||||||
|
|
||||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
||||||
val enteredPin = s.toString()
|
val enteredPin = s.toString()
|
||||||
if (enteredPin.length == 4) {
|
val isEnteredPinValid = enteredPin.length == 4
|
||||||
if (currentPin != null) {
|
|
||||||
|
if (isEnteredPinValid) {
|
||||||
|
if (isPinSet) {
|
||||||
if (enteredPin != currentPin) {
|
if (enteredPin != currentPin) {
|
||||||
binding.pinEditTextError.visibility = View.VISIBLE
|
binding.pinEditTextError.visibleWithText(R.string.pin_error_incorrect)
|
||||||
binding.pinEditTextError.text = context.getString(R.string.pin_error_incorrect)
|
|
||||||
binding.pinEditText.text = null
|
binding.pinEditText.text = null
|
||||||
isPinValid = false
|
isPinValid = false
|
||||||
} else {
|
} else {
|
||||||
|
@ -437,9 +460,8 @@ object DataStoreHelper {
|
||||||
binding.pinEditTextError.visibility = View.GONE
|
binding.pinEditTextError.visibility = View.GONE
|
||||||
isPinValid = true
|
isPinValid = true
|
||||||
}
|
}
|
||||||
} else if (editAccount && currentPin == null) {
|
} else if (isNewPin) {
|
||||||
binding.pinEditTextError.visibility = View.VISIBLE
|
binding.pinEditTextError.visibleWithText(R.string.pin_error_length)
|
||||||
binding.pinEditTextError.text = context.getString(R.string.pin_error_length)
|
|
||||||
isPinValid = false
|
isPinValid = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -457,27 +479,6 @@ object DataStoreHelper {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only when setting pin
|
|
||||||
if (editAccount && currentPin == null) {
|
|
||||||
dialog.setButton(AlertDialog.BUTTON_POSITIVE, context.getString(R.string.setup_done)) { _, _ ->
|
|
||||||
if (isPinValid) {
|
|
||||||
val enteredPin = binding.pinEditText.text.toString()
|
|
||||||
callback.invoke(enteredPin)
|
|
||||||
dialog.dismiss()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel)) { _, _ ->
|
|
||||||
dialog.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
dialog.setOnDismissListener {
|
|
||||||
if (!isPinValid) {
|
|
||||||
callback.invoke(null)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// We don't want to accidentally have the dialog dismiss when clicking outside of it.
|
// We don't want to accidentally have the dialog dismiss when clicking outside of it.
|
||||||
// That is what the cancel button is for.
|
// That is what the cancel button is for.
|
||||||
dialog.setCanceledOnTouchOutside(false)
|
dialog.setCanceledOnTouchOutside(false)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue