Repository: thundernest/k-9
Issue: Crash when message list footer is removed

### 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.306-SNAPSHOT (03835f5407c8eb722ba33f1a59d58520f94629ee)

### Where did you get the app from?

_No response_

### Android version

Android 12

### Device model

_No response_

### Steps to reproduce

1. Set up demo account (`something@k9mail.example`)
2. Go to "Turing Awards" folder
3. Swipe down to refresh
4. List is animated with added messages until the footer is removed → Crash

### Expected behavior

No crash

### Actual behavior

Crash

### Logs

```text
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionFooterViewHolder{cfc2786 position=15 id=1, oldPos=14, pLpos:14 scrap [attachedScrap] update tmpDetached not recyclable(1) no parent} androidx.recyclerview.widget.RecyclerView{dabd51 VFED.V... ......ID 0,0-1080,1668 #7f0a0271 app:id/message_list}, adapter:com.fsck.k9.fragment.MessageListAdapter@7779b6, layout:androidx.recyclerview.widget.LinearLayoutManager@2b876b7, context:com.fsck.k9.activity.MessageList@a859750
  at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:6156)
  at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6339)
  at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
  at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
  at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
  at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
  at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
  at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
  at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4255)
  at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4010)
  at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4578)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at androidx.swiperefreshlayout.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:689)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1103)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1263)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
  at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
  at com.android.internal.policy.DecorView.onLayout(DecorView.java:798)
  at android.view.View.layout(View.java:23143)
  at android.view.ViewGroup.layout(ViewGroup.java:6412)
  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3684)
  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3143)
  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2126)
  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8653)
  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1037)
  at android.view.Choreographer.doCallbacks(Choreographer.java:845)
  at android.view.Choreographer.doFrame(Choreographer.java:780)
  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022)
  at android.os.Handler.handleCallback(Handler.java:938)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loopOnce(Looper.java:201)
  at android.os.Looper.loop(Looper.java:288)
  at android.app.ActivityThread.main(ActivityThread.java:7839)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
```

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

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt b/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
index 0704a1fd0..9f150ffbd 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/fragment/MessageListAdapter.kt
@@ -139,10 +139,15 @@ class MessageListAdapter internal constructor(
             if (field == value) return
 
             val hadFooterText = field != null
+            val previousFooterPosition = footerPosition
             field = value
 
             if (hadFooterText) {
-                notifyItemChanged(footerPosition)
+                if (value == null) {
+                    notifyItemRemoved(previousFooterPosition)
+                } else {
+                    notifyItemChanged(footerPosition)
+                }
             } else {
                 notifyItemInserted(footerPosition)
             }