From 73b20bfe4aab71e62f336a98a9994b4e7de99b81 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Wed, 16 Jun 2021 09:26:03 +0200 Subject: [PATCH] Change load entry code --- .../keepass/activities/EntryActivity.kt | 36 ++---- .../keepass/activities/EntryEditActivity.kt | 118 ++++++++++-------- .../keepass/viewmodels/EntryViewModel.kt | 57 ++++----- 3 files changed, 103 insertions(+), 108 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 2893cb7ed..acb548677 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -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?>(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, historyPosition: Int) { + private fun retrieveEntryFromDatabase(entryId: NodeId, 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(EntryEditActivity.ADD_OR_UPDATE_ENTRY_KEY)?.let { entryUpdated -> + mEntryViewModel.loadEntry(entryUpdated) + } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index fb8a65981..4ab690079 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -139,66 +139,22 @@ class EntryEditActivity : LockingActivity(), // Entry is retrieve, it's an entry to update intent.getParcelableExtra>(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>(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) { + // 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) diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt index b6b93db70..2974668ff 100644 --- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/EntryViewModel.kt @@ -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 get() = _entryInfo private val _entryInfo = MutableLiveData() @@ -33,12 +35,21 @@ class EntryViewModel: ViewModel() { val historySelected : LiveData get() = _historySelected private val _historySelected = SingleLiveEvent() - fun loadEntryInfo(entryInfo: EntryInfo) { - _entryInfo.value = entryInfo - } - - fun loadEntryHistory(entryHistory: List) { - _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) // Custom data class to manage entry to retrieve and define is it's an history item (!= -1) data class EntryHistory(var nodeIdUUID: NodeId?, 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 { - override fun createFromParcel(parcel: Parcel): EntryHistory { - return EntryHistory(parcel) - } - - override fun newArray(size: Int): Array { - return arrayOfNulls(size) - } - } - } + var historyPosition: Int = -1) companion object { private val TAG = EntryViewModel::class.java.name