Skip to content

Commit

Permalink
Merge pull request #1660 from Infomaniak/refctor-thread-fragement-nav
Browse files Browse the repository at this point in the history
Expose similar `safeNavigate` method in ThreadFragment as other fragments have
  • Loading branch information
KevinBoulongne authored Jan 22, 2024
2 parents 402ab20 + 2cca96f commit bc52681
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,35 +129,14 @@ abstract class TwoPaneFragment : Fragment() {
}

private fun observeThreadNavigation() = with(twoPaneViewModel) {

getBackNavigationResult(AttachmentIntentUtils.DOWNLOAD_ATTACHMENT_RESULT, ::startActivity)

attachmentActionsArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.attachmentActionsBottomSheetDialog, args = it.toBundle())
}

newMessageArgs.observe(viewLifecycleOwner) {
safeNavigateToNewMessageActivity(args = it.toBundle())
}

replyBottomSheetArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.replyBottomSheetDialog, args = it.toBundle())
}

threadActionsArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.threadActionsBottomSheetDialog, args = it.toBundle())
}

messageActionsArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.messageActionsBottomSheetDialog, args = it.toBundle())
}

detailedContactArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.detailedContactBottomSheetDialog, args = it.toBundle())
}

attendeesArgs.observe(viewLifecycleOwner) {
safeNavigate(resId = R.id.attendeesBottomSheetDialog, args = it.toBundle())
navArgs.observe(viewLifecycleOwner) { (resId, args) ->
safeNavigate(resId, args)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@
package com.infomaniak.mail.ui.main.folder

import android.net.Uri
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.infomaniak.lib.core.utils.SingleLiveEvent
import com.infomaniak.mail.data.cache.mailboxContent.DraftController
import com.infomaniak.mail.data.models.calendar.Attendee
import com.infomaniak.mail.data.models.correspondent.Recipient
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.data.models.thread.Thread
import com.infomaniak.mail.ui.main.thread.DetailedContactBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.AttachmentActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.MessageActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.ReplyBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.ThreadActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.calendar.AttendeesBottomSheetDialogArgs
import com.infomaniak.mail.ui.newMessage.NewMessageActivityArgs
import com.infomaniak.mail.utils.Utils.runCatchingRealm
import dagger.hilt.android.lifecycle.HiltViewModel
Expand All @@ -49,13 +43,8 @@ class TwoPaneViewModel @Inject constructor(
val rightPaneFolderName = MutableLiveData<String>()
var previousFolderId: String? = null

val attachmentActionsArgs = SingleLiveEvent<AttachmentActionsBottomSheetDialogArgs>()
val newMessageArgs = SingleLiveEvent<NewMessageActivityArgs>()
val replyBottomSheetArgs = SingleLiveEvent<ReplyBottomSheetDialogArgs>()
val threadActionsArgs = SingleLiveEvent<ThreadActionsBottomSheetDialogArgs>()
val messageActionsArgs = SingleLiveEvent<MessageActionsBottomSheetDialogArgs>()
val detailedContactArgs = SingleLiveEvent<DetailedContactBottomSheetDialogArgs>()
val attendeesArgs = SingleLiveEvent<AttendeesBottomSheetDialogArgs>()
val navArgs = SingleLiveEvent<NavData>()

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

fun navigateToAttachmentActions(resource: String) {
attachmentActionsArgs.value = AttachmentActionsBottomSheetDialogArgs(resource)
}

private fun navigateToSelectedDraft(message: Message) = runCatchingRealm {
newMessageArgs.value = NewMessageActivityArgs(
arrivedFromExistingDraft = true,
Expand Down Expand Up @@ -104,28 +89,8 @@ class TwoPaneViewModel @Inject constructor(
)
}

fun navigateToReply(messageUid: String, shouldLoadDistantResources: Boolean) {
replyBottomSheetArgs.value = ReplyBottomSheetDialogArgs(messageUid, shouldLoadDistantResources)
}

fun navigateToThreadActions(threadUid: String, shouldLoadDistantResources: Boolean, messageUidToReplyTo: String) {
threadActionsArgs.value = ThreadActionsBottomSheetDialogArgs(threadUid, shouldLoadDistantResources, messageUidToReplyTo)
}

fun navigateToMessageAction(messageUid: String, isThemeTheSame: Boolean, shouldLoadDistantResources: Boolean) {
messageActionsArgs.value = MessageActionsBottomSheetDialogArgs(
messageUid = messageUid,
threadUid = currentThreadUid.value ?: return,
isThemeTheSame = isThemeTheSame,
shouldLoadDistantResources = shouldLoadDistantResources,
)
}

fun navigateToDetailContact(recipient: Recipient) {
detailedContactArgs.value = DetailedContactBottomSheetDialogArgs(recipient)
}

fun navigateToAttendees(attendees: Array<Attendee>) {
attendeesArgs.value = AttendeesBottomSheetDialogArgs(attendees)
}
data class NavData(
@IdRes val resId: Int,
val args: Bundle,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.IdRes
import androidx.core.graphics.ColorUtils
import androidx.core.view.isGone
import androidx.core.view.isVisible
Expand Down Expand Up @@ -61,8 +62,14 @@ import com.infomaniak.mail.ui.MainViewModel
import com.infomaniak.mail.ui.alertDialogs.*
import com.infomaniak.mail.ui.main.folder.TwoPaneFragment
import com.infomaniak.mail.ui.main.folder.TwoPaneViewModel
import com.infomaniak.mail.ui.main.folder.TwoPaneViewModel.NavData
import com.infomaniak.mail.ui.main.thread.ThreadAdapter.ContextMenuType
import com.infomaniak.mail.ui.main.thread.ThreadViewModel.OpenThreadResult
import com.infomaniak.mail.ui.main.thread.actions.AttachmentActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.MessageActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.ReplyBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.actions.ThreadActionsBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.thread.calendar.AttendeesBottomSheetDialogArgs
import com.infomaniak.mail.utils.*
import com.infomaniak.mail.utils.ExternalUtils.findExternalRecipients
import com.infomaniak.mail.utils.UiUtils.dividerDrawable
Expand Down Expand Up @@ -186,7 +193,12 @@ class ThreadFragment : Fragment() {
private fun setupAdapter() = with(binding.messagesList) {
adapter = ThreadAdapter(
shouldLoadDistantResources = shouldLoadDistantResources(),
onContactClicked = twoPaneViewModel::navigateToDetailContact,
onContactClicked = {
safeNavigate(
resId = R.id.detailedContactBottomSheetDialog,
args = DetailedContactBottomSheetDialogArgs(it).toBundle(),
)
},
onDraftClicked = { message ->
trackNewMessageEvent(OPEN_FROM_DRAFT_NAME)
twoPaneViewModel.navigateToNewMessage(
Expand All @@ -201,7 +213,12 @@ class ThreadFragment : Fragment() {
mainViewModel.currentMailbox.value?.let { mailbox -> threadViewModel.deleteDraft(message, mailbox) }
},
onAttachmentClicked = { attachment ->
attachment.resource?.let(twoPaneViewModel::navigateToAttachmentActions)
attachment.resource?.let { resource ->
safeNavigate(
resId = R.id.attachmentActionsBottomSheetDialog,
args = AttachmentActionsBottomSheetDialogArgs(resource).toBundle(),
)
}
},
onDownloadAllClicked = { message ->
trackAttachmentActionsEvent("downloadAll")
Expand All @@ -215,8 +232,13 @@ class ThreadFragment : Fragment() {
message.navigateToActionsBottomSheet()
},
onAllExpandedMessagesLoaded = ::scrollToFirstUnseenMessage,
navigateToAttendeeBottomSheet = { attendees ->
safeNavigate(
resId = R.id.attendeesBottomSheetDialog,
args = AttendeesBottomSheetDialogArgs(attendees.toTypedArray()).toBundle(),
)
},
navigateToNewMessageActivity = { twoPaneViewModel.navigateToNewMessage(mailToUri = it) },
navigateToAttendeeBottomSheet = { attendees -> twoPaneViewModel.navigateToAttendees(attendees.toTypedArray()) },
promptLink = { data, type ->
// When adding a phone number to contacts, Google decodes this value in case it's url-encoded. But I could not
// reproduce this issue when manually creating a url-encoded href. If this is triggered, fix it by also
Expand Down Expand Up @@ -316,22 +338,25 @@ class ThreadFragment : Fragment() {
threadViewModel.failedMessagesUids.observe(viewLifecycleOwner, threadAdapter::updateFailedMessages)
}

private fun observeQuickActionBarClicks() = with(twoPaneViewModel) {
private fun observeQuickActionBarClicks() {
threadViewModel.quickActionBarClicks.observe(viewLifecycleOwner) { (threadUid, lastMessageToReplyTo, menuId) ->
when (menuId) {
R.id.quickActionReply -> replyTo(lastMessageToReplyTo)
R.id.quickActionForward -> {
navigateToNewMessage(
twoPaneViewModel.navigateToNewMessage(
draftMode = DraftMode.FORWARD,
previousMessageUid = lastMessageToReplyTo.uid,
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
)
}
R.id.quickActionMenu -> {
navigateToThreadActions(
threadUid = threadUid,
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
messageUidToReplyTo = lastMessageToReplyTo.uid,
safeNavigate(
resId = R.id.threadActionsBottomSheetDialog,
args = ThreadActionsBottomSheetDialogArgs(
threadUid = threadUid,
shouldLoadDistantResources = shouldLoadDistantResources(lastMessageToReplyTo.uid),
messageUidToReplyTo = lastMessageToReplyTo.uid,
).toBundle(),
)
}
}
Expand Down Expand Up @@ -450,26 +475,33 @@ class ThreadFragment : Fragment() {
scheduleDownloadManager(url, name)
}

private fun replyTo(message: Message) = with(twoPaneViewModel) {
private fun replyTo(message: Message) {

val shouldLoadDistantResources = shouldLoadDistantResources(message.uid)

if (message.getRecipientsForReplyTo(replyAll = true).second.isEmpty()) {
navigateToNewMessage(
twoPaneViewModel.navigateToNewMessage(
draftMode = DraftMode.REPLY,
previousMessageUid = message.uid,
shouldLoadDistantResources = shouldLoadDistantResources,
)
} else {
navigateToReply(message.uid, shouldLoadDistantResources)
safeNavigate(
resId = R.id.replyBottomSheetDialog,
args = ReplyBottomSheetDialogArgs(message.uid, shouldLoadDistantResources).toBundle(),
)
}
}

private fun Message.navigateToActionsBottomSheet() {
twoPaneViewModel.navigateToMessageAction(
messageUid = uid,
isThemeTheSame = threadAdapter.isThemeTheSameMap[uid] ?: return,
shouldLoadDistantResources = shouldLoadDistantResources(uid),
safeNavigate(
resId = R.id.messageActionsBottomSheetDialog,
args = MessageActionsBottomSheetDialogArgs(
messageUid = uid,
threadUid = twoPaneViewModel.currentThreadUid.value ?: return,
isThemeTheSame = threadAdapter.isThemeTheSameMap[uid] ?: return,
shouldLoadDistantResources = shouldLoadDistantResources(uid),
).toBundle(),
)
}

Expand Down Expand Up @@ -548,6 +580,10 @@ class ThreadFragment : Fragment() {

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

private fun safeNavigate(@IdRes resId: Int, args: Bundle) {
twoPaneViewModel.navArgs.value = NavData(resId, args)
}

enum class HeaderState {
ELEVATED,
LOWERED,
Expand Down

0 comments on commit bc52681

Please sign in to comment.