fix: Credential allow action #2253

This commit is contained in:
J-Jamet
2025-10-29 16:44:48 +01:00
parent d99edb6b4d
commit 6d452fa49c

View File

@@ -6,10 +6,12 @@ import android.content.pm.PackageManager
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@@ -57,37 +59,58 @@ abstract class DatabaseActivity : StylishActivity(), DatabaseRetrieval {
} }
} }
/**
* Useful to only waiting for the activity result and prevent any parallel action
*/
protected var mAllowViewModelAction = true
/** /**
* Utility activity result launcher, * Utility activity result launcher,
* Used recursively, close each activity with return data * Used recursively, close each activity with return data
*/ */
protected open var mCredentialActivityResultLauncher: ActivityResultLauncher<Intent>? = protected var mCredentialActivityResultLauncher: DatabaseActivityResultLauncher =
DatabaseActivityResultLauncher(
registerForActivityResult( registerForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) {
setActivityResult(
lockDatabase = false,
resultCode = it.resultCode,
data = it.data
)
}
)
/**
* Custom ActivityResultLauncher to manage the database action
*/
protected class DatabaseActivityResultLauncher(
val builder: ActivityResultLauncher<Intent>
) : ActivityResultLauncher<Intent>() {
/**
* Useful to only waiting for the activity result and prevent any parallel action
*/
var allowAction = true
override fun launch(
input: Intent?,
options: ActivityOptionsCompat?
) { ) {
setActivityResult( allowAction = false
lockDatabase = false, builder.launch(input, options)
resultCode = it.resultCode,
data = it.data
)
} }
get() {
// Prevent parallel action override fun unregister() {
mAllowViewModelAction = false builder.unregister()
return field
} }
override fun getContract(): ActivityResultContract<Intent?, *> {
return builder.getContract()
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
mDatabaseViewModel.actionState.collect { uiState -> mDatabaseViewModel.actionState.collect { uiState ->
if (mAllowViewModelAction) { if (mCredentialActivityResultLauncher.allowAction) {
when (uiState) { when (uiState) {
is DatabaseViewModel.ActionState.Wait -> {} is DatabaseViewModel.ActionState.Wait -> {}
is DatabaseViewModel.ActionState.OnDatabaseReloaded -> { is DatabaseViewModel.ActionState.OnDatabaseReloaded -> {
@@ -147,7 +170,7 @@ abstract class DatabaseActivity : StylishActivity(), DatabaseRetrieval {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.RESUMED) { repeatOnLifecycle(Lifecycle.State.RESUMED) {
mDatabaseViewModel.databaseState.collect { database -> mDatabaseViewModel.databaseState.collect { database ->
if (mAllowViewModelAction) { if (mCredentialActivityResultLauncher.allowAction) {
// Nullable function // Nullable function
onUnknownDatabaseRetrieved(database) onUnknownDatabaseRetrieved(database)
database?.let { database?.let {