Repository: thundernest/k-9
Issue: When composing a message the "reply to" input is displayed and focused unexpectedly

### Checklist

- [X] I have used the search function to see if someone else has already submitted the same bug report.
- [X] I will describe the problem with as much detail as possible.

### App version

6.310

### Where did you get the app from?

_No response_

### Android version

doesn't matter

### Device model

_No response_

### Steps to reproduce

1. Open the screen to compose a message
2. Switch the screen orientation

### Expected behavior

The layout should look mostly the same before and after changing the screen orientation. 

### Actual behavior

The "reply to" field shows up and is focused when it wasn't before.

### Other

See also: https://forum.k9mail.app/t/new-mails-defaulting-to-reply-to-not-to-to/5484

Changed files:
app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/ReplyToPresenterTest.kt, app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToView.kt, app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToPresenter.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToPresenter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToPresenter.kt
index c8f33e007..5014bc5f2 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToPresenter.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToPresenter.kt
@@ -53,9 +53,7 @@ class ReplyToPresenter(private val view: ReplyToView) {
     }
 
     fun onNonRecipientFieldFocused() {
-        if (view.isVisible && view.getAddresses().isEmpty()) {
-            view.isVisible = false
-        }
+        view.hideIfBlank()
     }
 
     fun onSaveInstanceState(outState: Bundle) {
diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToView.kt b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToView.kt
index 76297080e..e2f23bf87 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToView.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/activity/compose/ReplyToView.kt
@@ -14,7 +14,7 @@ import com.fsck.k9.view.RecipientSelectView.Recipient
 private const val VIEW_INDEX_REPLY_TO_EXPANDER_VISIBLE = 0
 private const val VIEW_INDEX_REPLY_TO_EXPANDER_HIDDEN = 1
 
-class ReplyToView(activity: MessageCompose) : View.OnClickListener {
+class ReplyToView(activity: MessageCompose) {
     private val replyToView: RecipientSelectView = activity.findViewById(R.id.reply_to)
     private val replyToWrapper: View = activity.findViewById(R.id.reply_to_wrapper)
     private val replyToDivider: View = activity.findViewById(R.id.reply_to_divider)
@@ -24,29 +24,33 @@ class ReplyToView(activity: MessageCompose) : View.OnClickListener {
     private val textWatchers = mutableSetOf<TextWatcher>()
 
     init {
-        replyToExpander.setOnClickListener(this)
-        activity.findViewById<View>(R.id.reply_to_label).setOnClickListener(this)
+        replyToExpander.setOnClickListener {
+            isVisible = true
+            replyToView.requestFocus()
+        }
+
+        activity.findViewById<View>(R.id.reply_to_label).setOnClickListener {
+            replyToView.requestFocus()
+        }
     }
 
     var isVisible: Boolean
-        get() = replyToView.isVisible
+        get() = replyToWrapper.isVisible
         set(visible) {
             replyToDivider.isVisible = visible
             replyToView.isVisible = visible
             replyToWrapper.isVisible = visible
 
             if (visible && replyToExpanderContainer.displayedChild == VIEW_INDEX_REPLY_TO_EXPANDER_VISIBLE) {
-                replyToView.requestFocus()
                 replyToExpanderContainer.displayedChild = VIEW_INDEX_REPLY_TO_EXPANDER_HIDDEN
             } else if (replyToExpanderContainer.displayedChild == VIEW_INDEX_REPLY_TO_EXPANDER_HIDDEN) {
                 replyToExpanderContainer.displayedChild = VIEW_INDEX_REPLY_TO_EXPANDER_VISIBLE
             }
         }
 
-    override fun onClick(view: View) {
-        when (view.id) {
-            R.id.reply_to_expander -> isVisible = true
-            R.id.reply_to_label -> replyToView.requestFocus()
+    fun hideIfBlank() {
+        if (isVisible && replyToView.text.isBlank()) {
+            isVisible = false
         }
     }
 
diff --git a/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/ReplyToPresenterTest.kt b/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/ReplyToPresenterTest.kt
index ca33c808f..2f606d206 100644
--- a/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/ReplyToPresenterTest.kt
+++ b/app/ui/legacy/src/test/java/com/fsck/k9/activity/compose/ReplyToPresenterTest.kt
@@ -2,13 +2,12 @@ package com.fsck.k9.activity.compose
 
 import android.os.Bundle
 import com.fsck.k9.Identity
-import com.fsck.k9.K9RobolectricTest
+import com.fsck.k9.RobolectricTest
 import com.fsck.k9.mail.Address
 import com.google.common.truth.Truth.assertThat
 import org.junit.Test
 import org.mockito.kotlin.doReturn
 import org.mockito.kotlin.mock
-import org.mockito.kotlin.never
 import org.mockito.kotlin.stubbing
 import org.mockito.kotlin.verify
 
@@ -16,7 +15,7 @@ private const val REPLY_TO_ADDRESS = "reply-to@example.com"
 private const val REPLY_TO_ADDRESS_2 = "reply-to2@example.com"
 private const val REPLY_TO_ADDRESS_3 = "reply-to3@example.com"
 
-class ReplyToPresenterTest : K9RobolectricTest() {
+class ReplyToPresenterTest : RobolectricTest() {
     private val view = mock<ReplyToView>()
     private val replyToPresenter = ReplyToPresenter(view)
 
@@ -115,39 +114,4 @@ class ReplyToPresenterTest : K9RobolectricTest() {
         verify(view).silentlyRemoveAddresses(Address.parse(replyToOne))
         verify(view).silentlyAddAddresses(Address.parse(replyToTwo))
     }
-
-    @Test
-    fun testOnNonRecipientFieldFocused_notVisible_expectNoChange() {
-        stubbing(view) {
-            on { isVisible } doReturn false
-        }
-
-        replyToPresenter.onNonRecipientFieldFocused()
-
-        verify(view, never()).isVisible = false
-    }
-
-    @Test
-    fun testOnNonRecipientFieldFocused_noContentFieldVisible_expectHide() {
-        stubbing(view) {
-            on { isVisible } doReturn true
-            on { getAddresses() } doReturn emptyArray()
-        }
-
-        replyToPresenter.onNonRecipientFieldFocused()
-
-        verify(view).isVisible = false
-    }
-
-    @Test
-    fun testOnNonRecipientFieldFocused_withContentFieldVisible_expectNoChange() {
-        stubbing(view) {
-            on { isVisible } doReturn true
-            on { getAddresses() } doReturn Address.parse(REPLY_TO_ADDRESS)
-        }
-
-        replyToPresenter.onNonRecipientFieldFocused()
-
-        verify(view, never()).isVisible = false
-    }
 }