Skip to content

Commit 09b6a69

Browse files
Factorize reassignMessages() usage
1 parent 7a85475 commit 09b6a69

File tree

5 files changed

+53
-34
lines changed

5 files changed

+53
-34
lines changed

.idea/navEditor.xml

+17-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/src/main/java/com/infomaniak/mail/ui/main/thread/PrintMailFragment.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PrintMailFragment : Fragment() {
5353
return FragmentPrintMailBinding.inflate(inflater, container, false).also { binding = it }.root
5454
}
5555

56-
override fun onViewCreated(view: View, savedInstanceState: Bundle?): Unit = with(navigationArgs) {
56+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5757
super.onViewCreated(view, savedInstanceState)
5858

5959
setupAdapter()
@@ -62,9 +62,7 @@ class PrintMailFragment : Fragment() {
6262
threadAdapter.submitList(items)
6363
}
6464

65-
openThreadUid?.let {
66-
threadViewModel.reassignMessagesLive(it, messageUid, withSuperCollapsedBlock = false)
67-
}
65+
threadViewModel.reassignMessagesLiveWithoutSuperCollapsedBlock(navigationArgs.messageUid)
6866
}
6967

7068
private fun setupAdapter() {

app/src/main/java/com/infomaniak/mail/ui/main/thread/ThreadViewModel.kt

+31-14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import io.realm.kotlin.query.RealmResults
4747
import io.sentry.Sentry
4848
import io.sentry.SentryLevel
4949
import kotlinx.coroutines.*
50+
import kotlinx.coroutines.flow.Flow
5051
import kotlinx.coroutines.flow.map
5152
import javax.inject.Inject
5253
import kotlin.collections.set
@@ -128,25 +129,43 @@ class ThreadViewModel @Inject constructor(
128129
}
129130
}
130131

131-
fun reassignMessagesLive(threadUid: String, messageUid: String? = null, withSuperCollapsedBlock: Boolean = true) {
132+
fun reassignMessagesLive(threadUid: String) {
133+
reassignMessages {
134+
messageController.getSortedAndNotDeletedMessagesAsync(threadUid)?.map { mapRealmMessagesResult(it.list, threadUid) }
135+
}
136+
}
137+
138+
fun reassignMessagesLiveWithoutSuperCollapsedBlock(messageUid: String) {
139+
reassignMessages {
140+
messageController.getMessagesAsync(messageUid).map { mapRealmMessagesResult(it.list) }
141+
}
142+
}
143+
144+
private fun reassignMessages(messagesFlow: (() -> Flow<Pair<ThreadAdapterItems, MessagesWithoutHeavyData>>?)) {
132145
messagesLiveJob?.cancel()
133146
messagesLiveJob = viewModelScope.launch(ioCoroutineContext) {
147+
messagesFlow()?.collect(messagesLive::postValue)
148+
}
149+
}
134150

135-
val flow = if (messageUid == null) {
136-
messageController.getSortedAndNotDeletedMessagesAsync(threadUid)
137-
} else {
138-
messageController.getMessagesAsync(messageUid)
139-
}
151+
private suspend fun mapRealmMessagesResult(messages: RealmResults<Message>): Pair<ThreadAdapterItems, MessagesWithoutHeavyData> {
140152

141-
flow?.map { mapRealmMessagesResult(it.list, threadUid, withSuperCollapsedBlock) }
142-
?.collect(messagesLive::postValue)
153+
val items = mutableListOf<Any>()
154+
val messagesToFetch = mutableListOf<Message>()
155+
156+
messages.forEach { message ->
157+
splitBody(message).let {
158+
items += it
159+
if (!it.isFullyDownloaded()) messagesToFetch += it
160+
}
143161
}
162+
163+
return items to messagesToFetch
144164
}
145165

146166
private suspend fun mapRealmMessagesResult(
147167
messages: RealmResults<Message>,
148168
threadUid: String,
149-
withSuperCollapsedBlock: Boolean,
150169
): Pair<ThreadAdapterItems, MessagesWithoutHeavyData> {
151170

152171
superCollapsedBlock = superCollapsedBlock ?: SuperCollapsedBlock()
@@ -155,8 +174,7 @@ class ThreadViewModel @Inject constructor(
155174
val messagesToFetch = mutableListOf<Message>()
156175
val thread = messages.firstOrNull()?.threads?.firstOrNull { it.uid == threadUid } ?: return items to messagesToFetch
157176
val firstIndexAfterBlock = computeFirstIndexAfterBlock(thread, messages)
158-
superCollapsedBlock!!.shouldBeDisplayed =
159-
shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock, withSuperCollapsedBlock)
177+
superCollapsedBlock!!.shouldBeDisplayed = shouldBlockBeDisplayed(messages.count(), firstIndexAfterBlock)
160178

161179
suspend fun addMessage(message: Message) {
162180
splitBody(message).let {
@@ -241,9 +259,8 @@ class ThreadViewModel @Inject constructor(
241259
* - If there's any unread Message in between, it will be displayed (hence, all following Messages will be displayed too).
242260
* After all these Messages are displayed, if there's at least 2 remaining Messages, they're gonna be collapsed in the Block.
243261
*/
244-
private fun shouldBlockBeDisplayed(messagesCount: Int, firstIndexAfterBlock: Int, withSuperCollapsedBlock: Boolean): Boolean {
245-
return withSuperCollapsedBlock && // When we want to print a mail, we need the full list of Messages
246-
superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again
262+
private fun shouldBlockBeDisplayed(messagesCount: Int, firstIndexAfterBlock: Int): Boolean {
263+
return superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again
247264
!hasSuperCollapsedBlockBeenClicked && // Block hasn't been expanded by the user
248265
messagesCount >= SUPER_COLLAPSED_BLOCK_MINIMUM_MESSAGES_LIMIT && // At least 5 Messages in the Thread
249266
firstIndexAfterBlock >= SUPER_COLLAPSED_BLOCK_FIRST_INDEX_LIMIT // At least 2 Messages in the Block

app/src/main/java/com/infomaniak/mail/ui/main/thread/actions/MessageActionsBottomSheetDialog.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class MessageActionsBottomSheetDialog : MailActionsBottomSheetDialog() {
156156
trackBottomSheetMessageActionsEvent(ACTION_PRINT_NAME)
157157
safeNavigate(
158158
resId = R.id.printMailFragment,
159-
args = PrintMailFragmentArgs(threadUid, messageUid).toBundle(),
159+
args = PrintMailFragmentArgs(messageUid).toBundle(),
160160
currentClassName = MessageActionsBottomSheetDialog::class.java.name,
161161
)
162162
}

app/src/main/res/navigation/main_navigation.xml

+2-10
Original file line numberDiff line numberDiff line change
@@ -573,22 +573,14 @@
573573
android:id="@+id/action_attachmentActionsBottomSheetDialog_to_downloadAttachmentProgressDialog"
574574
app:destination="@id/downloadAttachmentProgressDialog" />
575575
</dialog>
576+
576577
<fragment
577578
android:id="@+id/printMailFragment"
578579
android:name="com.infomaniak.mail.ui.main.thread.PrintMailFragment"
579580
android:label="PrintMailFragment">
580-
581-
<argument
582-
android:name="openThreadUid"
583-
android:defaultValue="@null"
584-
app:argType="string"
585-
app:nullable="true" />
586-
587581
<argument
588582
android:name="messageUid"
589-
android:defaultValue="@null"
590-
app:argType="string"
591-
app:nullable="true" />
583+
app:argType="string" />
592584
</fragment>
593585

594586
<dialog

0 commit comments

Comments
 (0)