Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix print feature not working on SuperCollapsedBlock Message #1723

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,19 @@ 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)
}
}

private suspend fun mapRealmMessagesResult(
messages: RealmResults<Message>,
threadUid: String,
withSuperCollapsedBlock: Boolean,
): Pair<ThreadAdapterItems, MessagesWithoutHeavyData> {

superCollapsedBlock = superCollapsedBlock ?: SuperCollapsedBlock()
Expand All @@ -134,7 +135,8 @@ class ThreadViewModel @Inject constructor(
val messagesToFetch = mutableListOf<Message>()
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 {
Expand Down Expand Up @@ -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
Expand Down
Loading