Skip to content

Commit

Permalink
Fixed the message actions(deleting, archiving and etc.).| #793
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Dec 28, 2019
1 parent 7d1116f commit d7c5780
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import java.util.*
* E-mail: [email protected]
*/
class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.LoaderCallbacks<Cursor>,
Observer<NodeResponseWrapper<*>> {
Observer<NodeResponseWrapper<*>>, MessageDetailsFragment.MessageDetailsListener {

private lateinit var messageEntity: MessageEntity
private lateinit var localFolder: LocalFolder
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -343,7 +349,7 @@ class MessageDetailsActivity : BaseBackStackSyncActivity(), LoaderManager.Loader
MsgsCacheManager.getMsgAsByteArray(messageEntity.id.toString())
}

updateMsgDetails(messageEntity)
onMsgDetailsUpdated()

if (rawMimeBytes?.isNotEmpty() == true) {
if (isRetrieveIncomingMsgNeeded) {
Expand All @@ -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()
}

Expand Down Expand Up @@ -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<AttachmentInfo>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -116,17 +117,29 @@ class MessageDetailsFragment : BaseSyncFragment(), View.OnClickListener {
private var msgEncryptType = MessageEncryptionType.STANDARD
private var atts = mutableListOf<AttachmentInfo>()

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)
}
Expand Down Expand Up @@ -199,62 +212,43 @@ 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
}

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()
true
}

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
Expand Down Expand Up @@ -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<AttachmentInfo>) {
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""

Expand Down

0 comments on commit d7c5780

Please sign in to comment.