Change load entry code

This commit is contained in:
J-Jamet
2021-06-16 09:26:03 +02:00
parent fbf2006e3f
commit 73b20bfe4a
3 changed files with 103 additions and 108 deletions

View File

@@ -49,7 +49,6 @@ import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.education.EntryActivityEducation
import com.kunzisoft.keepass.magikeyboard.MagikIME
import com.kunzisoft.keepass.model.EntryAttachmentState
import com.kunzisoft.keepass.model.StreamDirection
import com.kunzisoft.keepass.otp.OtpType
import com.kunzisoft.keepass.services.AttachmentFileNotificationService
import com.kunzisoft.keepass.services.ClipboardEntryNotificationService
@@ -140,12 +139,17 @@ class EntryActivity : LockingActivity() {
intent.getParcelableExtra<NodeId<UUID>?>(KEY_ENTRY)?.let { entryId ->
val historyPosition = intent.getIntExtra(KEY_ENTRY_HISTORY_POSITION, -1)
retrieveEntryFromDatabase(entryId, historyPosition)
loadEntry()
mEntry?.let {
mEntryViewModel.loadEntry(it)
}
}
} catch (e: ClassCastException) {
Log.e(TAG, "Unable to retrieve the entry key")
}
// Fill specific history views
assignHistoryViews()
mEntryViewModel.entryInfo.observe(this) { entryInfo ->
// Manage entry copy to start notification if allowed
if (mFirstLaunchOfActivity) {
@@ -166,6 +170,9 @@ class EntryActivity : LockingActivity() {
val entryTitle = if (entryInfo.title.isNotEmpty()) entryInfo.title else entryInfo.id.toString()
collapsingToolbarLayout?.title = entryTitle
toolbar?.title = entryTitle
// Refresh Menu
invalidateOptionsMenu()
}
mEntryViewModel.otpElement.observe(this) { otpElement ->
@@ -215,7 +222,7 @@ class EntryActivity : LockingActivity() {
}
}
private fun retrieveEntryFromDatabase(entryId: NodeId<UUID>, historyPosition: Int) {
private fun retrieveEntryFromDatabase(entryId: NodeId<UUID>, historyPosition: Int = -1) {
// Manage current version and history
mLastEntryVersion = mDatabase?.getEntryById(entryId)
mEntry = if (historyPosition > -1) {
@@ -226,25 +233,6 @@ class EntryActivity : LockingActivity() {
mHistoryPosition = historyPosition
}
private fun loadEntry() {
mEntry?.let { entry ->
// To simplify template field visibility
mDatabase?.decodeEntryWithTemplateConfiguration(entry)?.let {
// To update current modification time
it.touch(modified = false, touchParents = false)
// Fill specific history views
assignHistoryViews()
// Refresh Menu
invalidateOptionsMenu()
val entryInfo = it.getEntryInfo(mDatabase)
mEntryViewModel.loadEntryInfo(entryInfo)
mEntryViewModel.loadEntryHistory(it.getHistory())
}
}
}
override fun onResume() {
super.onResume()
@@ -293,7 +281,9 @@ class EntryActivity : LockingActivity() {
when (requestCode) {
EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE -> {
// Reload the current id from database
loadEntry()
data?.getParcelableExtra<Entry>(EntryEditActivity.ADD_OR_UPDATE_ENTRY_KEY)?.let { entryUpdated ->
mEntryViewModel.loadEntry(entryUpdated)
}
}
}

View File

@@ -139,66 +139,22 @@ class EntryEditActivity : LockingActivity(),
// Entry is retrieve, it's an entry to update
intent.getParcelableExtra<NodeId<UUID>>(KEY_ENTRY)?.let {
// Create an Entry copy to modify from the database entry
mEntry = mDatabase?.getEntryById(it)
// Retrieve the parent
mEntry?.let { entry ->
// If no parent, add root group as parent
if (entry.parent == null) {
entry.parent = mDatabase?.rootGroup
}
}
retrieveEntryFromDatabase(it)
}
// Parent is retrieve, it's a new entry to create
intent.getParcelableExtra<NodeId<*>>(KEY_PARENT)?.let {
mParent = mDatabase?.getGroupById(it)
mEntry = mDatabase?.createEntry().apply {
// Add the default icon from parent if not a folder
val parentIcon = mParent?.icon
// Set default icon
if (parentIcon != null) {
if (parentIcon.custom.isUnknown
&& parentIcon.standard.id != IconImageStandard.FOLDER_ID) {
this?.icon = IconImage(parentIcon.standard)
}
if (!parentIcon.custom.isUnknown) {
this?.icon = IconImage(parentIcon.custom)
}
}
// Set default username
this?.username = mDatabase?.defaultUsername ?: ""
}
createEntryFromDatabase(it)
}
// Define is current entry is a template (in direct template group)
mIsTemplate = mDatabase?.entryIsTemplate(mEntry) ?: false
checkIfEntryIsTemplateFromDatabase()
// Default template
mEntryTemplate = mEntry?.let {
mDatabase?.getTemplate(it)
} ?: Template.STANDARD
retrieveTemplateEntryFromDatabase()
mEntryEditViewModel.loadTemplate(mEntryTemplate)
// Decode the entry
mEntry?.let {
mEntry = mDatabase?.decodeEntryWithTemplateConfiguration(it)
}
// Load entry info
mEntry?.getEntryInfo(mDatabase, true)?.let { tempEntryInfo ->
// Retrieve data from registration
val registerInfo = EntrySelectionHelper.retrieveRegisterInfoFromIntent(intent)
val searchInfo: SearchInfo? = registerInfo?.searchInfo
?: EntrySelectionHelper.retrieveSearchInfoFromIntent(intent)
searchInfo?.let { tempSearchInfo ->
tempEntryInfo.saveSearchInfo(mDatabase, tempSearchInfo)
}
registerInfo?.let { regInfo ->
tempEntryInfo.saveRegisterInfo(mDatabase, regInfo)
}
mEntryEditViewModel.loadEntryInfo(tempEntryInfo)
}
loadEntryInfo()
// View model listeners
mEntryEditViewModel.requestIconSelection.observe(this) { iconImage ->
@@ -379,6 +335,70 @@ class EntryEditActivity : LockingActivity(),
}
}
private fun retrieveEntryFromDatabase(entryId: NodeId<UUID>) {
// Create an Entry copy to modify from the database entry
mEntry = mDatabase?.getEntryById(entryId)
// Retrieve the parent
mEntry?.let { entry ->
// If no parent, add root group as parent
if (entry.parent == null) {
entry.parent = mDatabase?.rootGroup
}
}
}
private fun createEntryFromDatabase(parentId: NodeId<*>) {
mParent = mDatabase?.getGroupById(parentId)
mEntry = mDatabase?.createEntry().apply {
// Add the default icon from parent if not a folder
val parentIcon = mParent?.icon
// Set default icon
if (parentIcon != null) {
if (parentIcon.custom.isUnknown
&& parentIcon.standard.id != IconImageStandard.FOLDER_ID) {
this?.icon = IconImage(parentIcon.standard)
}
if (!parentIcon.custom.isUnknown) {
this?.icon = IconImage(parentIcon.custom)
}
}
// Set default username
this?.username = mDatabase?.defaultUsername ?: ""
}
}
private fun checkIfEntryIsTemplateFromDatabase() {
mIsTemplate = mDatabase?.entryIsTemplate(mEntry) ?: false
}
private fun retrieveTemplateEntryFromDatabase() {
mEntryTemplate = mEntry?.let {
mDatabase?.getTemplate(it)
} ?: Template.STANDARD
}
private fun loadEntryInfo() {
// Decode the entry
mEntry?.let {
mEntry = mDatabase?.decodeEntryWithTemplateConfiguration(it)
}
// Load entry info
mEntry?.getEntryInfo(mDatabase, true)?.let { tempEntryInfo ->
// Retrieve data from registration
val registerInfo = EntrySelectionHelper.retrieveRegisterInfoFromIntent(intent)
val searchInfo: SearchInfo? = registerInfo?.searchInfo
?: EntrySelectionHelper.retrieveSearchInfoFromIntent(intent)
searchInfo?.let { tempSearchInfo ->
tempEntryInfo.saveSearchInfo(mDatabase, tempSearchInfo)
}
registerInfo?.let { regInfo ->
tempEntryInfo.saveRegisterInfo(mDatabase, regInfo)
}
mEntryEditViewModel.loadEntryInfo(tempEntryInfo)
}
}
private fun entryValidatedForSave(actionTask: String, entry: Entry) {
onValidateSpecialMode()
finishForEntryResult(actionTask, entry)

View File

@@ -1,11 +1,11 @@
package com.kunzisoft.keepass.viewmodels
import android.os.Parcel
import android.os.Parcelable
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kunzisoft.keepass.app.database.IOActionTask
import com.kunzisoft.keepass.database.element.Attachment
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Entry
import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.model.EntryAttachmentState
@@ -16,6 +16,8 @@ import java.util.*
class EntryViewModel: ViewModel() {
private val mDatabase: Database? = Database.getInstance()
val entryInfo : LiveData<EntryInfo> get() = _entryInfo
private val _entryInfo = MutableLiveData<EntryInfo>()
@@ -33,12 +35,21 @@ class EntryViewModel: ViewModel() {
val historySelected : LiveData<EntryHistory> get() = _historySelected
private val _historySelected = SingleLiveEvent<EntryHistory>()
fun loadEntryInfo(entryInfo: EntryInfo) {
_entryInfo.value = entryInfo
}
fun loadEntryHistory(entryHistory: List<Entry>) {
_entryHistory.value = entryHistory
fun loadEntry(entry: Entry) {
IOActionTask(
{
// To simplify template field visibility
mDatabase?.decodeEntryWithTemplateConfiguration(entry)?.let {
// To update current modification time
it.touch(modified = false, touchParents = false)
EntryInfoHistory(it.getEntryInfo(mDatabase), it.getHistory())
}
},
{
_entryInfo.value = it?.entryInfo
_entryHistory.value = it?.entryHistory
}
).execute()
}
fun onOtpElementUpdated(optElement: OtpElement) {
@@ -57,38 +68,12 @@ class EntryViewModel: ViewModel() {
_historySelected.value = EntryHistory(item.nodeId, item, null, position)
}
data class EntryInfoHistory(val entryInfo: EntryInfo, val entryHistory: List<Entry>)
// Custom data class to manage entry to retrieve and define is it's an history item (!= -1)
data class EntryHistory(var nodeIdUUID: NodeId<UUID>?,
var entry: Entry?,
var lastEntryVersion: Entry?,
var historyPosition: Int = -1): Parcelable {
constructor(parcel: Parcel) : this(
parcel.readParcelable(NodeId::class.java.classLoader),
parcel.readParcelable(Entry::class.java.classLoader),
parcel.readParcelable(Entry::class.java.classLoader),
parcel.readInt())
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeParcelable(nodeIdUUID, flags)
parcel.writeParcelable(entry, flags)
parcel.writeParcelable(lastEntryVersion, flags)
parcel.writeInt(historyPosition)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<EntryHistory> {
override fun createFromParcel(parcel: Parcel): EntryHistory {
return EntryHistory(parcel)
}
override fun newArray(size: Int): Array<EntryHistory?> {
return arrayOfNulls(size)
}
}
}
var historyPosition: Int = -1)
companion object {
private val TAG = EntryViewModel::class.java.name