From fa97981282a9804d9d801e40745cddab7a82c565 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 17 Mar 2020 01:19:51 +0100 Subject: [PATCH] Clean up message flag listener in message list --- .../com/fsck/k9/fragment/MessageListAdapter.kt | 14 +++++++++++--- .../com/fsck/k9/fragment/MessageListFragment.java | 15 ++++----------- .../com/fsck/k9/fragment/MessageViewHolder.kt | 15 +-------------- 3 files changed, 16 insertions(+), 28 deletions(-) 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 af7cffa69..7b100e265 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 @@ -14,6 +14,7 @@ import android.text.style.ForegroundColorSpan import android.text.style.StyleSpan import android.view.LayoutInflater import android.view.View +import android.view.View.OnClickListener import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.TextView @@ -67,6 +68,12 @@ class MessageListAdapter internal constructor( appearance.fontSizes.messageListSubject } + private val flagClickListener = OnClickListener { view: View -> + val messageViewHolder = view.tag as MessageViewHolder + val messageListItem = getItem(messageViewHolder.position) + listItemListener.toggleMessageFlag(messageListItem) + } + private fun recipientSigil(toMe: Boolean, ccMe: Boolean): String { return if (toMe) { res.getString(R.string.messagelist_sent_to_me_sigil) @@ -96,7 +103,7 @@ class MessageListAdapter internal constructor( private fun newView(parent: ViewGroup?): View { val view = layoutInflater.inflate(R.layout.message_list_item, parent, false) - val holder = MessageViewHolder(view, listItemListener) + val holder = MessageViewHolder(view) holder.contactBadge.isVisible = appearance.showContactPicture holder.chip.isVisible = appearance.showAccountChip @@ -111,7 +118,8 @@ class MessageListAdapter internal constructor( appearance.fontSizes.setViewTextSize(holder.threadCount, appearance.fontSizes.messageListSubject) // thread count is next to subject holder.flagged.isVisible = appearance.stars - holder.flagged.setOnClickListener(holder) + holder.flagged.tag = holder + holder.flagged.setOnClickListener(flagClickListener) view.tag = holder @@ -282,5 +290,5 @@ class MessageListAdapter internal constructor( } interface MessageListItemActionListener { - fun toggleMessageFlagWithAdapterPosition(position: Int) + fun toggleMessageFlag(item: MessageListItem) } diff --git a/app/ui/src/main/java/com/fsck/k9/fragment/MessageListFragment.java b/app/ui/src/main/java/com/fsck/k9/fragment/MessageListFragment.java index 760214a79..832d2c9ab 100644 --- a/app/ui/src/main/java/com/fsck/k9/fragment/MessageListFragment.java +++ b/app/ui/src/main/java/com/fsck/k9/fragment/MessageListFragment.java @@ -1184,11 +1184,9 @@ public class MessageListFragment extends Fragment implements OnItemClickListener } @Override - public void toggleMessageFlagWithAdapterPosition(int adapterPosition) { - MessageListItem messageListItem = adapter.getItem(adapterPosition); + public void toggleMessageFlag(MessageListItem messageListItem) { boolean flagged = messageListItem.isStarred(); - - setFlag(adapterPosition,Flag.FLAGGED, !flagged); + setFlag(messageListItem, Flag.FLAGGED, !flagged); } private void toggleMessageSelectWithAdapterPosition(int adapterPosition) { @@ -1271,12 +1269,7 @@ public class MessageListFragment extends Fragment implements OnItemClickListener actionModeCallback.showFlag(isBatchFlag); } - private void setFlag(int adapterPosition, final Flag flag, final boolean newState) { - if (adapterPosition == AdapterView.INVALID_POSITION) { - return; - } - - MessageListItem messageListItem = adapter.getItem(adapterPosition); + private void setFlag(MessageListItem messageListItem, final Flag flag, final boolean newState) { Account account = messageListItem.getAccount(); if (showingThreadedList && messageListItem.getThreadCount() > 1) { @@ -2082,7 +2075,7 @@ public class MessageListFragment extends Fragment implements OnItemClickListener } else if (flag == Flag.FLAGGED) { flagState = messageListItem.isStarred(); } - setFlag(adapterPosition, flag, !flagState); + setFlag(messageListItem, flag, !flagState); } public void onMove() { diff --git a/app/ui/src/main/java/com/fsck/k9/fragment/MessageViewHolder.kt b/app/ui/src/main/java/com/fsck/k9/fragment/MessageViewHolder.kt index d85110f37..f49ee9bc3 100644 --- a/app/ui/src/main/java/com/fsck/k9/fragment/MessageViewHolder.kt +++ b/app/ui/src/main/java/com/fsck/k9/fragment/MessageViewHolder.kt @@ -1,6 +1,5 @@ package com.fsck.k9.fragment -import android.annotation.SuppressLint import android.view.View import android.widget.CheckBox import android.widget.ImageView @@ -8,11 +7,7 @@ import android.widget.TextView import com.fsck.k9.ui.ContactBadge import com.fsck.k9.ui.R -@SuppressLint("ResourceType") -class MessageViewHolder( - view: View, - private val itemActionListener: MessageListItemActionListener -) : View.OnClickListener { +class MessageViewHolder(view: View) { var position = -1 val contactBadge: ContactBadge = view.findViewById(R.id.contact_badge) @@ -24,12 +19,4 @@ class MessageViewHolder( val flagged: CheckBox = view.findViewById(R.id.star) val attachment: ImageView = view.findViewById(R.id.attachment) val status: ImageView = view.findViewById(R.id.status) - - override fun onClick(view: View) { - if (position != -1) { - if (view.id == R.id.star) { - itemActionListener.toggleMessageFlagWithAdapterPosition(position) - } - } - } }