diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MsgDetailsViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MsgDetailsViewModel.kt index 4cd21d3812..9d9f6bac7d 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MsgDetailsViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/MsgDetailsViewModel.kt @@ -41,7 +41,7 @@ class MsgDetailsViewModel(val localFolder: LocalFolder, val msgEntity: MessageEn } } - fun updateMsgState(newMsgState: MessageState) { + fun changeMsgState(newMsgState: MessageState) { val freshMsgEntity = msgLiveData.value freshMsgEntity?.let { msgEntity -> viewModelScope.launch { diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/MessageDetailsActivity.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/MessageDetailsActivity.kt index 07c402b22d..4ed900bfac 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/MessageDetailsActivity.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/MessageDetailsActivity.kt @@ -56,7 +56,7 @@ import java.util.* * E-mail: DenBond7@gmail.com */ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.LoaderCallbacks, - Observer> { + Observer>, MessageDetailsFragment.MessageDetailsListener { private lateinit var messageEntity: MessageEntity private lateinit var localFolder: LocalFolder @@ -297,6 +297,12 @@ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.Loader } } + override fun getMsgDetailsViewModel(): MsgDetailsViewModel? { + return if (::msgDetailsViewModel.isInitialized) { + msgDetailsViewModel + } else null + } + fun decryptMsg() { if (rawMimeBytes?.isNotEmpty() == true) { idlingForDecryption!!.increment() @@ -343,7 +349,7 @@ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.Loader MsgsCacheManager.getMsgAsByteArray(messageEntity.id.toString()) } - updateMsgDetails(messageEntity) + onMsgDetailsUpdated() if (rawMimeBytes?.isNotEmpty() == true) { if (isRetrieveIncomingMsgNeeded) { @@ -352,7 +358,7 @@ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.Loader if (!JavaEmailConstants.FOLDER_OUTBOX.equals(messageEntity.folder, ignoreCase = true) && !messageEntity.isSeen) { msgDetailsViewModel.setSeenStatus(true) - msgDetailsViewModel.updateMsgState(MessageState.PENDING_MARK_READ) + msgDetailsViewModel.changeMsgState(MessageState.PENDING_MARK_READ) changeMsgsReadState() } @@ -385,11 +391,11 @@ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.Loader fragment?.onErrorOccurred(requestCode, errorType, e) } - private fun updateMsgDetails(msgEntity: MessageEntity) { + private fun onMsgDetailsUpdated() { val fragment = supportFragmentManager .findFragmentById(R.id.messageDetailsFragment) as MessageDetailsFragment? - fragment?.updateMsgDetails(msgEntity) + fragment?.onMsgDetailsUpdated() } private fun updateAtts(atts: ArrayList) { diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/MessageDetailsFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/MessageDetailsFragment.kt index 39eff63405..39d1557bc0 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/MessageDetailsFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/MessageDetailsFragment.kt @@ -7,6 +7,7 @@ package com.flowcrypt.email.ui.activity.fragment import android.Manifest import android.app.Activity +import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.os.Bundle @@ -37,7 +38,6 @@ import com.flowcrypt.email.api.email.EmailUtil import com.flowcrypt.email.api.email.FoldersManager import com.flowcrypt.email.api.email.JavaEmailConstants import com.flowcrypt.email.api.email.model.AttachmentInfo -import com.flowcrypt.email.api.email.model.GeneralMessageDetails import com.flowcrypt.email.api.email.model.IncomingMessageInfo import com.flowcrypt.email.api.email.model.LocalFolder import com.flowcrypt.email.api.email.model.ServiceInfo @@ -53,6 +53,7 @@ import com.flowcrypt.email.database.dao.source.AccountDao import com.flowcrypt.email.database.dao.source.AccountDaoSource import com.flowcrypt.email.database.dao.source.ContactsDaoSource import com.flowcrypt.email.database.entity.MessageEntity +import com.flowcrypt.email.jetpack.viewmodel.MsgDetailsViewModel import com.flowcrypt.email.model.MessageEncryptionType import com.flowcrypt.email.model.MessageType import com.flowcrypt.email.service.attachment.AttachmentDownloadManagerService @@ -83,6 +84,8 @@ import java.util.* */ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { + private lateinit var onMsgDetailsListener: MessageDetailsListener + private var textViewSenderAddress: TextView? = null private var textViewDate: TextView? = null private var textViewSubject: TextView? = null @@ -104,8 +107,6 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { private var dateFormat: java.text.DateFormat? = null private var msgInfo: IncomingMessageInfo? = null - private var msgEntity: MessageEntity? = null - private var localFolder: LocalFolder? = null private var folderType: FoldersManager.FolderType? = null private var isAdditionalActionEnabled: Boolean = false @@ -116,17 +117,29 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { private var msgEncryptType = MessageEncryptionType.STANDARD private var atts = mutableListOf() + private val msgDetailsViewModel: MsgDetailsViewModel? + get() = onMsgDetailsListener.getMsgDetailsViewModel() + + private val msgEntity: MessageEntity? + get() = msgDetailsViewModel?.msgEntity + + private val localFolder: LocalFolder? + get() = msgDetailsViewModel?.localFolder + + + override fun onAttach(context: Context) { + super.onAttach(context) + if (context is MessageDetailsListener) { + this.onMsgDetailsListener = context + } else + throw IllegalArgumentException(context.toString() + " must implement " + + MessageDetailsListener::class.java.simpleName) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) - dateFormat = DateFormat.getTimeFormat(context) - val activityIntent = activity!!.intent - - if (activityIntent != null) { - this.msgEntity = activityIntent.getParcelableExtra(MessageDetailsActivity.EXTRA_KEY_MSG) - this.localFolder = activityIntent.getParcelableExtra(MessageDetailsActivity.EXTRA_KEY_FOLDER) - } updateActionsVisibility(localFolder) } @@ -199,9 +212,7 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.menuActionArchiveMessage -> { - //todo-denbond7 #793 - /*msgDaoSource.updateMsgState(context!!, msgEntity?.email ?: "", msgEntity?.label ?: "", - msgEntity?.uid?.toLong() ?: 0, MessageState.PENDING_ARCHIVING)*/ + msgDetailsViewModel?.changeMsgState(MessageState.PENDING_ARCHIVING) (activity as? BaseSyncActivity)?.archiveMsgs() activity?.finish() true @@ -209,31 +220,19 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { R.id.menuActionDeleteMessage -> { if (JavaEmailConstants.FOLDER_OUTBOX.equals(msgEntity!!.folder, ignoreCase = true)) { - //todo-denbond7 #793 - val msgEntity: GeneralMessageDetails? = null/*msgDaoSource.getMsg(context!!, this - .msgEntity!!.email, - this.msgEntity!!.label, this.msgEntity!!.uid.toLong())*/ - - if (msgEntity == null || msgEntity.msgState === MessageState.SENDING) { - Toast.makeText(context!!, if (msgEntity == null) - R.string.can_not_delete_sent_message - else - R.string.can_not_delete_sending_message, Toast.LENGTH_LONG).show() - } else { - //todo-denbond7 #793 - /*val deletedRows = MessageDaoSource().deleteOutgoingMsg(context!!, msgEntity) - if (deletedRows > 0) { - Toast.makeText(context!!, R.string.message_was_deleted, Toast.LENGTH_SHORT).show() + val msgEntity = msgDetailsViewModel?.msgLiveData?.value + + msgEntity?.let { + if (msgEntity.msgState === MessageState.SENDING) { + Toast.makeText(context, R.string.can_not_delete_sending_message, Toast.LENGTH_LONG).show() } else { - Toast.makeText(context!!, R.string.can_not_delete_sent_message, Toast.LENGTH_LONG).show() - }*/ + msgDetailsViewModel?.deleteMsg() + Toast.makeText(context, R.string.message_was_deleted, Toast.LENGTH_SHORT).show() + } } - activity?.setResult(MessageDetailsActivity.RESULT_CODE_UPDATE_LIST, null) } else { - //todo-denbond7 #793 - /*msgDaoSource.updateMsgState(context!!, msgEntity?.email ?: "", msgEntity?.label ?: "", - msgEntity?.uid?.toLong() ?: 0, MessageState.PENDING_DELETING)*/ + msgDetailsViewModel?.changeMsgState(MessageState.PENDING_DELETING) (activity as? BaseSyncActivity)?.deleteMsgs() } activity?.finish() @@ -241,20 +240,15 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { } R.id.menuActionMoveToInbox -> { - //todo-denbond7 #793 - /*msgDaoSource.updateMsgState(context!!, msgEntity?.email ?: "", msgEntity?.label ?: "", - msgEntity?.uid?.toLong() ?: 0, MessageState.PENDING_MOVE_TO_INBOX)*/ + msgDetailsViewModel?.changeMsgState(MessageState.PENDING_MOVE_TO_INBOX) (activity as? BaseSyncActivity)?.moveMsgsToINBOX() activity?.finish() true } R.id.menuActionMarkUnread -> { - //todo-denbond7 #793 - /*msgDaoSource.updateMsgState(context!!, msgEntity?.email ?: "", msgEntity?.label ?: "", - msgEntity?.uid?.toLong() ?: 0, MessageState.PENDING_MARK_UNREAD)*/ - //todo-denbond7 #793 - //msgDaoSource.setSeenStatus(context!!, msgEntity?.email, msgEntity?.label, msgEntity?.uid? .toLong() ?: 0L, false) + msgDetailsViewModel?.changeMsgState(MessageState.PENDING_MARK_UNREAD) + msgDetailsViewModel?.setSeenStatus(false) (activity as? BaseSyncActivity)?.changeMsgsReadState() activity?.finish() true @@ -381,13 +375,8 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { UIUtil.exchangeViewVisibility(false, progressView!!, statusView!!) } - /** - * Update message msgEntity. - * - * @param msgEntity This object contains general message msgEntity. - */ - fun updateMsgDetails(msgEntity: MessageEntity) { - this.msgEntity = msgEntity + fun onMsgDetailsUpdated() { + updateViews() } fun updateAttInfos(attInfoList: ArrayList) { @@ -537,19 +526,20 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { } private fun updateViews() { - if (msgEntity != null) { - val subject = if (TextUtils.isEmpty(msgEntity!!.subject)) getString(R.string.no_subject) else msgEntity!!.subject + msgEntity?.let { + val subject = if (TextUtils.isEmpty(it.subject)) getString(R.string.no_subject) else + it.subject if (folderType === FoldersManager.FolderType.SENT) { - textViewSenderAddress!!.text = EmailUtil.getFirstAddressString(msgEntity!!.to) + textViewSenderAddress?.text = EmailUtil.getFirstAddressString(it.to) } else { - textViewSenderAddress!!.text = EmailUtil.getFirstAddressString(msgEntity!!.from) + textViewSenderAddress?.text = EmailUtil.getFirstAddressString(it.from) } - textViewSubject!!.text = subject - if (JavaEmailConstants.FOLDER_OUTBOX.equals(msgEntity?.folder, ignoreCase = true)) { - textViewDate?.text = DateTimeUtil.formatSameDayTime(context!!, msgEntity?.sentDate ?: 0) + textViewSubject?.text = subject + if (JavaEmailConstants.FOLDER_OUTBOX.equals(it.folder, ignoreCase = true)) { + textViewDate?.text = DateTimeUtil.formatSameDayTime(context, it.sentDate ?: 0) } else { - textViewDate?.text = DateTimeUtil.formatSameDayTime(context!!, msgEntity?.receivedDate ?: 0) + textViewDate?.text = DateTimeUtil.formatSameDayTime(context, it.receivedDate ?: 0) } } @@ -620,7 +610,7 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { private fun updateMsgView() { emailWebView?.loadUrl("about:blank") if (layoutMsgParts!!.childCount > 1) { - layoutMsgParts!!.removeViews(1, layoutMsgParts!!.childCount - 1) + layoutMsgParts?.removeViews(1, layoutMsgParts!!.childCount - 1) } var isFirstMsgPartText = true @@ -986,6 +976,10 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener { } } + interface MessageDetailsListener { + fun getMsgDetailsViewModel(): MsgDetailsViewModel? + } + companion object { private const val REQUEST_CODE_REQUEST_WRITE_EXTERNAL_STORAGE = 100 private const val REQUEST_CODE_START_IMPORT_KEY_ACTIVITY = 101 diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/util/DateTimeUtil.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/util/DateTimeUtil.kt index eabb47df0e..72fbb03099 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/util/DateTimeUtil.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/util/DateTimeUtil.kt @@ -37,7 +37,7 @@ class DateTimeUtil { * @return The formatted date. */ @JvmStatic - fun formatSameDayTime(context: Context, date: Long?): String { + fun formatSameDayTime(context: Context?, date: Long?): String { val calendarOfDate = GregorianCalendar() calendarOfDate.timeInMillis = date ?: return ""