UX improvements

This commit is contained in:
Luna712 2023-10-28 17:01:49 -06:00
parent 99a459d001
commit fdabaed367
4 changed files with 85 additions and 44 deletions

View file

@ -3,10 +3,12 @@ package com.lagradost.cloudstream3.utils
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.View
import android.view.inputmethod.EditorInfo
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.widget.doOnTextChanged import androidx.core.widget.doOnTextChanged
import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonProperty
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog
@ -272,18 +274,18 @@ object DataStoreHelper {
binding.lockProfileCheckbox.setOnCheckedChangeListener { _, isChecked -> binding.lockProfileCheckbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) { if (isChecked) {
if (canSetPin) { if (canSetPin) {
showPinInputDialog(context) { pin -> showPinInputDialog(context, null, true) { pin ->
if (pin == null) return@showPinInputDialog
currentEditAccount = currentEditAccount.copy(lockPin = pin) currentEditAccount = currentEditAccount.copy(lockPin = pin)
} }
} }
} else { } else {
if (currentEditAccount.lockPin != null) { if (currentEditAccount.lockPin != null) {
// Ask for the current PIN // Ask for the current PIN
showPinInputDialog(context) { currentPin -> showPinInputDialog(context, currentEditAccount.lockPin, true) { pin ->
if (currentPin != currentEditAccount.lockPin) { if (pin == null || pin != currentEditAccount.lockPin) {
canSetPin = false canSetPin = false
binding.lockProfileCheckbox.isChecked = true binding.lockProfileCheckbox.isChecked = true
binding.lockProfileIncorrect.isVisible = true
} }
} }
} else { } else {
@ -324,15 +326,11 @@ object DataStoreHelper {
// Check if the selected account has a lock PIN set // Check if the selected account has a lock PIN set
if (account.lockPin != null) { if (account.lockPin != null) {
// Prompt for the lock pin // Prompt for the lock pin
showPinInputDialog(context) { enteredPin -> showPinInputDialog(context, account.lockPin, false) { pin ->
if (enteredPin == account.lockPin) { if (pin == null) return@showPinInputDialog
// Pin is correct, unlock the profile // Pin is correct, unlock the profile
setAccount(account, true) setAccount(account, true)
builder.dismissSafe() builder.dismissSafe()
} else {
// PIN is incorrect, display an error message
showToast(R.string.incorrect_pin)
}
} }
} else { } else {
// No lock PIN set, directly set the account // No lock PIN set, directly set the account
@ -374,23 +372,75 @@ object DataStoreHelper {
builder.show() builder.show()
} }
private fun showPinInputDialog(context: Context, callback: (String) -> Unit) { private fun showPinInputDialog(
context: Context,
currentPin: String?,
editAccount: Boolean,
callback: (String?) -> Unit
) {
val binding: LockPinDialogBinding = LockPinDialogBinding.inflate(LayoutInflater.from(context)) val binding: LockPinDialogBinding = LockPinDialogBinding.inflate(LayoutInflater.from(context))
val builder = val builder = AlertDialog.Builder(context, R.style.AlertDialogCustom)
AlertDialog.Builder(context, R.style.AlertDialogCustom) .setView(binding.root)
.setView(binding.root)
builder.setTitle(R.string.enter_pin) val dialog = builder.create()
.setPositiveButton(R.string.ok) { dialog, _ ->
if (editAccount && currentPin != null) {
dialog.setTitle(R.string.enter_current_pin)
} else dialog.setTitle(R.string.enter_pin)
binding.pinEditTextError.visibility = View.GONE
// A flag to track if the PIN is valid
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()
if (enteredPin.length == 4) {
if (currentPin != null) {
if (enteredPin != currentPin) {
binding.pinEditTextError.visibility = View.VISIBLE
binding.pinEditTextError.text = context.getString(R.string.pin_error_incorrect)
isPinValid = false
} else {
binding.pinEditTextError.visibility = View.GONE
isPinValid = true
callback.invoke(enteredPin)
dialog.dismiss()
}
} else {
binding.pinEditTextError.visibility = View.GONE
isPinValid = true
}
} else if (editAccount && currentPin == null) {
binding.pinEditTextError.visibility = View.VISIBLE
binding.pinEditTextError.text = context.getString(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() val enteredPin = binding.pinEditText.text.toString()
callback(enteredPin) callback.invoke(enteredPin)
dialog.dismiss() dialog.dismiss()
} }
.setNegativeButton(R.string.cancel) { dialog, _ -> true
dialog.cancel() }
}
builder.show() dialog.setButton(AlertDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel)) { _, _ ->
callback.invoke(null)
dialog.dismiss()
}
dialog.show()
} }
data class PosDur( data class PosDur(

View file

@ -6,13 +6,6 @@
android:orientation="vertical" android:orientation="vertical"
android:padding="16dp"> android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enter_lock_pin"
android:textSize="18sp"
android:gravity="center"/>
<EditText <EditText
android:id="@+id/pinEditText" android:id="@+id/pinEditText"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -22,4 +15,11 @@
android:inputType="numberPassword" android:inputType="numberPassword"
android:maxLength="4" /> android:maxLength="4" />
<TextView
android:id="@+id/pinEditTextError"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone" />
</LinearLayout> </LinearLayout>

View file

@ -63,14 +63,6 @@
android:layout_marginBottom="60dp" android:layout_marginBottom="60dp"
android:orientation="vertical"> android:orientation="vertical">
<TextView
android:id="@+id/lockProfileIncorrect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="@string/lock_profile_incorrect_current"
android:visibility="gone" />
<EditText <EditText
android:id="@+id/account_name" android:id="@+id/account_name"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -717,10 +717,9 @@
<string name="enter_pin">Enter PIN</string> <string name="enter_pin">Enter PIN</string>
<string name="ok">OK</string> <string name="enter_current_pin">Enter Current PIN</string>
<string name="lock_profile">Lock Profile</string> <string name="lock_profile">Lock Profile</string>
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_lock_pin">Enter Lock PIN</string> <string name="pin_error_incorrect">Incorrect PIN. Please try again.</string>
<string name="incorrect_pin">Incorrect PIN</string> <string name="pin_error_length">PIN must be 4 characters</string>
<string name="lock_profile_incorrect_current">Current PIN entered incorrectly</string>
</resources> </resources>