Skip to content

Commit

Permalink
Merge pull request #1728 from Infomaniak/prepare-code
Browse files Browse the repository at this point in the history
Clean code before optimizing ThreadState handling
  • Loading branch information
KevinBoulongne authored Mar 1, 2024
2 parents 151efd4 + 340cbb5 commit cc80d26
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.infomaniak.mail.data.models.correspondent.Recipient
import com.infomaniak.mail.data.models.getMessages.ActivitiesResult.MessageFlags
import com.infomaniak.mail.data.models.thread.Thread
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.MessageBodyUtils
import com.infomaniak.mail.utils.MessageBodyUtils.SplitBody
import com.infomaniak.mail.utils.extensions.toRealmInstant
import com.infomaniak.mail.utils.extensions.toShortUid
import io.realm.kotlin.ext.*
Expand Down Expand Up @@ -143,7 +143,7 @@ class Message : RealmObject {
// correctly, as the relationship between Body and Message relies on a Realm query.
@Transient
@Ignore
var splitBody: MessageBodyUtils.SplitBody? = null
var splitBody: SplitBody? = null
@Transient
@Ignore
var shouldHideDivider: Boolean = false
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class MainActivity : BaseActivity() {
}

private val showSendingSnackbarTimer: CountDownTimer by lazy {
Utils.createRefreshTimer(1_000L) { snackbarManager.setValue(getString(R.string.snackbarEmailSending)) }
Utils.createRefreshTimer(milliseconds = 1_000L) { snackbarManager.setValue(getString(R.string.snackbarEmailSending)) }
}

private val newMessageActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
}

// If we are coming from a Notification, we need to navigate to ThreadFragment.
openThreadAndResetBackup(threadUid)
openThreadAndResetItsState(threadUid)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ abstract class TwoPaneFragment : Fragment() {
trackNewMessageEvent(OPEN_FROM_DRAFT_NAME)
twoPaneViewModel.openDraft(thread)
} else {
openThreadAndResetBackup(thread.uid)
openThreadAndResetItsState(thread.uid)
}
}

fun openThreadAndResetBackup(threadUid: String) {
getRightPane()?.getFragment<ThreadFragment?>()?.resetThreadBackupCache()
fun openThreadAndResetItsState(threadUid: String) {
getRightPane()?.getFragment<ThreadFragment?>()?.resetThreadState()
twoPaneViewModel.openThread(threadUid)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class ThreadAdapter(
}

private fun MessageViewHolder.bindCalendarEvent(message: Message) {

val calendarAttachment = message.calendarAttachment ?: return
val calendarEvent = message.latestCalendarEventResponse?.calendarEvent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ class ThreadFragment : Fragment() {
_binding = null
}

fun resetThreadBackupCache() {
threadViewModel.resetThreadBackupCache()
fun resetThreadState() = with(threadViewModel) {
isExpandedMap = mutableMapOf()
isThemeTheSameMap = mutableMapOf()
hasSuperCollapsedBlockBeenClicked = false
verticalScroll = null
}

private fun setupUi() = with(binding) {
Expand Down Expand Up @@ -205,11 +208,11 @@ class ThreadFragment : Fragment() {
}
}

private fun setupAdapter() = with(binding.messagesList) {
private fun setupAdapter() = with(threadViewModel) {

adapter = ThreadAdapter(
binding.messagesList.adapter = ThreadAdapter(
shouldLoadDistantResources = shouldLoadDistantResources(),
isCalendarEventExpandedMap = threadViewModel.isCalendarEventExpandedMap,
isCalendarEventExpandedMap = isCalendarEventExpandedMap,
threadAdapterState = object : ThreadAdapterState {
override var isExpandedMap by threadViewModel::isExpandedMap
override var isThemeTheSameMap by threadViewModel::isThemeTheSameMap
Expand All @@ -234,7 +237,7 @@ class ThreadFragment : Fragment() {
},
onDeleteDraftClicked = { message ->
trackMessageActionsEvent("deleteDraft")
mainViewModel.currentMailbox.value?.let { mailbox -> threadViewModel.deleteDraft(message, mailbox) }
mainViewModel.currentMailbox.value?.let { mailbox -> deleteDraft(message, mailbox) }
},
onAttachmentClicked = {
trackAttachmentActionsEvent("open")
Expand Down Expand Up @@ -276,13 +279,13 @@ class ThreadFragment : Fragment() {
navigateToDownloadProgressDialog(attachment, attachmentIntentType, ThreadFragment::class.java.name)
},
replyToCalendarEvent = { attendanceState, message ->
threadViewModel.replyToCalendarEvent(
replyToCalendarEvent(
attendanceState,
message,
).observe(viewLifecycleOwner) { successfullyUpdated ->
if (successfullyUpdated) {
snackbarManager.setValue(getString(R.string.snackbarCalendarChoiceSent))
threadViewModel.fetchCalendarEvents(listOf(message), forceFetch = true)
fetchCalendarEvents(listOf(message), forceFetch = true)
} else {
snackbarManager.setValue(getString(R.string.errorCalendarChoiceCouldNotBeSent))
threadAdapter.undoUserAttendanceClick(message)
Expand All @@ -307,14 +310,14 @@ class ThreadFragment : Fragment() {
),
)

addItemDecoration(
binding.messagesList.addItemDecoration(
DividerItemDecorator(
divider = InsetDrawable(dividerDrawable(context), 0),
shouldIgnoreView = { view -> view.tag == ThreadAdapter.IGNORE_DIVIDER_TAG },
),
)

recycledViewPool.setMaxRecycledViews(0, 0)
binding.messagesList.recycledViewPool.setMaxRecycledViews(0, 0)
threadAdapter.stateRestorationPolicy = StateRestorationPolicy.PREVENT_WHEN_EMPTY
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ class ThreadViewModel @Inject constructor(
AccountUtils.currentMailboxId,
).map { it.obj }.asLiveData(ioCoroutineContext)

fun resetThreadBackupCache() {
isExpandedMap = mutableMapOf()
isThemeTheSameMap = mutableMapOf()
hasSuperCollapsedBlockBeenClicked = false
verticalScroll = null
}

fun resetMessagesRelatedCache() {
treatedMessagesForCalendarEvent.clear()
isCalendarEventExpandedMap.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.navigation.fragment.navArgs
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.infomaniak.lib.core.utils.context
import com.infomaniak.lib.core.utils.safeBinding
import com.infomaniak.lib.core.utils.toPx
import com.infomaniak.lib.core.views.DividerItemDecorator
import com.infomaniak.mail.R
import com.infomaniak.mail.databinding.BottomSheetAttendeesBinding
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/infomaniak/mail/utils/SharedUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class SharedUtils @Inject constructor(
* @param mailbox The Mailbox where the Threads & Messages are located
* @param threads The Threads to mark as read
* @param message The Message to mark as read
* @param started The callback for when the refresh of Threads starts
* @param stopped The callback for when the refresh of Threads ends
* @param callbacks The callbacks for when the refresh of Threads begins/ends
* @param shouldRefreshThreads Sometimes, we don't want to refresh Threads after doing this action. For example, when replying to a Message.
*/
suspend fun markAsSeen(
Expand Down

0 comments on commit cc80d26

Please sign in to comment.