From 4f676d2316bf2cf124c6069ecc340e1fa298b8db Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Wed, 28 Feb 2024 09:12:17 +0100 Subject: [PATCH] Fix print feature not working on SuperCollapsedBlock Message --- .../mail/ui/main/thread/PrintMailFragment.kt | 16 ++++++++-------- .../mail/ui/main/thread/ThreadViewModel.kt | 14 ++++++++------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/infomaniak/mail/ui/main/thread/PrintMailFragment.kt b/app/src/main/java/com/infomaniak/mail/ui/main/thread/PrintMailFragment.kt index b8007a4f69..46edb5c985 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/main/thread/PrintMailFragment.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/main/thread/PrintMailFragment.kt @@ -59,12 +59,11 @@ class PrintMailFragment : Fragment() { setupAdapter() messagesLive.observe(viewLifecycleOwner) { (items, _) -> - threadAdapter.submitList(items.filter { it is Message && it.uid == navigationArgs.messageUid }) + threadAdapter.submitList(listOf(items.single { it is Message && it.uid == navigationArgs.messageUid })) } navigationArgs.openThreadUid?.let { - reassignThreadLive(it) - reassignMessagesLive(it) + reassignMessagesLive(it, withSuperCollapsedBlock = false) } } @@ -79,11 +78,12 @@ class PrintMailFragment : Fragment() { } private fun startPrintingView() { - threadViewModel.threadLive.value?.subject?.let { subject -> - printMailViewModel.startPrintingService(requireActivity(), subject, getWebViewToPrint()) { - findNavController().popBackStack() - } - } + printMailViewModel.startPrintingService( + activityContext = requireActivity(), + subject = (threadAdapter.items.single() as Message).subject, + webView = getWebViewToPrint(), + onFinish = findNavController()::popBackStack, + ) } private fun getWebViewToPrint(): WebView = with(binding.messagesList[0]) { findViewById(R.id.bodyWebView) } diff --git a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt index bb8bc4b0e3..700242c60e 100644 --- a/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt +++ b/app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt @@ -114,11 +114,11 @@ class ThreadViewModel @Inject constructor( } } - fun reassignMessagesLive(threadUid: String) { + fun reassignMessagesLive(threadUid: String, withSuperCollapsedBlock: Boolean = true) { messagesLiveJob?.cancel() messagesLiveJob = viewModelScope.launch(ioCoroutineContext) { messageController.getSortedAndNotDeletedMessagesAsync(threadUid) - ?.map { mapRealmMessagesResult(it.list, threadUid) } + ?.map { mapRealmMessagesResult(it.list, threadUid, withSuperCollapsedBlock) } ?.collect(messagesLive::postValue) } } @@ -126,6 +126,7 @@ class ThreadViewModel @Inject constructor( private suspend fun mapRealmMessagesResult( messages: RealmResults, threadUid: String, + withSuperCollapsedBlock: Boolean, ): Pair { superCollapsedBlock = superCollapsedBlock ?: SuperCollapsedBlock() @@ -134,7 +135,8 @@ class ThreadViewModel @Inject constructor( val messagesToFetch = mutableListOf() val thread = messages.firstOrNull()?.threads?.firstOrNull { it.uid == threadUid } ?: return items to messagesToFetch val firstIndexAfterBlock = computeFirstIndexAfterBlock(thread, messages) - superCollapsedBlock!!.shouldBeDisplayed = shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock) + superCollapsedBlock!!.shouldBeDisplayed = + shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock, withSuperCollapsedBlock) suspend fun addMessage(message: Message) { splitBody(message).let { @@ -219,9 +221,9 @@ class ThreadViewModel @Inject constructor( * - If there's any unread Message in between, it will be displayed (hence, all following Messages will be displayed too). * After all these Messages are displayed, if there's at least 2 remaining Messages, they're gonna be collapsed in the Block. */ - private fun shouldBlockBeDisplayed(messagesCount: Int, firstIndexAfterBlock: Int): Boolean { - - return superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again + private fun shouldBlockBeDisplayed(messagesCount: Int, firstIndexAfterBlock: Int, withSuperCollapsedBlock: Boolean): Boolean { + return withSuperCollapsedBlock && // When we want to print a mail, we need the full list of Messages + superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again superCollapsedBlock?.hasBeenClicked == false && // Block hasn't been expanded by the user messagesCount >= SUPER_COLLAPSED_BLOCK_MINIMUM_MESSAGES_LIMIT && // At least 5 Messages in the Thread firstIndexAfterBlock >= SUPER_COLLAPSED_BLOCK_FIRST_INDEX_LIMIT // At least 2 Messages in the Block