Skip to content

Commit

Permalink
Migrated to use drafts().send() to send drafts(fixed deleting drafts …
Browse files Browse the repository at this point in the history
…after sending).| #74
  • Loading branch information
DenBond7 committed Nov 11, 2024
1 parent 6822679 commit 528c8cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: DenBond7
* Contributors: denbond7
*/

package com.flowcrypt.email.api.email.model
Expand Down Expand Up @@ -42,6 +42,7 @@ data class OutgoingMessageInfo(
@Expose val timestamp: Long = System.currentTimeMillis(),
@Expose val signature: String? = null,
@Expose val quotedTextForReply: String? = null,
@Expose val draftId: String? = null,
) : Parcelable {

@IgnoredOnParcel
Expand Down Expand Up @@ -89,6 +90,7 @@ data class OutgoingMessageInfo(
if (timestamp != other.timestamp) return false
if (signature != other.signature) return false
if (quotedTextForReply != other.quotedTextForReply) return false
if (draftId != other.draftId) return false
return true
}

Expand All @@ -110,6 +112,7 @@ data class OutgoingMessageInfo(
result = 31 * result + timestamp.hashCode()
result = 31 * result + (signature?.hashCode() ?: 0)
result = 31 * result + (quotedTextForReply?.hashCode() ?: 0)
result = 31 * result + (draftId?.hashCode() ?: 0)
return result
}

Expand Down Expand Up @@ -142,7 +145,8 @@ data class OutgoingMessageInfo(
MessageState.NEW.value
},
password = password,
attachmentsDirectory = UUID.randomUUID().toString()
attachmentsDirectory = UUID.randomUUID().toString(),
draftId = draftId
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class DraftViewModel(
val savingDraftStateFlow: StateFlow<Result<Boolean>> =
savingDraftMutableStateFlow.asStateFlow()

fun getSessionDraftMessageEntity(): MessageEntity? {
return sessionDraftMessageEntity?.copy()
}

fun processDraft(
coroutineScope: CoroutineScope = viewModelScope,
currentOutgoingMessageInfo: OutgoingMessageInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.google.android.gms.auth.UserRecoverableAuthException
import com.google.android.gms.common.util.CollectionUtils
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
import com.google.api.client.http.FileContent
import com.google.api.services.gmail.model.Draft
import jakarta.mail.AuthenticationFailedException
import jakarta.mail.Flags
import jakarta.mail.Folder
Expand Down Expand Up @@ -473,12 +474,25 @@ class MessagesSenderWorker(context: Context, params: WorkerParameters) :
}

val mediaContent = FileContent(Constants.MIME_TYPE_RFC822, copyOfMimeMsg)

gmailMsg = gmail
.users()
.messages()
.send(GmailApiHelper.DEFAULT_USER_ID, gmailMsg, mediaContent)
.execute()
gmailMsg = if (msgEntity.draftId.isNullOrEmpty()) {
gmail
.users()
.messages()
.send(GmailApiHelper.DEFAULT_USER_ID, gmailMsg, mediaContent)
.execute()
} else {
gmail
.users()
.drafts()
.send(
GmailApiHelper.DEFAULT_USER_ID,
Draft().apply {
message = gmailMsg
id = msgEntity.draftId
},
mediaContent
).execute()
}

if (gmailMsg.id == null) {
return@withContext false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,9 @@ class CreateMessageFragment : BaseFragment<FragmentCreateMessageBinding>(),
*/
private fun sendMsg() {
dismissCurrentSnackBar()
val outgoingMessageInfo = composeMsgViewModel.outgoingMessageInfoStateFlow.value
val outgoingMessageInfo = composeMsgViewModel.outgoingMessageInfoStateFlow.value.copy(
draftId = draftViewModel.getSessionDraftMessageEntity()?.draftId
)

navController?.navigate(
CreateMessageFragmentDirections
Expand Down

0 comments on commit 528c8cd

Please sign in to comment.