Skip to content

Commit

Permalink
Fixed updating drafts on the thread details screen. Step 3.| #74
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 21, 2024
1 parent 6e82361 commit f533967
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data class OutgoingMessageInfo(
@Expose val signature: String? = null,
@Expose val quotedTextForReply: String? = null,
@Expose val draftId: String? = null,
@Expose val draftThreadId: Long? = null,
) : Parcelable {

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

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

Expand Down Expand Up @@ -146,7 +149,8 @@ data class OutgoingMessageInfo(
},
password = password,
attachmentsDirectory = UUID.randomUUID().toString(),
draftId = draftId
draftId = draftId,
threadId = draftThreadId
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ abstract class MessageDao : BaseDao<MessageEntity> {
label: String = JavaEmailConstants.FOLDER_OUTBOX
): List<MessageEntity>

@Query("SELECT * FROM messages WHERE folder = :label")
abstract fun getAllOutboxMessagesFlow(
label: String = JavaEmailConstants.FOLDER_OUTBOX
): Flow<List<MessageEntity>>

@Query(
"SELECT * FROM messages " +
"WHERE account = :account AND folder = :label AND state IN (:msgStates)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,24 @@ class CreateOutgoingMessageViewModel(
val activeAccount = getActiveAccountSuspend()
?: throw IllegalStateException("No active account")

val replyTo = outgoingMessageInfo.replyToMessageEntityId?.let {
roomDatabase.msgDao().getMsgById(it)?.replyToAddresses
val replyToMessageEntity = outgoingMessageInfo.replyToMessageEntityId?.let {
roomDatabase.msgDao().getMsgById(it)
}
messageEntity = outgoingMessageInfo.toMessageEntity(
val template = outgoingMessageInfo.toMessageEntity(
folder = JavaEmailConstants.FOLDER_OUTBOX,
flags = Flags(Flags.Flag.SEEN),
replyTo = replyTo,
replyTo = replyToMessageEntity?.replyToAddresses,
password = outgoingMessageInfo.password?.let {
KeyStoreCryptoManager.encrypt(String(it)).toByteArray()
}
)

messageEntity = if (replyToMessageEntity != null) {
template.copy(threadId = replyToMessageEntity.threadId)
} else {
template
}

val messageId = roomDatabase.msgDao().insertSuspend(messageEntity)
messageEntity = messageEntity.copy(id = messageId, uid = messageId)
roomDatabase.msgDao().updateSuspend(messageEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class ThreadDetailsViewModel(
val sessionFromRecipientsStateFlow: StateFlow<Set<RecipientLookUpManager.RecipientInfo>> =
sessionFromRecipientsMutableStateFlow.asStateFlow()

val allOutboxMessagesFlow = roomDatabase.msgDao().getAllOutboxMessagesFlow()

@OptIn(ExperimentalCoroutinesApi::class)
val localFolderFlow: StateFlow<LocalFolder?> =
threadMessageEntityFlow.mapLatest { threadMessageEntity ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,10 @@ class CreateMessageFragment : BaseFragment<FragmentCreateMessageBinding>(),
MessageType.REPLY,
MessageType.REPLY_ALL
)
}
},
draftThreadId = if (args.incomingMessageInfo?.msgEntity?.isDraft == true) {
args.incomingMessageInfo?.msgEntity?.threadId
} else null
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,20 @@ class ThreadDetailsFragment : BaseFragment<FragmentThreadDetailsBinding>(), Prog
}
}

launchAndRepeatWithViewLifecycle {
threadDetailsViewModel.allOutboxMessagesFlow.collect { messageEntities ->
val threadId =
threadDetailsViewModel.threadMessageEntityFlow.value?.threadId ?: return@collect
val draftsToBeDeletedInLocalCache = messageEntities.filter {
it.threadId == threadId && !it.draftId.isNullOrEmpty()
}

draftsToBeDeletedInLocalCache.forEach {
it.draftId?.let { draftId -> deleteDraftFromLocalCache(draftId) }
}
}
}

launchAndRepeatWithViewLifecycle {
threadDetailsViewModel.messageActionsAvailabilityStateFlow.collect {
activity?.invalidateOptionsMenu()
Expand Down

0 comments on commit f533967

Please sign in to comment.