mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Fix ViewModel methods call
This commit is contained in:
@@ -21,24 +21,37 @@ package com.kunzisoft.keepass.app.database
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.SingletonHolderParameter
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
import com.kunzisoft.keepass.viewmodels.FileDatabaseInfo
|
||||
|
||||
class FileDatabaseHistoryAction(applicationContext: Context) {
|
||||
class FileDatabaseHistoryAction(private val applicationContext: Context) {
|
||||
|
||||
private val databaseFileHistoryDao =
|
||||
AppDatabase
|
||||
.getDatabase(applicationContext)
|
||||
.fileDatabaseHistoryDao()
|
||||
|
||||
fun getFileDatabaseHistory(databaseUri: Uri,
|
||||
fileHistoryResultListener: (fileDatabaseHistoryResult: FileDatabaseHistoryEntity?) -> Unit) {
|
||||
fun getDatabaseFile(databaseUri: Uri,
|
||||
databaseFileResult: (DatabaseFile?) -> Unit) {
|
||||
IOActionTask(
|
||||
{
|
||||
databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
val fileDatabaseHistoryEntity = databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
val fileDatabaseInfo = FileDatabaseInfo(applicationContext, databaseUri)
|
||||
DatabaseFile(
|
||||
databaseUri,
|
||||
UriUtil.parse(fileDatabaseHistoryEntity?.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistoryEntity?.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity?.databaseAlias ?: ""),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
},
|
||||
{
|
||||
fileHistoryResultListener.invoke(it)
|
||||
databaseFileResult.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
@@ -59,62 +72,124 @@ class FileDatabaseHistoryAction(applicationContext: Context) {
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun getAllFileDatabaseHistories(fileHistoryResultListener: (fileDatabaseHistoryResult: List<FileDatabaseHistoryEntity>?) -> Unit) {
|
||||
fun getDatabaseFileList(databaseFileListResult: (List<DatabaseFile>) -> Unit) {
|
||||
IOActionTask(
|
||||
{
|
||||
databaseFileHistoryDao.getAll()
|
||||
val hideBrokenLocations = PreferencesUtil.hideBrokenLocations(applicationContext)
|
||||
// Show only uri accessible
|
||||
val databaseFileListLoaded = ArrayList<DatabaseFile>()
|
||||
databaseFileHistoryDao.getAll().forEach { fileDatabaseHistoryEntity ->
|
||||
val fileDatabaseInfo = FileDatabaseInfo(applicationContext, fileDatabaseHistoryEntity.databaseUri)
|
||||
if (hideBrokenLocations && fileDatabaseInfo.exists
|
||||
|| !hideBrokenLocations) {
|
||||
databaseFileListLoaded.add(
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileDatabaseHistoryEntity.databaseUri),
|
||||
UriUtil.parse(fileDatabaseHistoryEntity.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistoryEntity.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity.databaseAlias),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
databaseFileListLoaded
|
||||
},
|
||||
{
|
||||
fileHistoryResultListener.invoke(it)
|
||||
databaseFileList ->
|
||||
databaseFileList?.let {
|
||||
databaseFileListResult.invoke(it)
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun addOrUpdateDatabaseUri(databaseUri: Uri, keyFileUri: Uri? = null) {
|
||||
// TODO in Thread
|
||||
addOrUpdateFileDatabaseHistory(FileDatabaseHistoryEntity(
|
||||
databaseUri.toString(),
|
||||
"",
|
||||
keyFileUri?.toString(),
|
||||
System.currentTimeMillis()
|
||||
), true)
|
||||
}
|
||||
|
||||
fun addOrUpdateFileDatabaseHistory(fileDatabaseHistory: FileDatabaseHistoryEntity,
|
||||
unmodifiedAlias: Boolean = false,
|
||||
fileHistoryUpdatedResult: ((FileDatabaseHistoryEntity?) -> Unit)? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
val fileDatabaseHistoryRetrieve = databaseFileHistoryDao.getByDatabaseUri(fileDatabaseHistory.databaseUri)
|
||||
val fileDatabaseHistoryRetrieve = databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())
|
||||
|
||||
val fileDatabaseHistory = FileDatabaseHistoryEntity(
|
||||
databaseUri.toString(),
|
||||
fileDatabaseHistoryRetrieve?.databaseAlias ?: "",
|
||||
keyFileUri?.toString(),
|
||||
System.currentTimeMillis()
|
||||
)
|
||||
|
||||
if (unmodifiedAlias) {
|
||||
fileDatabaseHistory.databaseAlias = fileDatabaseHistoryRetrieve?.databaseAlias ?: ""
|
||||
}
|
||||
// Update values if history element not yet in the database
|
||||
if (fileDatabaseHistoryRetrieve == null) {
|
||||
databaseFileHistoryDao.add(fileDatabaseHistory)
|
||||
} else {
|
||||
databaseFileHistoryDao.update(fileDatabaseHistory)
|
||||
}
|
||||
fileDatabaseHistoryRetrieve
|
||||
},
|
||||
{
|
||||
fileHistoryUpdatedResult?.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun deleteFileDatabaseHistory(fileDatabaseHistory: FileDatabaseHistoryEntity,
|
||||
fileHistoryDeletedResult: (FileDatabaseHistoryEntity?) -> Unit) {
|
||||
fun addOrUpdateDatabaseFile(databaseFileToAddOrUpdate: DatabaseFile,
|
||||
databaseFileToAddedOrUpdatedResult: ((DatabaseFile?) -> Unit)? = null) {
|
||||
IOActionTask(
|
||||
{
|
||||
databaseFileHistoryDao.delete(fileDatabaseHistory)
|
||||
databaseFileToAddOrUpdate.databaseUri?.let { databaseUri ->
|
||||
val fileDatabaseHistory = FileDatabaseHistoryEntity(
|
||||
databaseUri.toString(),
|
||||
databaseFileToAddOrUpdate.databaseAlias ?: "",
|
||||
databaseFileToAddOrUpdate.keyFileUri?.toString(),
|
||||
System.currentTimeMillis()
|
||||
)
|
||||
|
||||
val fileDatabaseHistoryRetrieve = databaseFileHistoryDao.getByDatabaseUri(fileDatabaseHistory.databaseUri)
|
||||
|
||||
// Update values if history element not yet in the database
|
||||
if (fileDatabaseHistoryRetrieve == null) {
|
||||
databaseFileHistoryDao.add(fileDatabaseHistory)
|
||||
} else {
|
||||
databaseFileHistoryDao.update(fileDatabaseHistory)
|
||||
}
|
||||
|
||||
val fileDatabaseInfo = FileDatabaseInfo(applicationContext,
|
||||
fileDatabaseHistory.databaseUri)
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileDatabaseHistory.databaseUri),
|
||||
UriUtil.parse(fileDatabaseHistory.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistory.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistory.databaseAlias),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
if (it != null && it > 0)
|
||||
fileHistoryDeletedResult.invoke(fileDatabaseHistory)
|
||||
else
|
||||
fileHistoryDeletedResult.invoke(null)
|
||||
databaseFileToAddedOrUpdatedResult?.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
fun deleteDatabaseFile(databaseFileToDelete: DatabaseFile,
|
||||
databaseFileDeletedResult: (DatabaseFile?) -> Unit) {
|
||||
IOActionTask(
|
||||
{
|
||||
databaseFileToDelete.databaseUri?.let { databaseUri ->
|
||||
databaseFileHistoryDao.getByDatabaseUri(databaseUri.toString())?.let { fileDatabaseHistory ->
|
||||
val returnValue = databaseFileHistoryDao.delete(fileDatabaseHistory)
|
||||
if (returnValue > 0) {
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileDatabaseHistory.databaseUri),
|
||||
UriUtil.parse(fileDatabaseHistory.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistory.databaseUri),
|
||||
databaseFileToDelete.databaseAlias
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
databaseFileDeletedResult.invoke(it)
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
|
||||
@@ -53,11 +53,10 @@ class CreateDatabaseRunnable(context: Context,
|
||||
super.onStartRun()
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
super.onFinishRun()
|
||||
override fun onActionRun() {
|
||||
super.onActionRun()
|
||||
|
||||
if (result.isSuccess) {
|
||||
// TODO in Thread
|
||||
// Add database to recent files
|
||||
if (PreferencesUtil.rememberDatabaseLocations(context)) {
|
||||
FileDatabaseHistoryAction.getInstance(context.applicationContext)
|
||||
|
||||
@@ -66,9 +66,7 @@ class LoadDatabaseRunnable(private val context: Context,
|
||||
catch (e: LoadDatabaseException) {
|
||||
setError(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
if (result.isSuccess) {
|
||||
// Save keyFile in app database
|
||||
if (PreferencesUtil.rememberDatabaseLocations(context)) {
|
||||
@@ -89,4 +87,8 @@ class LoadDatabaseRunnable(private val context: Context,
|
||||
mDatabase.closeAndClear(cacheDirectory)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFinishRun() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,8 @@ import android.app.Application
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.kunzisoft.keepass.app.App
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.app.database.IOActionTask
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
|
||||
class DatabaseFileViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
@@ -23,27 +20,8 @@ class DatabaseFileViewModel(application: Application) : AndroidViewModel(applica
|
||||
}
|
||||
|
||||
fun loadDatabaseFile(databaseUri: Uri) {
|
||||
mFileDatabaseHistoryAction?.getFileDatabaseHistory(databaseUri) { fileDatabaseHistoryEntity ->
|
||||
IOActionTask (
|
||||
{
|
||||
val fileDatabaseInfo = FileDatabaseInfo(
|
||||
getApplication<App>().applicationContext,
|
||||
databaseUri
|
||||
)
|
||||
DatabaseFile(
|
||||
databaseUri,
|
||||
UriUtil.parse(fileDatabaseHistoryEntity?.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistoryEntity?.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity?.databaseAlias ?: ""),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
},
|
||||
{
|
||||
databaseFileLoaded.value = it ?: DatabaseFile(databaseUri)
|
||||
}
|
||||
).execute()
|
||||
mFileDatabaseHistoryAction?.getDatabaseFile(databaseUri) { databaseFileRetrieved ->
|
||||
databaseFileLoaded.value = databaseFileRetrieved
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
package com.kunzisoft.keepass.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.*
|
||||
import com.kunzisoft.keepass.app.App
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
|
||||
import com.kunzisoft.keepass.app.database.IOActionTask
|
||||
import com.kunzisoft.keepass.model.DatabaseFile
|
||||
import com.kunzisoft.keepass.settings.PreferencesUtil
|
||||
import com.kunzisoft.keepass.utils.UriUtil
|
||||
|
||||
class DatabaseFilesViewModel(application: Application) : AndroidViewModel(application) {
|
||||
|
||||
@@ -22,32 +19,7 @@ class DatabaseFilesViewModel(application: Application) : AndroidViewModel(applic
|
||||
}
|
||||
|
||||
fun loadListOfDatabases() {
|
||||
val databaseFileListLoaded = ArrayList<DatabaseFile>()
|
||||
|
||||
mFileDatabaseHistoryAction?.getAllFileDatabaseHistories { databaseFileHistoryList ->
|
||||
databaseFileHistoryList?.let { historyList ->
|
||||
IOActionTask({
|
||||
val context = getApplication<App>().applicationContext
|
||||
val hideBrokenLocations = PreferencesUtil.hideBrokenLocations(context)
|
||||
// Show only uri accessible
|
||||
historyList.forEach { fileDatabaseHistoryEntity ->
|
||||
val fileDatabaseInfo = FileDatabaseInfo(context, fileDatabaseHistoryEntity.databaseUri)
|
||||
if (hideBrokenLocations && fileDatabaseInfo.exists
|
||||
|| !hideBrokenLocations) {
|
||||
databaseFileListLoaded.add(
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileDatabaseHistoryEntity.databaseUri),
|
||||
UriUtil.parse(fileDatabaseHistoryEntity.keyFileUri),
|
||||
UriUtil.decode(fileDatabaseHistoryEntity.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileDatabaseHistoryEntity.databaseAlias),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}, {
|
||||
mFileDatabaseHistoryAction?.getDatabaseFileList { databaseFileListRetrieved ->
|
||||
var newValue = databaseFilesLoaded.value
|
||||
if (newValue == null) {
|
||||
newValue = DatabaseFileData()
|
||||
@@ -57,12 +29,10 @@ class DatabaseFilesViewModel(application: Application) : AndroidViewModel(applic
|
||||
databaseFileToActivate = null
|
||||
databaseFileList.apply {
|
||||
clear()
|
||||
addAll(databaseFileListLoaded)
|
||||
addAll(databaseFileListRetrieved)
|
||||
}
|
||||
}
|
||||
databaseFilesLoaded.value = newValue
|
||||
}).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,92 +46,40 @@ class DatabaseFilesViewModel(application: Application) : AndroidViewModel(applic
|
||||
|
||||
private fun addOrUpdateDatabaseFile(databaseFileToAddOrUpdate: DatabaseFile,
|
||||
databaseFileAction: DatabaseFileAction) {
|
||||
|
||||
databaseFileToAddOrUpdate.databaseUri?.let { databaseUri ->
|
||||
mFileDatabaseHistoryAction?.getFileDatabaseHistory(databaseUri) { fileDatabaseHistoryToAddOrUpdate ->
|
||||
fileDatabaseHistoryToAddOrUpdate?.let {
|
||||
mFileDatabaseHistoryAction?.addOrUpdateFileDatabaseHistory(fileDatabaseHistoryToAddOrUpdate) { fileHistoryAddedOrUpdated ->
|
||||
fileHistoryAddedOrUpdated?.let {
|
||||
IOActionTask (
|
||||
{
|
||||
val newValue = databaseFilesLoaded.value
|
||||
newValue?.apply {
|
||||
val fileDatabaseInfo = FileDatabaseInfo(getApplication<App>().applicationContext,
|
||||
fileHistoryAddedOrUpdated.databaseUri)
|
||||
mFileDatabaseHistoryAction?.addOrUpdateDatabaseFile(databaseFileToAddOrUpdate) { databaseFileAddedOrUpdated ->
|
||||
databaseFileAddedOrUpdated?.let { _ ->
|
||||
databaseFilesLoaded.value = databaseFilesLoaded.value?.apply {
|
||||
this.databaseFileAction = databaseFileAction
|
||||
val databaseFileToActivate =
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileHistoryAddedOrUpdated.databaseUri),
|
||||
UriUtil.parse(fileHistoryAddedOrUpdated.keyFileUri),
|
||||
UriUtil.decode(fileHistoryAddedOrUpdated.databaseUri),
|
||||
fileDatabaseInfo.retrieveDatabaseAlias(fileHistoryAddedOrUpdated.databaseAlias),
|
||||
fileDatabaseInfo.exists,
|
||||
fileDatabaseInfo.getModificationString(),
|
||||
fileDatabaseInfo.getSizeString()
|
||||
)
|
||||
when (databaseFileAction) {
|
||||
DatabaseFileAction.ADD -> {
|
||||
databaseFileList.add(databaseFileToActivate)
|
||||
databaseFileList.add(databaseFileAddedOrUpdated)
|
||||
}
|
||||
DatabaseFileAction.UPDATE -> {
|
||||
databaseFileList
|
||||
.find { it.databaseUri == databaseFileToActivate.databaseUri }
|
||||
.find { it.databaseUri == databaseFileAddedOrUpdated.databaseUri }
|
||||
?.apply {
|
||||
keyFileUri = databaseFileToActivate.keyFileUri
|
||||
databaseAlias = databaseFileToActivate.databaseAlias
|
||||
databaseFileExists = databaseFileToActivate.databaseFileExists
|
||||
databaseLastModified = databaseFileToActivate.databaseLastModified
|
||||
databaseSize = databaseFileToActivate.databaseSize
|
||||
keyFileUri = databaseFileAddedOrUpdated.keyFileUri
|
||||
databaseAlias = databaseFileAddedOrUpdated.databaseAlias
|
||||
databaseFileExists = databaseFileAddedOrUpdated.databaseFileExists
|
||||
databaseLastModified = databaseFileAddedOrUpdated.databaseLastModified
|
||||
databaseSize = databaseFileAddedOrUpdated.databaseSize
|
||||
}
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
this.databaseFileToActivate = databaseFileToActivate
|
||||
}
|
||||
},
|
||||
{ databaseFileAddedOrUpdated ->
|
||||
databaseFileAddedOrUpdated?.let {
|
||||
databaseFilesLoaded.value = it
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
this.databaseFileToActivate = databaseFileAddedOrUpdated
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun deleteDatabaseFile(databaseFileToDelete: DatabaseFile) {
|
||||
|
||||
databaseFileToDelete.databaseUri?.let { databaseUri ->
|
||||
mFileDatabaseHistoryAction?.getFileDatabaseHistory(databaseUri) { fileDatabaseHistoryToDelete ->
|
||||
fileDatabaseHistoryToDelete?.let {
|
||||
mFileDatabaseHistoryAction?.deleteFileDatabaseHistory(fileDatabaseHistoryToDelete) { fileHistoryDeleted ->
|
||||
fileHistoryDeleted?.let { _ ->
|
||||
IOActionTask (
|
||||
{
|
||||
val newValue = databaseFilesLoaded.value
|
||||
newValue?.apply {
|
||||
mFileDatabaseHistoryAction?.deleteDatabaseFile(databaseFileToDelete) { databaseFileDeleted ->
|
||||
databaseFileDeleted?.let { _ ->
|
||||
databaseFilesLoaded.value = databaseFilesLoaded.value?.apply {
|
||||
databaseFileAction = DatabaseFileAction.DELETE
|
||||
databaseFileToActivate =
|
||||
DatabaseFile(
|
||||
UriUtil.parse(fileHistoryDeleted.databaseUri),
|
||||
UriUtil.parse(fileHistoryDeleted.keyFileUri),
|
||||
UriUtil.decode(fileHistoryDeleted.databaseUri),
|
||||
databaseFileToDelete.databaseAlias
|
||||
)
|
||||
databaseFileList.remove(databaseFileToDelete)
|
||||
}
|
||||
},
|
||||
{ databaseFileDeleted ->
|
||||
databaseFileDeleted?.let {
|
||||
databaseFilesLoaded.value = it
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
databaseFileToActivate = databaseFileDeleted
|
||||
databaseFileList.remove(databaseFileDeleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user