Skip to content

Commit

Permalink
Split this XXX characters long line (which is greater than 130 author…
Browse files Browse the repository at this point in the history
…ized).
  • Loading branch information
KevinBoulongne committed Jun 4, 2024
1 parent d233257 commit bccf294
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ internal class BodyCleaner {
)
.addAttributes(":all", "class", "dir", "id", "style")
.addAttributes("a", "name")
// Allow all URI schemes in links. Removing all protocols makes the list of protocols empty which means allow all protocols
// Allow all URI schemes in links.
// Removing all protocols makes the list of protocols empty, which means allow all protocols.
.removeProtocols("a", "href", "ftp", "http", "https", "mailto")
.addAttributes("area", "alt", "coords", "href", "shape")
.addProtocols("area", "href", "http", "https")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import io.realm.kotlin.serializers.RealmListKSerializer
import io.realm.kotlin.types.RealmList
import kotlinx.serialization.json.*

// Documentation: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-transforming-serializer/
// Documentation on KotlinLang.org : https://chk.me/kYZiqN0
object FlatteningSubBodiesSerializer : JsonTransformingSerializer<RealmList<SubBody>>(
RealmListKSerializer(SubBody.serializer()),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,9 @@ class RefreshController @Inject constructor(
if (uidsCount < Utils.PAGE_SIZE) {
it.theEndIsReached() // If we didn't even get 1 full page, it means we already reached the end.
} else {
it.resetHistoryInfo() // If the Folder has been emptied, and the end isn't reached yet, we need to reset history info, so we'll be able to get all Messages again.
// If the Folder has been emptied, and the end isn't reached yet, we need
// to reset history info, so we'll be able to get all Messages again.
it.resetHistoryInfo()
}
}
}
Expand Down Expand Up @@ -666,25 +668,7 @@ class RefreshController @Inject constructor(
addedMessagesUids.add(remoteMessage.shortUid)

val newThread = if (isConversationMode) {

// Other pre-existing Threads that will also require this Message and will provide the prior Messages for this new Thread.
val existingThreads = ThreadController.getThreads(remoteMessage.messageIds, realm = this)
val existingMessages = getExistingMessages(existingThreads)

// Some Messages don't have references to all previous Messages of the Thread (ex: these from the iOS Mail app).
// Because we are missing the links between Messages, it will create multiple Threads for the same Folder.
// Hence, we need to find these duplicates.
val isThereDuplicatedThreads = isThereDuplicatedThreads(remoteMessage.messageIds, existingThreads.count())

// Create Thread in this Folder
val thread = createNewThreadIfRequired(scope, remoteMessage, existingThreads, existingMessages)
// Update Threads in other Folders
updateOtherExistingThreads(scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)

// Now that all other existing Threads are updated, we need to remove the duplicated Threads.
if (isThereDuplicatedThreads) removeDuplicatedThreads(remoteMessage.messageIds, impactedThreadsManaged)

thread
createNewThread(scope, remoteMessage, impactedThreadsManaged)
} else {
remoteMessage.toThread()
}
Expand All @@ -707,6 +691,31 @@ class RefreshController @Inject constructor(
return impactedThreadsUnmanaged
}

private fun MutableRealm.createNewThread(
scope: CoroutineScope,
remoteMessage: Message,
impactedThreadsManaged: MutableSet<Thread>,
): Thread? {
// Other pre-existing Threads that will also require this Message and will provide the prior Messages for this new Thread.
val existingThreads = ThreadController.getThreads(remoteMessage.messageIds, realm = this)
val existingMessages = getExistingMessages(existingThreads)

// Some Messages don't have references to all previous Messages of the Thread (ex: these from the iOS Mail app).
// Because we are missing the links between Messages, it will create multiple Threads for the same Folder.
// Hence, we need to find these duplicates.
val isThereDuplicatedThreads = isThereDuplicatedThreads(remoteMessage.messageIds, existingThreads.count())

// Create Thread in this Folder
val thread = createNewThreadIfRequired(scope, remoteMessage, existingThreads, existingMessages)
// Update Threads in other Folders
updateOtherExistingThreads(scope, remoteMessage, existingThreads, existingMessages, impactedThreadsManaged)

// Now that all other existing Threads are updated, we need to remove the duplicated Threads.
if (isThereDuplicatedThreads) removeDuplicatedThreads(remoteMessage.messageIds, impactedThreadsManaged)

return thread
}

private fun MutableRealm.addRemoteMessageToFolder(
remoteMessage: Message,
folder: Folder,
Expand Down Expand Up @@ -954,7 +963,7 @@ class RefreshController @Inject constructor(

enum class RefreshMode {
REFRESH_FOLDER, /* Fetch activities, and also get old Messages until `NUMBER_OF_OLD_MESSAGES_TO_FETCH` is reached */
REFRESH_FOLDER_WITH_ROLE, /* Same as `NEW_MESSAGES`, but also check if other Folders need to be updated (Inbox, Sent, Draft, etc…) */
REFRESH_FOLDER_WITH_ROLE, /* Same, but also check if other Folders need to be updated (Inbox, Sent, Draft, etc…) */
ONE_PAGE_OF_OLD_MESSAGES, /* Get 1 page of old Messages */
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ThreadController @Inject constructor(
* - Handle duplicates using the existing logic.
* @param remoteThreads The list of API Threads that need to be processed.
* @param filterFolder The selected Folder on which we filter the Search.
* @return A list of search Threads. The search only returns Messages from SPAM or TRASH if we explicitly selected those folders
* @return List of search Threads. The search only returns Messages from SPAM or TRASH if we explicitly selected those folders
*/
suspend fun initAndGetSearchFolderThreads(
remoteThreads: List<Thread>,
Expand Down Expand Up @@ -245,7 +245,8 @@ class ThreadController @Inject constructor(
*
* @param messages List of messages for which heavy data needs to be fetched.
* @param realm The realm context in which the heavy data fetching and updates should occur.
* @param okHttpClient An optional OkHttpClient instance to use for making network requests. If not provided, a default client will be used.
* @param okHttpClient An optional OkHttpClient instance to use for making network requests.
* If not provided, a default client will be used.
*/
fun fetchMessagesHeavyData(
messages: List<Message>,
Expand Down Expand Up @@ -291,7 +292,7 @@ class ThreadController @Inject constructor(
}

}.onFailure {
// This `runCatching / onFailure` is here only to catch `OutOfMemoryError` when trying to deserialize very big Body
// This `onFailure` is here only to catch `OutOfMemoryError` when trying to deserialize very big Body.
handleFailure(localMessage.uid)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,15 @@ class Message : RealmObject {
val allFoldersAreSearch = _folders.all { it.id == SEARCH_FOLDER_ID }
val allFoldersAreTheSame = _folders.all { it.id == _folders.firstOrNull()?.id }
when {
allFoldersAreSearch -> "Message has multiple times the Search folder as parent, it should not be possible"
allFoldersAreTheSame -> "Message has multiple times the same parent folder, it should not be possible"
else -> "Message has multiple parent folders, it should not be possible"
allFoldersAreSearch -> {
"Message has multiple times the Search folder as parent, it should not be possible"
}
allFoldersAreTheSame -> {
"Message has multiple times the same parent folder, it should not be possible"
}
else -> {
"Message has multiple parent folders, it should not be possible"
}
}
}
Sentry.captureMessage(sentryMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ class EmailContextualMenuAlertDialog @Inject constructor(

override val items = listOf(
ContextualItem(R.string.contextMenuEmailOpen) { email, _ ->
val mailToUri = Uri.parse(WebView.SCHEME_MAILTO + email)
(activityContext as MainActivity).navigateToNewMessageActivity(NewMessageActivityArgs(mailToUri = mailToUri).toBundle())
(activityContext as MainActivity).navigateToNewMessageActivity(
NewMessageActivityArgs(
mailToUri = Uri.parse(WebView.SCHEME_MAILTO + email),
).toBundle(),
)
},
ContextualItem(R.string.contextMenuEmailCopy) { email, snackbarManager ->
activityContext.copyStringToClipboard(email, R.string.snackbarEmailCopiedToClipboard, snackbarManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,18 @@ class ThreadListAdapter @Inject constructor(

override fun onBindViewHolder(item: Any, viewHolder: ThreadListViewHolder, position: Int) = with(viewHolder.binding) {
when (getItemViewType(position)) {
DisplayType.THREAD.layout -> (this as CardviewThreadItemBinding).displayThread(item as Thread, position)
DisplayType.DATE_SEPARATOR.layout -> (this as ItemThreadDateSeparatorBinding).displayDateSeparator(item as String)
DisplayType.FLUSH_FOLDER_BUTTON.layout -> (this as ItemThreadFlushFolderButtonBinding).displayFlushFolderButton(item as FolderRole)
DisplayType.LOAD_MORE_BUTTON.layout -> (this as ItemThreadLoadMoreButtonBinding).displayLoadMoreButton()
DisplayType.THREAD.layout -> {
(this as CardviewThreadItemBinding).displayThread(item as Thread, position)
}
DisplayType.DATE_SEPARATOR.layout -> {
(this as ItemThreadDateSeparatorBinding).displayDateSeparator(item as String)
}
DisplayType.FLUSH_FOLDER_BUTTON.layout -> {
(this as ItemThreadFlushFolderButtonBinding).displayFlushFolderButton(item as FolderRole)
}
DisplayType.LOAD_MORE_BUTTON.layout -> {
(this as ItemThreadLoadMoreButtonBinding).displayLoadMoreButton()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.ernestoyaquello.dragdropswiperecyclerview.DragDropSwipeRecyclerView.ListOrientation
import com.ernestoyaquello.dragdropswiperecyclerview.DragDropSwipeRecyclerView.ListOrientation.DirectionFlag
import com.ernestoyaquello.dragdropswiperecyclerview.DragDropSwipeRecyclerView.ListOrientation.VERTICAL_LIST_WITH_VERTICAL_DRAGGING
import com.ernestoyaquello.dragdropswiperecyclerview.listener.OnItemSwipeListener
import com.ernestoyaquello.dragdropswiperecyclerview.listener.OnItemSwipeListener.SwipeDirection
import com.ernestoyaquello.dragdropswiperecyclerview.listener.OnListScrollListener
Expand Down Expand Up @@ -330,7 +330,7 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
binding.threadsList.apply {
adapter = threadListAdapter
layoutManager = LinearLayoutManager(context)
orientation = VERTICAL_LIST_WITH_VERTICAL_DRAGGING
orientation = ListOrientation.VERTICAL_LIST_WITH_VERTICAL_DRAGGING
disableDragDirection(DirectionFlag.UP)
disableDragDirection(DirectionFlag.DOWN)
disableDragDirection(DirectionFlag.RIGHT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,19 @@ class ThreadViewModel @Inject constructor(

return formatLists(messages) { index, messageUid ->
when {
index == 0 -> MessageBehavior.DISPLAYED // First Message
previousBlock.contains(messageUid) && isStillInBlock -> MessageBehavior.COLLAPSED // All Messages already in block
index == 0 -> { // First Message
MessageBehavior.DISPLAYED
}
previousBlock.contains(messageUid) && isStillInBlock -> { // All Messages already in block
MessageBehavior.COLLAPSED
}
!previousBlock.contains(messageUid) && isStillInBlock -> { // First Message not in block
isStillInBlock = false
MessageBehavior.FIRST_AFTER_BLOCK
}
else -> MessageBehavior.DISPLAYED // All following Messages
else -> { // All following Messages
MessageBehavior.DISPLAYED
}
}
}
}
Expand Down Expand Up @@ -190,7 +196,7 @@ class ThreadViewModel @Inject constructor(
* 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 = with(threadState) {
return superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden for any reason, we mustn't ever display it again
return superCollapsedBlock?.shouldBeDisplayed == true && // If the Block was hidden, we mustn't ever display it again
!hasSuperCollapsedBlockBeenClicked && // 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 Expand Up @@ -307,7 +313,7 @@ class ThreadViewModel @Inject constructor(
// TODO: A race condition exists between the two notify in the ThreadAdapter.
// The 1st notify involves sending Messages to the adapter, while the 2nd notify entails retrieving
// Messages' heavy data and subsequently notifying the adapter with the `uids` of failed Messages.
// Ideally, the adapter should process the 1st notify before the 2nd one, but occasionally, the order is reversed.
// Ideally, the adapter should process the 1st notify before the 2nd one, but occasionally the order is reversed.
// Consequently, it appears that the adapter disregards the 2nd notify,
// leading to an infinite shimmering effect that we cannot escape from.
delay(100L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ class CalendarEventBannerView @JvmOverloads constructor(
"${startDate.formatDateAndHour()} -\n${displayEndDate.formatDateAndHour()}"
}
else -> {
"${startDate.formatMediumDate()} - ${displayEndDate.formatMediumDate()}\n${context.getString(R.string.calendarAllDayLong)}"
"${startDate.formatMediumDate()} - ${displayEndDate.formatMediumDate()}\n" +
context.getString(R.string.calendarAllDayLong)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class NewMessageAiManager @Inject constructor(
}

fun observeAiPromptStatus() {
aiViewModel.aiPromptOpeningStatus.observe(viewLifecycleOwner) { (shouldDisplay, shouldResetContent, becauseOfGeneration) ->
if (shouldDisplay) onAiPromptOpened(shouldResetContent) else onAiPromptClosed(becauseOfGeneration)
aiViewModel.aiPromptOpeningStatus.observe(viewLifecycleOwner) {
if (it.isOpened) onAiPromptOpened(it.shouldResetPrompt) else onAiPromptClosed(it.becauseOfGeneration)
}
}

Expand Down

0 comments on commit bccf294

Please sign in to comment.