Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Feb 15, 2024
1 parent db6dd3b commit 4ad3340
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.infomaniak.mail.data.cache.mailboxContent
import android.content.Context
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.RealmDatabase
import com.infomaniak.mail.data.cache.mailboxContent.FolderController.Companion.getFolder
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.data.models.Folder.FolderRole
import com.infomaniak.mail.data.models.message.Message
Expand Down Expand Up @@ -115,16 +114,15 @@ class ThreadController @Inject constructor(
val searchFolder = FolderController.getOrCreateSearchFolder(realm = this)
remoteThreads.map { remoteThread ->
ensureActive()
val firstMessageFolderId = remoteThread.messages.single().folderId
if (remoteThread.messages.size == 1) {
getFolder(firstMessageFolderId, this@writeBlocking)?.let { folder ->
remoteThread.folderName = folder.getLocalizedName(context)
}
}

remoteThread.isFromSearch = true

val folderId = if (remoteThread.messages.count() == 1) {
remoteThread.messages.single().folderId
val firstMessageFolderId = remoteThread.messages.single().folderId
FolderController.getFolder(firstMessageFolderId, this@writeBlocking)?.let { folder ->
remoteThread.folderName = folder.getLocalizedName(context)
}
firstMessageFolderId
} else {
filterFolder!!.id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ class ThreadListAdapter @Inject constructor(
private fun shouldDisplayFolderName(folderName: String) = isFolderNameVisible && folderName.isNotEmpty()

private fun CardviewThreadItemBinding.displayThread(thread: Thread, position: Int) {
resetFolderNameVisibility()
displayFolderName(thread)

refreshCachedSelectedPosition(thread.uid, position) // If item changed position, update cached position.
setupThreadDensityDependentUi()
displayAvatar(thread)

displayFolderName(thread)

with(thread) {
expeditor.text = formatRecipientNames(computeDisplayedRecipients())
mailSubject.text = context.formatSubject(subject)
Expand Down Expand Up @@ -236,15 +236,18 @@ class ThreadListAdapter @Inject constructor(
}

private fun CardviewThreadItemBinding.displayFolderName(thread: Thread) {
resetFolderNameVisibility()

val folderNameView = if (localSettings.threadDensity == COMPACT) folderNameCompactMode else folderNameExpandMode
if (shouldDisplayFolderName(thread.folderName)) {
folderNameView.isVisible = true
folderNameView.text = context.postfixWithTag(
tag = thread.folderName,
tagColor = TagColor(R.color.folderNameBackground, R.color.folderNameTextColor),
tagColor = TagColor(R.color.tagBackground, R.color.tagTextColor),
ellipsizeConfiguration = SubjectFormatter.EllipsizeConfiguration(
maxWidth = context.resources.getDimension(R.dimen.subjectTagMaxSize).toInt(),
truncateAt = TextUtils.TruncateAt.END
maxWidth = context.resources.getDimension(R.dimen.folderNameTagMaxSize).toInt(),
truncateAt = TextUtils.TruncateAt.END,
tagTextPaint = SubjectFormatter.getTagsPaint(context)
),
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,14 @@ import androidx.core.content.res.ResourcesCompat
import com.infomaniak.mail.R

/**
* A span to create a rounded background on a text.
*
* If radius is set, it generates a rounded background.
* If radius is null, it generates a circle background.
* A span to create a rounded background with the specified radius on a text.
*/
class RoundedBackgroundSpan(
private val backgroundColor: Int,
private val textColor: Int,
private val textTypeface: Typeface,
private val fontSize: Float,
private val cornerRadius: Float = CORNER_RADIUS
private val cornerRadius: Float
) : ReplacementSpan(), LineHeightSpan {

override fun getSize(paint: Paint, text: CharSequence?, start: Int, end: Int, fm: FontMetricsInt?): Int {
Expand Down Expand Up @@ -102,12 +99,5 @@ class RoundedBackgroundSpan(
private const val LEFT_MARGIN = 4
private const val PADDING = 16
private const val VERTICAL_OFFSET = 4
private const val CORNER_RADIUS = 10f

fun getTagsPaint(context: Context) = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
color = ResourcesCompat.getColor(context.resources, R.color.folderNameTextColor, null)
textSize = context.resources.getDimension(R.dimen.externalTagTextSize)
typeface = ResourcesCompat.getFont(context, R.font.tag_font)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package com.infomaniak.mail.ui.main.thread

import android.content.Context
import android.content.res.Resources
import android.graphics.Paint
import android.text.StaticLayout
import android.text.TextPaint
import android.text.TextUtils
import androidx.core.content.res.ResourcesCompat
import com.infomaniak.mail.MatomoMail.trackExternalEvent
import com.infomaniak.mail.R
import com.infomaniak.mail.data.models.thread.Thread
Expand All @@ -31,7 +34,7 @@ import com.infomaniak.mail.utils.extensions.formatSubject
import com.infomaniak.mail.utils.extensions.postfixWithTag
import javax.inject.Inject
import javax.inject.Singleton
import com.infomaniak.lib.core.R as CoreR
import com.infomaniak.lib.core.R as RCore

@Singleton
class SubjectFormatter @Inject constructor(private val context: Context) {
Expand Down Expand Up @@ -97,20 +100,20 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
private fun postFixWithFolder(
previousContent: CharSequence,
folderName: String,
ellipsizeTag: EllipsizeConfiguration
ellipsizeConfiguration: EllipsizeConfiguration
) = context.postfixWithTag(
previousContent,
folderName,
TagColor(R.color.folderNameBackground, R.color.folderNameTextColor),
ellipsizeTag
TagColor(R.color.tagBackground, R.color.tagTextColor),
ellipsizeConfiguration
)

private fun getFolderName(thread: Thread) = if (thread.messages.size > 1) "" else thread.folderName

private fun getEllipsizeConfiguration(previousContent: CharSequence, tag: String): EllipsizeConfiguration {
val paddingsInPixels = (context.resources.getDimension(CoreR.dimen.marginStandard) * 2).toInt()
val paddingsInPixels = (context.resources.getDimension(R.dimen.threadHorizontalMargin) * 2).toInt()
val width = Resources.getSystem().displayMetrics.widthPixels - paddingsInPixels
val tagsTextPaint = RoundedBackgroundSpan.getTagsPaint(context)
val tagsTextPaint = getTagsPaint(context)

val layoutBeforeAddingTag = StaticLayout.Builder.obtain(
previousContent,
Expand All @@ -120,13 +123,20 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
width
).build()

val fullString = "$previousContent $tag"
val layoutAfterAddingTag = StaticLayout.Builder.obtain(fullString, 0, fullString.length, tagsTextPaint, width).build()
// Colors are not used but here, we just need to compute the tag layouts
val stringAfterAddingTag = context.postfixWithTag(
previousContent,
tag,
TagColor(RCore.color.black, RCore.color.black)
)

val layoutAfterAddingTag =
StaticLayout.Builder.obtain(stringAfterAddingTag, 0, stringAfterAddingTag.length, tagsTextPaint, width).build()

val positionLastChar = layoutBeforeAddingTag.getPrimaryHorizontal(previousContent.length).toInt()
val linesCountDifferent = layoutAfterAddingTag.lineCount != layoutBeforeAddingTag.lineCount
val maxWidth = if (layoutAfterAddingTag.lineCount != layoutBeforeAddingTag.lineCount) width else width - positionLastChar
return EllipsizeConfiguration(maxWidth, TextUtils.TruncateAt.MIDDLE, linesCountDifferent)
val maxWidth = if (linesCountDifferent) width else width - positionLastChar
return EllipsizeConfiguration(maxWidth, TextUtils.TruncateAt.MIDDLE, linesCountDifferent, tagsTextPaint)
}

data class SubjectData(
Expand All @@ -139,6 +149,15 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
data class EllipsizeConfiguration(
val maxWidth: Int,
val truncateAt: TextUtils.TruncateAt = TextUtils.TruncateAt.MIDDLE,
val withNewLine: Boolean = false
val withNewLine: Boolean = false,
val tagTextPaint: TextPaint
)

companion object {

fun getTagsPaint(context: Context) = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
textSize = context.resources.getDimension(R.dimen.tagTextSize)
typeface = ResourcesCompat.getFont(context, R.font.tag_font)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import com.infomaniak.mail.ui.main.folder.ThreadListAdapter
import com.infomaniak.mail.ui.main.thread.MessageWebViewClient
import com.infomaniak.mail.ui.main.thread.RoundedBackgroundSpan
import com.infomaniak.mail.ui.main.thread.SubjectFormatter
import com.infomaniak.mail.ui.main.thread.SubjectFormatter.Companion.getTagsPaint
import com.infomaniak.mail.ui.main.thread.ThreadFragment.HeaderState
import com.infomaniak.mail.utils.*
import com.infomaniak.mail.utils.Utils
Expand Down Expand Up @@ -516,7 +517,7 @@ fun Context.postfixWithTag(

fun getConfiguredTag(): CharSequence {
return if (ellipsizeConfiguration != null) {
val textPaint = RoundedBackgroundSpan.getTagsPaint(this)
val textPaint = getTagsPaint(this)
with(ellipsizeConfiguration) {
val tagNameLayout =
StaticLayout.Builder.obtain(tag, 0, tag.length, textPaint, maxWidth)
Expand Down Expand Up @@ -555,14 +556,14 @@ private fun Spannable.setTagSpan(
val backgroundColor = context.getColor(backgroundColorRes)
val textColor = context.getColor(textColorRes)
val textTypeface = ResourcesCompat.getFont(context, R.font.tag_font)!!
val textSize = context.resources.getDimensionPixelSize(R.dimen.externalTagTextSize).toFloat()
val textSize = context.resources.getDimensionPixelSize(R.dimen.tagTextSize).toFloat()
setSpan(
RoundedBackgroundSpan(
backgroundColor = backgroundColor,
textColor = textColor,
textTypeface = textTypeface,
fontSize = textSize,
cornerRadius = context.resources.getDimension(R.dimen.subjectTagRadius)
cornerRadius = context.resources.getDimension(R.dimen.tagRadius)
),
startIndex,
endIndex,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/cardview_thread_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@id/mailSubject"
app:layout_constraintTop_toTopOf="@id/mailSubject"
tools:background="@color/grey_background"
tools:background="@color/tagBackground"
tools:gravity="center"
tools:text="Folder name"
tools:visibility="visible" />
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@
<color name="calendarHeaderBackground">@color/orca</color>

<!-- Thread -->
<color name="folderNameTextColor">@color/shark</color>
<color name="folderNameBackground">@color/orca</color>
<color name="tagTextColor">@color/shark</color>
<color name="tagBackground">@color/orca</color>

<!-- Swipe actions -->
<color name="swipeDelete">@color/redDestructiveAction</color>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@
<color name="manyAvatarsAdditionalPeopleBackground">@color/iconColorSecondaryText</color>

<!-- Thread -->
<color name="folderNameTextColor">@color/elephant</color>
<color name="folderNameBackground">@color/polar_bear</color>
<color name="tagTextColor">@color/elephant</color>
<color name="tagBackground">@color/polar_bear</color>

<!-- Swipe actions -->
<color name="swipeDelete">@color/redDestructiveAction</color>
Expand Down
13 changes: 8 additions & 5 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
<dimen name="noMailTextViewSize">300dp</dimen>
<dimen name="searchViewHeight">38dp</dimen>
<dimen name="smallCornerRadius">6dp</dimen>
<dimen name="subjectTagRadius">4dp</dimen>
<dimen name="subjectTagMaxSize">150dp</dimen>
<dimen name="userAvatarSizeLarge">40dp</dimen>
<dimen name="messageDividerHeight">6dp</dimen>
<dimen name="smallIconSize">12dp</dimen>
Expand All @@ -52,6 +50,7 @@
<dimen name="textButtonPrimaryHeight">56dp</dimen>
<dimen name="textButtonPrimaryMediumHeight">40dp</dimen>
<dimen name="mediumButtonHorizontalMargin">20dp</dimen>
<dimen name="threadHorizontalMargin">@dimen/marginStandard</dimen>

<!-- Menu Drawer -->
<dimen name="decoratedItemViewHeight">48dp</dimen>
Expand All @@ -61,12 +60,16 @@
<dimen name="decoratedItemConstraintMarginStart">@dimen/marginStandardSmall</dimen>
<dimen name="decoratedItemTextMarginStart">@dimen/marginStandardSmall</dimen>

<!-- Tag -->
<dimen name="tagRadius">4dp</dimen>
<dimen name="tagTextSize">12sp</dimen>

<!-- Search folder name tag -->
<dimen name="folderNameTagMaxSize">150dp</dimen>

<!-- Settings -->
<dimen name="dividerHorizontalPadding">16dp</dimen>

<!-- External tag -->
<dimen name="externalTagTextSize">12sp</dimen>

<!-- AI -->
<dimen name="scrimOpacity">0.32</dimen>
<integer name="aiPromptAnimationDuration">300</integer>
Expand Down

0 comments on commit 4ad3340

Please sign in to comment.