This commit is contained in:
Luna712 2023-10-28 17:30:00 -06:00
parent fdabaed367
commit 7dc07bd407

View file

@ -242,29 +242,23 @@ object DataStoreHelper {
} }
binding.applyBtt.setOnClickListener { binding.applyBtt.setOnClickListener {
val currentAccounts = accounts.toMutableList() if (currentEditAccount.lockPin != null) {
// Ask for the current PIN
val overrideIndex = showPinInputDialog(context, currentEditAccount.lockPin, false) { pin ->
currentAccounts.indexOfFirst { it.keyIndex == currentEditAccount.keyIndex } if (pin == null || pin != currentEditAccount.lockPin) {
binding.lockProfileCheckbox.isChecked = true
// If an account is found that has the same keyIndex, override that one; if not, append it
if (overrideIndex != -1) {
currentAccounts[overrideIndex] = currentEditAccount
} else { } else {
currentAccounts.add(currentEditAccount) // PIN is correct, proceed to update the account
} performAccountUpdate(currentEditAccount)
// Save the current homepage for new accounts
val currentHomePage = DataStoreHelper.currentHomePage
// Set the new default account as well as add the key for the new account
setAccount(currentEditAccount, false)
DataStoreHelper.currentHomePage = currentHomePage
accounts = currentAccounts.toTypedArray()
dialog.dismissSafe() dialog.dismissSafe()
} }
}
} else {
// No lock PIN set, proceed to update the account
performAccountUpdate(currentEditAccount)
dialog.dismissSafe()
}
}
// Handle setting or changing the PIN // Handle setting or changing the PIN
var canSetPin = true var canSetPin = true
@ -297,6 +291,23 @@ object DataStoreHelper {
canSetPin = true canSetPin = true
} }
private fun performAccountUpdate(account: Account) {
val currentAccounts = accounts.toMutableList()
val overrideIndex = currentAccounts.indexOfFirst { it.keyIndex == account.keyIndex }
if (overrideIndex != -1) {
currentAccounts[overrideIndex] = account
} else {
currentAccounts.add(account)
}
val currentHomePage = DataStoreHelper.currentHomePage
setAccount(account, false)
DataStoreHelper.currentHomePage = currentHomePage
accounts = currentAccounts.toTypedArray()
}
private fun getDefaultAccount(context: Context): Account { private fun getDefaultAccount(context: Context): Account {
return accounts.let { currentAccounts -> return accounts.let { currentAccounts ->
currentAccounts.getOrNull(currentAccounts.indexOfFirst { it.keyIndex == 0 }) ?: Account( currentAccounts.getOrNull(currentAccounts.indexOfFirst { it.keyIndex == 0 }) ?: Account(
@ -436,10 +447,15 @@ object DataStoreHelper {
} }
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel)) { _, _ -> dialog.setButton(AlertDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel)) { _, _ ->
callback.invoke(null)
dialog.dismiss() dialog.dismiss()
} }
dialog.setOnDismissListener {
if (!isPinValid) {
callback.invoke(null)
}
}
dialog.show() dialog.show()
} }