Clean up message flag listener in message list

This commit is contained in:
cketti 2020-03-17 01:19:51 +01:00
parent e98d3504e0
commit fa97981282
3 changed files with 16 additions and 28 deletions

View file

@ -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)
}

View file

@ -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() {

View file

@ -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)
}
}
}
}