Repository: thundernest/k-9
Issue: Don't create AbsoluteSizeSpan when using default font sizes

Right now we always create an `AbsoluteSizeSpan` to set the font size of the subject or sender (depending on settings) in the second line of message list items.

https://github.com/k9mail/k-9/blob/47629ae35efcd8f8d1c0005e9f1a18eb156adf02/app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt#L293-L299

We use `-1` as magic value to mean "use default font size" in `FontSizes`. In such a case an `AbsoluteSizeSpan` with a font size of `-1` is created. This happens to do the right thing in that it does nothing. But we should change our code to not create the span if the default font size is to be used.

Changed files:
app/ui/src/test/java/com/fsck/k9/fragment/MessageListAdapterTest.kt, app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt

diff --git a/app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt b/app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
index ca06d24a4..f0686a1f9 100644
--- a/app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
+++ b/app/ui/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
@@ -20,6 +20,7 @@ import android.widget.TextView
 import androidx.core.view.isVisible
 
 import com.fsck.k9.Account
+import com.fsck.k9.FontSizes
 import com.fsck.k9.ui.R
 import com.fsck.k9.Preferences
 import com.fsck.k9.contacts.ContactPictureLoader
@@ -251,27 +252,30 @@ class MessageListAdapter internal constructor(
             sigil: String
     ) {
         val previewText = preview.text as Spannable
-        previewText.setSpan(buildSenderSpan(), 0, beforePreviewText.length + sigil.length,
-                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+
+        val beforePreviewLength = beforePreviewText.length + sigil.length
+        addBeforePreviewSpan(previewText, beforePreviewLength)
 
         // Set span (color) for preview message
         previewText.setSpan(
                 ForegroundColorSpan(previewTextColor),
-                beforePreviewText.length + sigil.length,
+                beforePreviewLength,
                 previewText.length,
                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
         )
     }
 
-    /**
-     * Create a span section for the sender, and assign the correct font size and weight
-     */
-    private fun buildSenderSpan(): AbsoluteSizeSpan {
-        val fontSize = if (appearance.senderAboveSubject)
+    private fun addBeforePreviewSpan(text: Spannable, length: Int) {
+        val fontSize = if (appearance.senderAboveSubject) {
             appearance.fontSizes.messageListSubject
-        else
+        } else {
             appearance.fontSizes.messageListSender
-        return AbsoluteSizeSpan(fontSize, true)
+        }
+
+        if (fontSize != FontSizes.FONT_DEFAULT) {
+            val span = AbsoluteSizeSpan(fontSize, true)
+            text.setSpan(span, 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
+        }
     }
 
     private fun fetchCounterPartyAddress(
diff --git a/app/ui/src/test/java/com/fsck/k9/fragment/MessageListAdapterTest.kt b/app/ui/src/test/java/com/fsck/k9/fragment/MessageListAdapterTest.kt
index 48e283e84..1bb674316 100644
--- a/app/ui/src/test/java/com/fsck/k9/fragment/MessageListAdapterTest.kt
+++ b/app/ui/src/test/java/com/fsck/k9/fragment/MessageListAdapterTest.kt
@@ -364,7 +364,6 @@ class MessageListAdapterTest : RobolectricTest() {
     }
 
     @Test
-    @Ignore("Currently failing. See issue #4153.")
     fun withoutSenderAboveSubjectAndDefaultFontSize_shouldNotSetTextSizeSpanInSecondLineView() {
         val adapter = createAdapter(
                 fontSizes = createFontSizes(sender = FONT_DEFAULT),
@@ -389,7 +388,6 @@ class MessageListAdapterTest : RobolectricTest() {
     }
 
     @Test
-    @Ignore("Currently failing. See issue #4153.")
     fun withSenderAboveSubjectAndDefaultFontSize_shouldNotSetTextSizeSpanInSecondLineView() {
         val adapter = createAdapter(
                 fontSizes = createFontSizes(subject = FONT_DEFAULT),