Skip to content

Commit bc52681

Browse files
Merge pull request #1660 from Infomaniak/refctor-thread-fragement-nav
Expose similar `safeNavigate` method in ThreadFragment as other fragments have
2 parents 402ab20 + 2cca96f commit bc52681

File tree

3 files changed

+61
-81
lines changed

3 files changed

+61
-81
lines changed

app/src/main/java/com/infomaniak/mail/ui/main/folder/TwoPaneFragment.kt

+2-23
Original file line numberDiff line numberDiff line change
@@ -129,35 +129,14 @@ abstract class TwoPaneFragment : Fragment() {
129129
}
130130

131131
private fun observeThreadNavigation() = with(twoPaneViewModel) {
132-
133132
getBackNavigationResult(AttachmentIntentUtils.DOWNLOAD_ATTACHMENT_RESULT, ::startActivity)
134133

135-
attachmentActionsArgs.observe(viewLifecycleOwner) {
136-
safeNavigate(resId = R.id.attachmentActionsBottomSheetDialog, args = it.toBundle())
137-
}
138-
139134
newMessageArgs.observe(viewLifecycleOwner) {
140135
safeNavigateToNewMessageActivity(args = it.toBundle())
141136
}
142137

143-
replyBottomSheetArgs.observe(viewLifecycleOwner) {
144-
safeNavigate(resId = R.id.replyBottomSheetDialog, args = it.toBundle())
145-
}
146-
147-
threadActionsArgs.observe(viewLifecycleOwner) {
148-
safeNavigate(resId = R.id.threadActionsBottomSheetDialog, args = it.toBundle())
149-
}
150-
151-
messageActionsArgs.observe(viewLifecycleOwner) {
152-
safeNavigate(resId = R.id.messageActionsBottomSheetDialog, args = it.toBundle())
153-
}
154-
155-
detailedContactArgs.observe(viewLifecycleOwner) {
156-
safeNavigate(resId = R.id.detailedContactBottomSheetDialog, args = it.toBundle())
157-
}
158-
159-
attendeesArgs.observe(viewLifecycleOwner) {
160-
safeNavigate(resId = R.id.attendeesBottomSheetDialog, args = it.toBundle())
138+
navArgs.observe(viewLifecycleOwner) { (resId, args) ->
139+
safeNavigate(resId, args)
161140
}
162141
}
163142

app/src/main/java/com/infomaniak/mail/ui/main/folder/TwoPaneViewModel.kt

+7-42
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@
1818
package com.infomaniak.mail.ui.main.folder
1919

2020
import android.net.Uri
21+
import android.os.Bundle
22+
import androidx.annotation.IdRes
2123
import androidx.lifecycle.MutableLiveData
2224
import androidx.lifecycle.ViewModel
2325
import com.infomaniak.lib.core.utils.SingleLiveEvent
2426
import com.infomaniak.mail.data.cache.mailboxContent.DraftController
25-
import com.infomaniak.mail.data.models.calendar.Attendee
26-
import com.infomaniak.mail.data.models.correspondent.Recipient
2727
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
2828
import com.infomaniak.mail.data.models.message.Message
2929
import com.infomaniak.mail.data.models.thread.Thread
30-
import com.infomaniak.mail.ui.main.thread.DetailedContactBottomSheetDialogArgs
31-
import com.infomaniak.mail.ui.main.thread.actions.AttachmentActionsBottomSheetDialogArgs
32-
import com.infomaniak.mail.ui.main.thread.actions.MessageActionsBottomSheetDialogArgs
33-
import com.infomaniak.mail.ui.main.thread.actions.ReplyBottomSheetDialogArgs
34-
import com.infomaniak.mail.ui.main.thread.actions.ThreadActionsBottomSheetDialogArgs
35-
import com.infomaniak.mail.ui.main.thread.calendar.AttendeesBottomSheetDialogArgs
3630
import com.infomaniak.mail.ui.newMessage.NewMessageActivityArgs
3731
import com.infomaniak.mail.utils.Utils.runCatchingRealm
3832
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -49,13 +43,8 @@ class TwoPaneViewModel @Inject constructor(
4943
val rightPaneFolderName = MutableLiveData<String>()
5044
var previousFolderId: String? = null
5145

52-
val attachmentActionsArgs = SingleLiveEvent<AttachmentActionsBottomSheetDialogArgs>()
5346
val newMessageArgs = SingleLiveEvent<NewMessageActivityArgs>()
54-
val replyBottomSheetArgs = SingleLiveEvent<ReplyBottomSheetDialogArgs>()
55-
val threadActionsArgs = SingleLiveEvent<ThreadActionsBottomSheetDialogArgs>()
56-
val messageActionsArgs = SingleLiveEvent<MessageActionsBottomSheetDialogArgs>()
57-
val detailedContactArgs = SingleLiveEvent<DetailedContactBottomSheetDialogArgs>()
58-
val attendeesArgs = SingleLiveEvent<AttendeesBottomSheetDialogArgs>()
47+
val navArgs = SingleLiveEvent<NavData>()
5948

6049
fun openThread(uid: String) {
6150
currentThreadUid.value = uid
@@ -69,10 +58,6 @@ class TwoPaneViewModel @Inject constructor(
6958
navigateToSelectedDraft(thread.messages.single())
7059
}
7160

72-
fun navigateToAttachmentActions(resource: String) {
73-
attachmentActionsArgs.value = AttachmentActionsBottomSheetDialogArgs(resource)
74-
}
75-
7661
private fun navigateToSelectedDraft(message: Message) = runCatchingRealm {
7762
newMessageArgs.value = NewMessageActivityArgs(
7863
arrivedFromExistingDraft = true,
@@ -104,28 +89,8 @@ class TwoPaneViewModel @Inject constructor(
10489
)
10590
}
10691

107-
fun navigateToReply(messageUid: String, shouldLoadDistantResources: Boolean) {
108-
replyBottomSheetArgs.value = ReplyBottomSheetDialogArgs(messageUid, shouldLoadDistantResources)
109-
}
110-
111-
fun navigateToThreadActions(threadUid: String, shouldLoadDistantResources: Boolean, messageUidToReplyTo: String) {
112-
threadActionsArgs.value = ThreadActionsBottomSheetDialogArgs(threadUid, shouldLoadDistantResources, messageUidToReplyTo)
113-
}
114-
115-
fun navigateToMessageAction(messageUid: String, isThemeTheSame: Boolean, shouldLoadDistantResources: Boolean) {
116-
messageActionsArgs.value = MessageActionsBottomSheetDialogArgs(
117-
messageUid = messageUid,
118-
threadUid = currentThreadUid.value ?: return,
119-
isThemeTheSame = isThemeTheSame,
120-
shouldLoadDistantResources = shouldLoadDistantResources,
121-
)
122-
}
123-
124-
fun navigateToDetailContact(recipient: Recipient) {
125-
detailedContactArgs.value = DetailedContactBottomSheetDialogArgs(recipient)
126-
}
127-
128-
fun navigateToAttendees(attendees: Array<Attendee>) {
129-
attendeesArgs.value = AttendeesBottomSheetDialogArgs(attendees)
130-
}
92+
data class NavData(
93+
@IdRes val resId: Int,
94+
val args: Bundle,
95+
)
13196
}

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

+52-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import android.text.method.LinkMovementMethod
2525
import android.view.LayoutInflater
2626
import android.view.View
2727
import android.view.ViewGroup
28+
import androidx.annotation.IdRes
2829
import androidx.core.graphics.ColorUtils
2930
import androidx.core.view.isGone
3031
import androidx.core.view.isVisible
@@ -61,8 +62,14 @@ import com.infomaniak.mail.ui.MainViewModel
6162
import com.infomaniak.mail.ui.alertDialogs.*
6263
import com.infomaniak.mail.ui.main.folder.TwoPaneFragment
6364
import com.infomaniak.mail.ui.main.folder.TwoPaneViewModel
65+
import com.infomaniak.mail.ui.main.folder.TwoPaneViewModel.NavData
6466
import com.infomaniak.mail.ui.main.thread.ThreadAdapter.ContextMenuType
6567
import com.infomaniak.mail.ui.main.thread.ThreadViewModel.OpenThreadResult
68+
import com.infomaniak.mail.ui.main.thread.actions.AttachmentActionsBottomSheetDialogArgs
69+
import com.infomaniak.mail.ui.main.thread.actions.MessageActionsBottomSheetDialogArgs
70+
import com.infomaniak.mail.ui.main.thread.actions.ReplyBottomSheetDialogArgs
71+
import com.infomaniak.mail.ui.main.thread.actions.ThreadActionsBottomSheetDialogArgs
72+
import com.infomaniak.mail.ui.main.thread.calendar.AttendeesBottomSheetDialogArgs
6673
import com.infomaniak.mail.utils.*
6774
import com.infomaniak.mail.utils.ExternalUtils.findExternalRecipients
6875
import com.infomaniak.mail.utils.UiUtils.dividerDrawable
@@ -186,7 +193,12 @@ class ThreadFragment : Fragment() {
186193
private fun setupAdapter() = with(binding.messagesList) {
187194
adapter = ThreadAdapter(
188195
shouldLoadDistantResources = shouldLoadDistantResources(),
189-
onContactClicked = twoPaneViewModel::navigateToDetailContact,
196+
onContactClicked = {
197+
safeNavigate(
198+
resId = R.id.detailedContactBottomSheetDialog,
199+
args = DetailedContactBottomSheetDialogArgs(it).toBundle(),
200+
)
201+
},
190202
onDraftClicked = { message ->
191203
trackNewMessageEvent(OPEN_FROM_DRAFT_NAME)
192204
twoPaneViewModel.navigateToNewMessage(
@@ -201,7 +213,12 @@ class ThreadFragment : Fragment() {
201213
mainViewModel.currentMailbox.value?.let { mailbox -> threadViewModel.deleteDraft(message, mailbox) }
202214
},
203215
onAttachmentClicked = { attachment ->
204-
attachment.resource?.let(twoPaneViewModel::navigateToAttachmentActions)
216+
attachment.resource?.let { resource ->
217+
safeNavigate(
218+
resId = R.id.attachmentActionsBottomSheetDialog,
219+
args = AttachmentActionsBottomSheetDialogArgs(resource).toBundle(),
220+
)
221+
}
205222
},
206223
onDownloadAllClicked = { message ->
207224
trackAttachmentActionsEvent("downloadAll")
@@ -215,8 +232,13 @@ class ThreadFragment : Fragment() {
215232
message.navigateToActionsBottomSheet()
216233
},
217234
onAllExpandedMessagesLoaded = ::scrollToFirstUnseenMessage,
235+
navigateToAttendeeBottomSheet = { attendees ->
236+
safeNavigate(
237+
resId = R.id.attendeesBottomSheetDialog,
238+
args = AttendeesBottomSheetDialogArgs(attendees.toTypedArray()).toBundle(),
239+
)
240+
},
218241
navigateToNewMessageActivity = { twoPaneViewModel.navigateToNewMessage(mailToUri = it) },
219-
navigateToAttendeeBottomSheet = { attendees -> twoPaneViewModel.navigateToAttendees(attendees.toTypedArray()) },
220242
promptLink = { data, type ->
221243
// When adding a phone number to contacts, Google decodes this value in case it's url-encoded. But I could not
222244
// reproduce this issue when manually creating a url-encoded href. If this is triggered, fix it by also
@@ -316,22 +338,25 @@ class ThreadFragment : Fragment() {
316338
threadViewModel.failedMessagesUids.observe(viewLifecycleOwner, threadAdapter::updateFailedMessages)
317339
}
318340

319-
private fun observeQuickActionBarClicks() = with(twoPaneViewModel) {
341+
private fun observeQuickActionBarClicks() {
320342
threadViewModel.quickActionBarClicks.observe(viewLifecycleOwner) { (threadUid, lastMessageToReplyTo, menuId) ->
321343
when (menuId) {
322344
R.id.quickActionReply -> replyTo(lastMessageToReplyTo)
323345
R.id.quickActionForward -> {
324-
navigateToNewMessage(
346+
twoPaneViewModel.navigateToNewMessage(
325347
draftMode = DraftMode.FORWARD,
326348
previousMessageUid = lastMessageToReplyTo.uid,
327349
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
328350
)
329351
}
330352
R.id.quickActionMenu -> {
331-
navigateToThreadActions(
332-
threadUid = threadUid,
333-
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
334-
messageUidToReplyTo = lastMessageToReplyTo.uid,
353+
safeNavigate(
354+
resId = R.id.threadActionsBottomSheetDialog,
355+
args = ThreadActionsBottomSheetDialogArgs(
356+
threadUid = threadUid,
357+
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
358+
messageUidToReplyTo = lastMessageToReplyTo.uid,
359+
).toBundle(),
335360
)
336361
}
337362
}
@@ -450,26 +475,33 @@ class ThreadFragment : Fragment() {
450475
scheduleDownloadManager(url, name)
451476
}
452477

453-
private fun replyTo(message: Message) = with(twoPaneViewModel) {
478+
private fun replyTo(message: Message) {
454479

455480
val shouldLoadDistantResources = shouldLoadDistantResources(message.uid)
456481

457482
if (message.getRecipientsForReplyTo(replyAll = true).second.isEmpty()) {
458-
navigateToNewMessage(
483+
twoPaneViewModel.navigateToNewMessage(
459484
draftMode = DraftMode.REPLY,
460485
previousMessageUid = message.uid,
461486
shouldLoadDistantResources = shouldLoadDistantResources,
462487
)
463488
} else {
464-
navigateToReply(message.uid, shouldLoadDistantResources)
489+
safeNavigate(
490+
resId = R.id.replyBottomSheetDialog,
491+
args = ReplyBottomSheetDialogArgs(message.uid, shouldLoadDistantResources).toBundle(),
492+
)
465493
}
466494
}
467495

468496
private fun Message.navigateToActionsBottomSheet() {
469-
twoPaneViewModel.navigateToMessageAction(
470-
messageUid = uid,
471-
isThemeTheSame = threadAdapter.isThemeTheSameMap[uid] ?: return,
472-
shouldLoadDistantResources = shouldLoadDistantResources(uid),
497+
safeNavigate(
498+
resId = R.id.messageActionsBottomSheetDialog,
499+
args = MessageActionsBottomSheetDialogArgs(
500+
messageUid = uid,
501+
threadUid = twoPaneViewModel.currentThreadUid.value ?: return,
502+
isThemeTheSame = threadAdapter.isThemeTheSameMap[uid] ?: return,
503+
shouldLoadDistantResources = shouldLoadDistantResources(uid),
504+
).toBundle(),
473505
)
474506
}
475507

@@ -548,6 +580,10 @@ class ThreadFragment : Fragment() {
548580

549581
fun getAnchor(): View? = _binding?.quickActionBar
550582

583+
private fun safeNavigate(@IdRes resId: Int, args: Bundle) {
584+
twoPaneViewModel.navArgs.value = NavData(resId, args)
585+
}
586+
551587
enum class HeaderState {
552588
ELEVATED,
553589
LOWERED,

0 commit comments

Comments
 (0)