Skip to content

Commit

Permalink
Added logic to re-decrypt a message automatically when a user added a…
Browse files Browse the repository at this point in the history
… missed passphrase.| #1251
  • Loading branch information
DenBond7 committed Jun 2, 2021
1 parent 666f487 commit f8e1d43
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class MsgDetailsViewModel(
)
}

private val afterKeysStorageUpdatedMsgLiveData: MediatorLiveData<MessageEntity?> =
MediatorLiveData()

private val afterKeysUpdatedMsgLiveData: LiveData<MessageEntity?> =
keysStorage.secretKeyRingsLiveData.switchMap {
liveData {
Expand All @@ -128,6 +131,19 @@ class MsgDetailsViewModel(
}
}

private val afterPassphrasesUpdatedMsgLiveData: LiveData<MessageEntity?> =
keysStorage.passphrasesUpdatesLiveData.switchMap {
liveData {
emit(
roomDatabase.msgDao().getMsgSuspend(
account = messageEntity.email,
folder = messageEntity.folder,
uid = messageEntity.uid
)
)
}
}

private val mediatorMsgLiveData: MediatorLiveData<MessageEntity?> = MediatorLiveData()

private val processingMsgLiveData: MediatorLiveData<Result<ParseDecryptedMsgResult?>> =
Expand Down Expand Up @@ -248,19 +264,28 @@ class MsgDetailsViewModel(
)

init {
afterKeysStorageUpdatedMsgLiveData.addSource(afterKeysUpdatedMsgLiveData) {
afterKeysStorageUpdatedMsgLiveData.value = it
}
afterKeysStorageUpdatedMsgLiveData.addSource(afterPassphrasesUpdatedMsgLiveData) {
afterKeysStorageUpdatedMsgLiveData.value = it
}

mediatorMsgLiveData.addSource(initMsgLiveData) { mediatorMsgLiveData.value = it }
//here we resolve a situation when a user updates private keys.
// To prevent errors we skip the first call
mediatorMsgLiveData.addSource(afterKeysUpdatedMsgLiveData, object : Observer<MessageEntity?> {
var isFirstCall = true
override fun onChanged(messageEntity: MessageEntity?) {
if (isFirstCall) {
isFirstCall = false
} else {
mediatorMsgLiveData.value = messageEntity
mediatorMsgLiveData.addSource(
afterKeysStorageUpdatedMsgLiveData,
object : Observer<MessageEntity?> {
var isFirstCall = true
override fun onChanged(messageEntity: MessageEntity?) {
if (isFirstCall) {
isFirstCall = false
} else {
mediatorMsgLiveData.value = messageEntity
}
}
}
})
})

processingMsgLiveData.addSource(processingProgressLiveData) { processingMsgLiveData.value = it }
processingMsgLiveData.addSource(processingOutgoingMsgLiveData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ class MessageDetailsFragment : BaseFragment(), ProgressBehaviour, View.OnClickLi
this@MessageDetailsFragment,
REQUEST_CODE_SHOW_FIX_EMPTY_PASSPHRASE_DIALOG
)
fragment.show(parentFragmentManager, FixEmptyPassphraseDialogFragment::class.java.simpleName)
val tag = FixEmptyPassphraseDialogFragment::class.java.simpleName
if (parentFragmentManager.findFragmentByTag(tag) == null) {
fragment.show(parentFragmentManager, tag)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class FixEmptyPassphraseDialogFragment : BaseDialogFragment() {

@SuppressLint("FragmentLiveDataObserve")
private fun setupCheckPrivateKeysViewModel() {
checkPrivateKeysViewModel.checkPrvKeysLiveData.observe(this, { it ->
checkPrivateKeysViewModel.checkPrvKeysLiveData.observe(this, {
when (it.status) {
Result.Status.LOADING -> {
baseActivity?.countingIdlingResource?.incrementSafely()
Expand Down

0 comments on commit f8e1d43

Please sign in to comment.