@@ -22,7 +22,7 @@ import android.content.res.Resources
22
22
import android.graphics.Paint
23
23
import android.text.StaticLayout
24
24
import android.text.TextPaint
25
- import android.text.TextUtils.*
25
+ import android.text.TextUtils.TruncateAt
26
26
import androidx.annotation.ColorRes
27
27
import androidx.core.content.res.ResourcesCompat
28
28
import com.infomaniak.mail.MatomoMail.trackExternalEvent
@@ -34,7 +34,6 @@ import com.infomaniak.mail.utils.extensions.formatSubject
34
34
import com.infomaniak.mail.utils.extensions.postfixWithTag
35
35
import javax.inject.Inject
36
36
import javax.inject.Singleton
37
- import com.infomaniak.lib.core.R as RCore
38
37
39
38
@Singleton
40
39
class SubjectFormatter @Inject constructor(private val context : Context ) {
@@ -87,14 +86,14 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
87
86
val folderName = getFolderName(thread)
88
87
if (folderName.isEmpty()) return previousContent
89
88
90
- val ellipsizeTag = getEllipsizeConfiguration(previousContent, folderName)
89
+ val ellipsizeTag = getEllipsizeConfiguration(folderName)
91
90
return postFixWithFolder(previousContent, folderName, ellipsizeTag)
92
91
}
93
92
94
93
private fun postFixWithFolder (
95
94
previousContent : CharSequence ,
96
95
folderName : String ,
97
- ellipsizeConfiguration : EllipsizeConfiguration ,
96
+ ellipsizeConfiguration : EllipsizeConfiguration ? ,
98
97
) = context.postfixWithTag(
99
98
previousContent,
100
99
folderName,
@@ -104,33 +103,16 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
104
103
105
104
private fun getFolderName (thread : Thread ) = if (thread.messages.size > 1 ) " " else thread.folderName
106
105
107
- private fun getEllipsizeConfiguration (previousContent : CharSequence , tag : String ): EllipsizeConfiguration {
106
+ private fun getEllipsizeConfiguration (tag : String ): EllipsizeConfiguration ? {
108
107
val paddingsInPixels = (context.resources.getDimension(R .dimen.threadHorizontalMargin) * 2 ).toInt()
109
108
val width = Resources .getSystem().displayMetrics.widthPixels - paddingsInPixels
110
- val tagsTextPaint = getTagsPaint(context)
111
-
112
- val layoutBeforeAddingTag = StaticLayout .Builder .obtain(
113
- previousContent,
114
- 0 ,
115
- previousContent.length,
116
- tagsTextPaint,
117
- width,
118
- ).build()
119
-
120
- // Colors are not taken into account here. We only call postfixWithTag to compute the layout size
121
- val stringAfterAddingTag = context.postfixWithTag(
122
- previousContent,
123
- tag,
124
- TagColor (RCore .color.black, RCore .color.black),
125
- )
126
109
127
- val layoutAfterAddingTag =
128
- StaticLayout .Builder .obtain(stringAfterAddingTag , 0 , stringAfterAddingTag .length, tagsTextPaint , width).build()
110
+ val tagTextPaint = getTagsPaint(context)
111
+ val layoutWithTag = StaticLayout .Builder .obtain(tag , 0 , tag .length, tagTextPaint , width).build()
129
112
130
- val positionLastChar = layoutBeforeAddingTag.getPrimaryHorizontal(previousContent.length).toInt()
131
- val isLinesCountDifferent = layoutAfterAddingTag.lineCount != layoutBeforeAddingTag.lineCount
132
- val maxWidth = if (isLinesCountDifferent) width else width - positionLastChar
133
- return EllipsizeConfiguration (maxWidth, TruncateAt .MIDDLE , isLinesCountDifferent, tagsTextPaint)
113
+ return layoutWithTag.takeIf { it.lineCount > 1 }?.let {
114
+ EllipsizeConfiguration (width, TruncateAt .MIDDLE , withNewLine = true )
115
+ }
134
116
}
135
117
136
118
data class SubjectData (
@@ -144,7 +126,6 @@ class SubjectFormatter @Inject constructor(private val context: Context) {
144
126
val maxWidth : Int ,
145
127
val truncateAt : TruncateAt = TruncateAt .MIDDLE ,
146
128
val withNewLine : Boolean = false ,
147
- val tagTextPaint : TextPaint ,
148
129
)
149
130
150
131
data class TagColor (@ColorRes val backgroundColorRes : Int , @ColorRes val textColorRes : Int )
0 commit comments