mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-20 17:11:32 -08:00
fix: Replace IOActionTask by direct coroutines #2541
This commit is contained in:
6 files changed
+140
-204
No files matched your search
@@ -33,8 +33,11 @@ import com.kunzisoft.keepass.database.element.binary.BinaryData.Companion.BASE64
|
||||
import com.kunzisoft.keepass.model.CipherEncryptDatabase
|
||||
import com.kunzisoft.keepass.services.DeviceUnlockNotificationService
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.SingletonHolderParameter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class CipherDatabaseAction(context: Context) {
|
||||
|
||||
@@ -161,8 +164,8 @@ class CipherDatabaseAction(context: Context) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val cipherDatabase = withContext(Dispatchers.IO) {
|
||||
cipherDatabaseDao.getByDatabaseUri(databaseUri.toString())
|
||||
?.let { cipherDatabaseEntity ->
|
||||
CipherEncryptDatabase().apply {
|
||||
@@ -177,15 +180,13 @@ class CipherDatabaseAction(context: Context) {
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
{ cipherDatabase ->
|
||||
cipherDatabaseResultListener?.invoke(cipherDatabase) ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseRetrieved(databaseUri, cipherDatabase)
|
||||
}
|
||||
}
|
||||
cipherDatabaseResultListener?.invoke(cipherDatabase) ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseRetrieved(databaseUri, cipherDatabase)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,8 +230,8 @@ class CipherDatabaseAction(context: Context) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
val cipherDatabaseRetrieve =
|
||||
cipherDatabaseDao.getByDatabaseUri(cipherDatabaseEntity.databaseUri)
|
||||
// Update values if element not yet in the database
|
||||
@@ -239,15 +240,13 @@ class CipherDatabaseAction(context: Context) {
|
||||
} else {
|
||||
cipherDatabaseDao.update(cipherDatabaseEntity)
|
||||
}
|
||||
},
|
||||
{
|
||||
cipherDatabaseResultListener?.invoke() ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseAddedOrUpdated(cipherEncryptDatabase)
|
||||
}
|
||||
}
|
||||
cipherDatabaseResultListener?.invoke() ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseAddedOrUpdated(cipherEncryptDatabase)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,18 +263,16 @@ class CipherDatabaseAction(context: Context) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
cipherDatabaseDao.deleteByDatabaseUri(databaseUri.toString())
|
||||
},
|
||||
{
|
||||
cipherDatabaseResultListener?.invoke() ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseDeleted(databaseUri)
|
||||
}
|
||||
}
|
||||
cipherDatabaseResultListener?.invoke() ?: run {
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onCipherDatabaseDeleted(databaseUri)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
reloadPreferences()
|
||||
}
|
||||
@@ -287,11 +284,11 @@ class CipherDatabaseAction(context: Context) {
|
||||
}
|
||||
}
|
||||
// To erase the residues
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
cipherDatabaseDao.deleteAll()
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
mDatabaseListeners.forEach { listener ->
|
||||
listener.onAllCipherDatabasesDeleted()
|
||||
}
|
||||
|
||||
@@ -25,11 +25,14 @@ import android.util.Log
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.SingletonHolderParameter
|
||||
import com.kunzisoft.keepass.utils.decodeUri
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
|
||||
@@ -40,13 +43,14 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
databaseUri: Uri,
|
||||
databaseFileResult: (DatabaseFile?) -> Unit
|
||||
) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
val fileDatabaseHistoryEntity =
|
||||
databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
val fileDatabaseInfo = FileDatabaseInfo(
|
||||
applicationContext,
|
||||
databaseUri)
|
||||
databaseUri,
|
||||
)
|
||||
DatabaseFile(
|
||||
databaseUri = databaseUri,
|
||||
keyFileUri = fileDatabaseHistoryEntity?.keyFileUri?.parseUri(),
|
||||
@@ -54,48 +58,48 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
readOnly = fileDatabaseHistoryEntity?.readOnly,
|
||||
userVerification = fileDatabaseHistoryEntity?.userVerification,
|
||||
databaseDecodedPath = fileDatabaseHistoryEntity?.databaseUri?.decodeUri(),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity?.databaseAlias ?: ""),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(
|
||||
fileDatabaseHistoryEntity?.databaseAlias ?: "",
|
||||
),
|
||||
databaseFileExists = fileDatabaseInfo.exists,
|
||||
databaseLastModified = fileDatabaseInfo.getLastModificationString(),
|
||||
databaseSize = fileDatabaseInfo.getSizeString()
|
||||
databaseSize = fileDatabaseInfo.getSizeString(),
|
||||
)
|
||||
},
|
||||
{
|
||||
databaseFileResult.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
databaseFileResult.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
fun getKeyFileUriByDatabaseUri(
|
||||
databaseUri: Uri,
|
||||
keyFileUriResultListener: (Uri?) -> Unit
|
||||
) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
},
|
||||
{
|
||||
it?.let { fileHistoryEntity ->
|
||||
fileHistoryEntity.keyFileUri?.let { keyFileUri ->
|
||||
keyFileUriResultListener.invoke(keyFileUri.parseUri())
|
||||
}
|
||||
} ?: keyFileUriResultListener.invoke(null)
|
||||
}
|
||||
).execute()
|
||||
result?.let { fileHistoryEntity ->
|
||||
fileHistoryEntity.keyFileUri?.let { keyFileUri ->
|
||||
keyFileUriResultListener.invoke(keyFileUri.parseUri())
|
||||
}
|
||||
} ?: keyFileUriResultListener.invoke(null)
|
||||
}
|
||||
}
|
||||
|
||||
fun getDatabaseFileList(databaseFileListResult: (List<DatabaseFile>) -> Unit) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
val hideBrokenLocations =
|
||||
PreferencesUtil.hideBrokenLocations(
|
||||
applicationContext)
|
||||
applicationContext,
|
||||
)
|
||||
// Show only uri accessible
|
||||
val databaseFileListLoaded = mutableListOf<DatabaseFile>()
|
||||
databaseFileHistoryDao.getAll().forEach { fileDatabaseHistoryEntity ->
|
||||
val fileDatabaseInfo = FileDatabaseInfo(
|
||||
applicationContext,
|
||||
fileDatabaseHistoryEntity.databaseUri)
|
||||
fileDatabaseHistoryEntity.databaseUri,
|
||||
)
|
||||
if (hideBrokenLocations && fileDatabaseInfo.exists
|
||||
|| !hideBrokenLocations
|
||||
) {
|
||||
@@ -103,26 +107,26 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
DatabaseFile(
|
||||
databaseUri = fileDatabaseHistoryEntity.databaseUri.parseUri(),
|
||||
keyFileUri = fileDatabaseHistoryEntity.keyFileUri?.parseUri(),
|
||||
hardwareKey = HardwareKey.getHardwareKeyFromString(fileDatabaseHistoryEntity.hardwareKey),
|
||||
hardwareKey = HardwareKey.getHardwareKeyFromString(
|
||||
fileDatabaseHistoryEntity.hardwareKey,
|
||||
),
|
||||
readOnly = fileDatabaseHistoryEntity.readOnly,
|
||||
userVerification = fileDatabaseHistoryEntity.userVerification,
|
||||
databaseDecodedPath = fileDatabaseHistoryEntity.databaseUri.decodeUri(),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity.databaseAlias),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(
|
||||
fileDatabaseHistoryEntity.databaseAlias,
|
||||
),
|
||||
databaseFileExists = fileDatabaseInfo.exists,
|
||||
databaseLastModified = fileDatabaseInfo.getLastModificationString(),
|
||||
databaseSize = fileDatabaseInfo.getSizeString()
|
||||
databaseSize = fileDatabaseInfo.getSizeString(),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
databaseFileListLoaded
|
||||
},
|
||||
{ databaseFileList ->
|
||||
databaseFileList?.let {
|
||||
databaseFileListResult.invoke(it)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
databaseFileListResult.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
fun addOrUpdateDatabaseUri(
|
||||
@@ -142,8 +146,8 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
databaseFileToAddOrUpdate: DatabaseFile,
|
||||
databaseFileAddedOrUpdatedResult: ((DatabaseFile?) -> Unit)? = null
|
||||
) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
databaseFileToAddOrUpdate.databaseUri?.let { databaseUri ->
|
||||
// Try to get info in database first
|
||||
val fileDatabaseHistoryRetrieve =
|
||||
@@ -162,8 +166,10 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
?: fileDatabaseHistoryRetrieve?.readOnly,
|
||||
userVerification = databaseFileToAddOrUpdate.userVerification
|
||||
?: fileDatabaseHistoryRetrieve?.userVerification
|
||||
?: PreferencesUtil.isUserVerificationModeEnabledByDefault(applicationContext),
|
||||
updated = System.currentTimeMillis()
|
||||
?: PreferencesUtil.isUserVerificationModeEnabledByDefault(
|
||||
applicationContext,
|
||||
),
|
||||
updated = System.currentTimeMillis(),
|
||||
)
|
||||
|
||||
// Update values if history element not yet in the database
|
||||
@@ -178,8 +184,10 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
}
|
||||
|
||||
val fileDatabaseInfo =
|
||||
FileDatabaseInfo(applicationContext,
|
||||
fileDatabaseHistory.databaseUri)
|
||||
FileDatabaseInfo(
|
||||
applicationContext,
|
||||
fileDatabaseHistory.databaseUri,
|
||||
)
|
||||
DatabaseFile(
|
||||
databaseUri = fileDatabaseHistory.databaseUri.parseUri(),
|
||||
keyFileUri = fileDatabaseHistory.keyFileUri?.parseUri(),
|
||||
@@ -187,25 +195,23 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
readOnly = fileDatabaseHistory.readOnly,
|
||||
userVerification = fileDatabaseHistory.userVerification,
|
||||
databaseDecodedPath = fileDatabaseHistory.databaseUri.decodeUri(),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistory.databaseAlias),
|
||||
databaseAlias = fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistory.databaseAlias),
|
||||
databaseFileExists = fileDatabaseInfo.exists,
|
||||
databaseLastModified = fileDatabaseInfo.getLastModificationString(),
|
||||
databaseSize = fileDatabaseInfo.getSizeString()
|
||||
databaseSize = fileDatabaseInfo.getSizeString(),
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
databaseFileAddedOrUpdatedResult?.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
databaseFileAddedOrUpdatedResult?.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteDatabaseFile(
|
||||
databaseFileToDelete: DatabaseFile,
|
||||
databaseFileDeletedResult: (DatabaseFile?) -> Unit
|
||||
) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
databaseFileToDelete.databaseUri?.let { databaseUri ->
|
||||
databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
?.let { fileDatabaseHistory ->
|
||||
@@ -213,59 +219,53 @@ class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
if (returnValue > 0) {
|
||||
DatabaseFile(
|
||||
databaseUri = fileDatabaseHistory.databaseUri.parseUri(),
|
||||
keyFileUri = fileDatabaseHistory.keyFileUri?.parseUri(),
|
||||
hardwareKey = HardwareKey.getHardwareKeyFromString(fileDatabaseHistory.hardwareKey),
|
||||
keyFileUri = fileDatabaseHistory.keyFileUri?.parseUri(),
|
||||
hardwareKey = HardwareKey.getHardwareKeyFromString(
|
||||
fileDatabaseHistory.hardwareKey,
|
||||
),
|
||||
readOnly = fileDatabaseHistory.readOnly,
|
||||
userVerification = fileDatabaseHistory.userVerification,
|
||||
databaseDecodedPath = fileDatabaseHistory.databaseUri.decodeUri(),
|
||||
databaseAlias = databaseFileToDelete.databaseAlias
|
||||
databaseAlias = databaseFileToDelete.databaseAlias,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
databaseFileDeletedResult.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
databaseFileDeletedResult.invoke(result)
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteKeyFileByDatabaseUri(
|
||||
databaseUri: Uri,
|
||||
result: (() ->Unit)? = null
|
||||
result: (() -> Unit)? = null
|
||||
) {
|
||||
IOActionTask(
|
||||
{
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
databaseFileHistoryDao.deleteKeyFileByDatabaseUri(databaseUri.toString())
|
||||
},
|
||||
{
|
||||
result?.invoke()
|
||||
}
|
||||
).execute()
|
||||
result?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAllKeyFiles(result: (() ->Unit)? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
fun deleteAllKeyFiles(result: (() -> Unit)? = null) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
databaseFileHistoryDao.deleteAllKeyFiles()
|
||||
},
|
||||
{
|
||||
result?.invoke()
|
||||
}
|
||||
).execute()
|
||||
result?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteAll(result: (() ->Unit)? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
fun deleteAll(result: (() -> Unit)? = null) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
databaseFileHistoryDao.deleteAll()
|
||||
},
|
||||
{
|
||||
result?.invoke()
|
||||
}
|
||||
).execute()
|
||||
result?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
companion object : SingletonHolderParameter<FileDatabaseHistoryAction, Context>(::FileDatabaseHistoryAction) {
|
||||
|
||||
@@ -22,7 +22,6 @@ package com.kunzisoft.keepass.password
|
||||
import android.content.res.Resources
|
||||
import android.graphics.Color
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -42,16 +41,17 @@ class PasswordEntropy(actionOnInitFinished: (() -> Unit)? = null) {
|
||||
private var entropyJob: Job? = null
|
||||
|
||||
init {
|
||||
IOActionTask({
|
||||
// Create the password generator object
|
||||
val configuration: Configuration = ConfigurationBuilder()
|
||||
.setLocale(Locale.getDefault())
|
||||
.setMinimumEntropy(80.0)
|
||||
.createConfiguration()
|
||||
mPasswordEntropyCalculator = Nbvcxz(configuration)
|
||||
}, {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
// Create the password generator object
|
||||
val configuration: Configuration = ConfigurationBuilder()
|
||||
.setLocale(Locale.getDefault())
|
||||
.setMinimumEntropy(80.0)
|
||||
.createConfiguration()
|
||||
mPasswordEntropyCalculator = Nbvcxz(configuration)
|
||||
}
|
||||
actionOnInitFinished?.invoke()
|
||||
}).execute()
|
||||
}
|
||||
}
|
||||
|
||||
enum class Strength(val color: Int) {
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 Jeremy Jamet / Kunzisoft.
|
||||
*
|
||||
* This file is part of KeePassDX.
|
||||
*
|
||||
* KeePassDX is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* KeePassDX is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with KeePassDX. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
package com.kunzisoft.keepass.utils
|
||||
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
|
||||
/**
|
||||
* Class to invoke action in a separate IO thread
|
||||
*/
|
||||
class IOActionTask<T>(
|
||||
private val action: () -> T,
|
||||
private val onActionComplete: ((T?) -> Unit)? = null,
|
||||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.Main),
|
||||
private val exceptionHandler: CoroutineExceptionHandler? = null
|
||||
) {
|
||||
fun execute() {
|
||||
scope.launch(exceptionHandler ?: EmptyCoroutineContext) {
|
||||
withContext(Dispatchers.IO) {
|
||||
val asyncResult: Deferred<T?> = async {
|
||||
exceptionHandler?.let {
|
||||
action.invoke()
|
||||
} ?: try {
|
||||
action.invoke()
|
||||
} catch (e: Throwable) {
|
||||
e.printStackTrace()
|
||||
null
|
||||
}
|
||||
}
|
||||
withContext(Dispatchers.Main) {
|
||||
onActionComplete?.invoke(asyncResult.await())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,20 @@ package com.kunzisoft.keepass.viewmodels
|
||||
import android.app.Application
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kunzisoft.keepass.app.App
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.model.CipherEncryptDatabase
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/**
|
||||
* ViewModel for managing a single database file.
|
||||
@@ -44,31 +47,27 @@ class DatabaseFileViewModel(application: Application) : AndroidViewModel(applica
|
||||
* Checks if the given database URI corresponds to the default database.
|
||||
*/
|
||||
fun checkIfIsDefaultDatabase(databaseUri: Uri) {
|
||||
IOActionTask(
|
||||
{
|
||||
(PreferencesUtil.getDefaultDatabasePath(getApplication<App>().applicationContext)
|
||||
?.parseUri() == databaseUri)
|
||||
},
|
||||
{
|
||||
_isDefaultDatabase.value = it
|
||||
},
|
||||
).execute()
|
||||
viewModelScope.launch {
|
||||
val isDefault = withContext(Dispatchers.IO) {
|
||||
PreferencesUtil.getDefaultDatabasePath(getApplication<App>().applicationContext)
|
||||
?.parseUri() == databaseUri
|
||||
}
|
||||
_isDefaultDatabase.value = isDefault
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the default database path from preferences.
|
||||
*/
|
||||
fun removeDefaultDatabase() {
|
||||
IOActionTask(
|
||||
{
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
PreferencesUtil.saveDefaultDatabasePath(
|
||||
getApplication<App>().applicationContext,
|
||||
null,
|
||||
)
|
||||
},
|
||||
{
|
||||
},
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,18 +4,21 @@ import android.app.Application
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kunzisoft.keepass.app.App
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.hardware.HardwareKey
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.IOActionTask
|
||||
import com.kunzisoft.keepass.utils.UriUtil.releaseUriPermission
|
||||
import com.kunzisoft.keepass.utils.parseUri
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class DatabaseFilesViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
@@ -56,29 +59,25 @@ class DatabaseFilesViewModel(application: Application) : AndroidViewModel(applic
|
||||
}
|
||||
|
||||
private fun checkDefaultDatabase() {
|
||||
IOActionTask(
|
||||
{
|
||||
viewModelScope.launch {
|
||||
val uri = withContext(Dispatchers.IO) {
|
||||
PreferencesUtil.getDefaultDatabasePath(getApplication<App>().applicationContext)
|
||||
?.parseUri()
|
||||
},
|
||||
{
|
||||
_defaultDatabase.value = it
|
||||
}
|
||||
).execute()
|
||||
_defaultDatabase.value = uri
|
||||
}
|
||||
}
|
||||
|
||||
fun setDefaultDatabase(databaseFile: DatabaseFile?) {
|
||||
IOActionTask(
|
||||
{
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.IO) {
|
||||
PreferencesUtil.saveDefaultDatabasePath(
|
||||
getApplication<App>().applicationContext,
|
||||
databaseFile?.databaseUri,
|
||||
)
|
||||
},
|
||||
{
|
||||
checkDefaultDatabase()
|
||||
}
|
||||
).execute()
|
||||
checkDefaultDatabase()
|
||||
}
|
||||
}
|
||||
|
||||
fun loadListOfDatabases() {
|
||||
|
||||
Reference in new issue
Block a user