Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f6226a7d authored by Nihar Thakkar's avatar Nihar Thakkar
Browse files

Cleaned up code and added separate text view for message preview, subject and sender.

parent 3695bfac
Loading
Loading
Loading
Loading
+50 −121
Original line number Diff line number Diff line
@@ -6,11 +6,7 @@ import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.format.DateUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
@@ -19,8 +15,9 @@ import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Locale;

import io.eelo.mail.Account;
import io.eelo.mail.FontSizes;
import io.eelo.mail.K9;
import io.eelo.mail.R;
import io.eelo.mail.RoundedQuickContactBadge;
@@ -49,63 +46,36 @@ public class MessageListAdapter extends CursorAdapter
{

    private final MessageListFragment fragment;
    private Drawable mAttachmentIcon;
    private Drawable mForwardedIcon;
    private Drawable mAnsweredIcon;
    private Drawable mForwardedAnsweredIcon;
    private FontSizes fontSizes = K9.getFontSizes();

    MessageListAdapter(MessageListFragment fragment)
    {
        super(fragment.getActivity(), null, 0);
        this.fragment = fragment;
        mAttachmentIcon = fragment.getResources().getDrawable(R.drawable.ic_email_attachment);
        mAnsweredIcon = fragment.getResources().getDrawable(R.drawable.ic_reply_light);
        mForwardedIcon = fragment.getResources().getDrawable(R.drawable.ic_forward_light);
        mForwardedAnsweredIcon = fragment.getResources().getDrawable(R.drawable.ic_reply_light);
    }

    private String recipientSigil(boolean toMe, boolean ccMe)
    {
        if (toMe)
        {
            // Remove sigil indication
            //return fragment.getString(R.string.messagelist_sent_to_me_sigil);
            return "";
        }
        else if (ccMe)
        {
            // Remove sigil indication
            //return fragment.getString(R.string.messagelist_sent_cc_me_sigil);
            return "";
        }
        else
        {
            return "";
        }
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        // Inflate the message lst item
        View view = fragment.getK9LayoutInflater().inflate(R.layout.message_list_item, parent, false);

        MessageViewHolder holder = new MessageViewHolder(fragment);
        holder.date = (TextView) view.findViewById(R.id.date);

        holder.date = (TextView) view.findViewById(R.id.date);

        if (fragment.previewLines == 0 && fragment.contactsPictureLoader == null)
        {
            view.findViewById(R.id.preview).setVisibility(View.GONE);
            holder.preview = (TextView) view.findViewById(R.id.sender_compact);


            view.findViewById(R.id.preview_text_view).setVisibility(View.GONE);
        }
        else
        {
            view.findViewById(R.id.sender_compact).setVisibility(View.GONE);
            holder.preview = (TextView) view.findViewById(R.id.preview);

            holder.preview = (TextView) view.findViewById(R.id.preview_text_view);
        }

        RoundedQuickContactBadge contactBadge =
@@ -129,23 +99,12 @@ public class MessageListAdapter extends CursorAdapter
            contactBadge.setVisibility(View.GONE);
        }

        if (fragment.senderAboveSubject)
        {
            holder.from = (TextView) view.findViewById(R.id.subject);
            fontSizes.setViewTextSize(holder.from, fontSizes.getMessageListSender());
        holder.from = (TextView) view.findViewById(R.id.from_text_view);

        }
        else
        {
            holder.subject = (TextView) view.findViewById(R.id.subject);
            fontSizes.setViewTextSize(holder.subject, fontSizes.getMessageListSubject());

        }
        holder.subject = (TextView) view.findViewById(R.id.subject_text_view);

        fontSizes.setViewTextSize(holder.date, fontSizes.getMessageListDate());
        fontSizes.setViewTextSize(holder.preview, fontSizes.getMessageListPreview());
        holder.threadCount = (TextView) view.findViewById(R.id.thread_count);
        fontSizes.setViewTextSize(holder.threadCount, fontSizes.getMessageListSubject()); // thread count is next to subject

        view.findViewById(R.id.selected_checkbox_wrapper).setVisibility((fragment.checkboxes) ? View.VISIBLE : View.GONE);

        holder.selected = (CheckBox) view.findViewById(R.id.selected_checkbox);
@@ -169,8 +128,6 @@ public class MessageListAdapter extends CursorAdapter
        Address[] ccAddrs = Address.unpack(ccList);

        boolean fromMe = fragment.messageHelper.toMe(account, fromAddrs);
        boolean toMe = fragment.messageHelper.toMe(account, toAddrs);
        boolean ccMe = fragment.messageHelper.toMe(account, ccAddrs);

        CharSequence displayName = fragment.messageHelper.getDisplayName(account, fromAddrs, toAddrs);
        CharSequence displayDate = DateUtils.getRelativeTimeSpanString(context, cursor.getLong(DATE_COLUMN));
@@ -198,10 +155,13 @@ public class MessageListAdapter extends CursorAdapter
        long uniqueId = cursor.getLong(fragment.uniqueIdColumn);
        boolean selected = fragment.selected.contains(uniqueId);

        // If checkboxes are enabled and this email is checked, check the checkbox. #dopetonguetwister
        if (fragment.checkboxes)
        {
            holder.selected.setChecked(selected);
        }

        // If stars are enabled and this email is starred/flagged, show a 'Star' icon
        if (fragment.stars)
        {
            if (flagged)
@@ -217,69 +177,69 @@ public class MessageListAdapter extends CursorAdapter
        {
            holder.starImageView.setVisibility(View.GONE);
        }

        // Get the position of this list item
        holder.position = cursor.getPosition();

        // Set the image of the sender's contact badge
        if (holder.contactBadge != null)
        {
            updateContactBadge(holder, counterpartyAddress);
        }

        // Set the background color of this list item
        setBackgroundColor(view, selected, read);

        // I don't really understand this either. PS: I didn't write this code
        if (fragment.activeMessage != null)
        {
            changeBackgroundColorIfActiveMessage(cursor, account, view);
        }
        updateWithThreadCount(holder, threadCount);
        CharSequence beforePreviewText = (fragment.senderAboveSubject) ? subject : displayName;
        String sigil = recipientSigil(toMe, ccMe);
        SpannableStringBuilder messageStringBuilder = new SpannableStringBuilder(sigil)
                .append(beforePreviewText);

        if (fragment.previewLines > 0)
        {
            String preview = getPreview(cursor);
        // Set the text of the thread count text view
        updateWithThreadCount(holder, threadCount);

            if (preview != null && !preview.equals(""))
            {
                holder.preview.setLines(fragment.previewLines + 1);
                messageStringBuilder.append("\n").append(preview);
            }
            else
        // Set the text of the from text view
        if (holder.from != null)
        {
                holder.preview.setLines(1);
            }
            holder.from.setTypeface(Typeface.create(holder.from.getTypeface(), Typeface.BOLD));
            holder.from.setText(displayName);
            holder.from.setVisibility(View.VISIBLE);
        }
        else

        // Set the text of the subject text view
        if (holder.subject != null)
        {
            holder.preview.setLines(1);
            holder.subject.setTypeface(Typeface.create(holder.subject.getTypeface(), Typeface.NORMAL));
            holder.subject.setText(subject);
            holder.subject.setVisibility(View.VISIBLE);
        }

        holder.preview.setText(messageStringBuilder, TextView.BufferType.SPANNABLE);

        formatPreviewText(holder.preview, beforePreviewText, sigil);

        Drawable statusHolder = buildStatusHolder(forwarded, answered);

        if (holder.from != null)
        // Set the text of the preview text view
        if (fragment.previewLines > 0)
        {
            holder.from.setTypeface(Typeface.create(holder.from.getTypeface(), Typeface.BOLD));
            if (fragment.senderAboveSubject)
            String preview = getPreview(cursor);

            if (preview != null && !preview.equals(""))
            {
                holder.from.setText(displayName);
                holder.preview.setText(preview);
                holder.preview.setVisibility(View.VISIBLE);
            }
            else
            {
                holder.from.setText(new SpannableStringBuilder(sigil).append(displayName));
                holder.preview.setVisibility(View.GONE);
            }
        }
        if (holder.subject != null)
        {
            holder.subject.setTypeface(Typeface.create(holder.subject.getTypeface(), Typeface.NORMAL));
            holder.subject.setText(subject);
        }

        // Set the text of the date text view
        holder.date.setText(displayDate);

        if (statusHolder != null)
        // If the email has been replied to or has been forwarded, show an icon to indicate that
        Drawable replyStatusHolder = buildStatusHolder(forwarded, answered);

        if (replyStatusHolder != null)
        {
            holder.replyStatusImageView.setImageDrawable(statusHolder);
            holder.replyStatusImageView.setImageDrawable(replyStatusHolder);
            holder.replyStatusImageView.setVisibility(View.VISIBLE);
        }
        else
@@ -287,6 +247,7 @@ public class MessageListAdapter extends CursorAdapter
            holder.replyStatusImageView.setVisibility(View.GONE);
        }

        // If the email has any attachments, show an 'Attachment' icon
        if (hasAttachments)
        {
            holder.attachmentImageView.setVisibility(View.VISIBLE);
@@ -298,30 +259,6 @@ public class MessageListAdapter extends CursorAdapter
        }
    }

    private void formatPreviewText(TextView preview, CharSequence beforePreviewText, String sigil)
    {
        Spannable previewText = (Spannable) preview.getText();
        previewText.setSpan(buildSenderSpan(), 0, beforePreviewText.length() + sigil.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        int previewSpanColor = buildPreviewSpanColor();

        // Set span (color) for preview message
        previewText.setSpan(new ForegroundColorSpan(previewSpanColor), beforePreviewText.length() + sigil.length(),
                previewText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    /**
     * Create a span section for the sender, and assign the correct font size and weight
     */
    private AbsoluteSizeSpan buildSenderSpan()
    {
        int fontSize = (fragment.senderAboveSubject) ?
                fontSizes.getMessageListSubject() :
                fontSizes.getMessageListSender();
        return new AbsoluteSizeSpan(fontSize, true);
    }

    private Address fetchCounterPartyAddress(boolean fromMe, Address[] toAddrs, Address[] ccAddrs, Address[] fromAddrs)
    {
        if (fromMe)
@@ -379,14 +316,6 @@ public class MessageListAdapter extends CursorAdapter
        }
    }

    private int buildPreviewSpanColor()
    {
        //TODO: make this part of the theme
        return (K9.getK9Theme() == K9.Theme.LIGHT) ?
                Color.rgb(105, 105, 105) :
                Color.rgb(160, 160, 160);
    }

    private Drawable buildStatusHolder(boolean forwarded, boolean answered)
    {
        if (forwarded && answered)
@@ -436,7 +365,7 @@ public class MessageListAdapter extends CursorAdapter
    {
        if (threadCount > 1)
        {
            holder.threadCount.setText(String.format("%d", threadCount));
            holder.threadCount.setText(String.format(Locale.getDefault(), "%d", threadCount));
            holder.threadCount.setVisibility(View.VISIBLE);
        }
        else
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ public class MessageViewHolder implements View.OnClickListener
    public int position = -1;
    public RoundedQuickContactBadge contactBadge;

    public MessageViewHolder(MessageListFragment fragment)
    MessageViewHolder(MessageListFragment fragment)
    {
        this.fragment = fragment;
    }
+28 −39
Original line number Diff line number Diff line
@@ -39,21 +39,19 @@
        android:background="@android:color/transparent"
        android:src="@drawable/ic_contact_picture" />

    <RelativeLayout
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="@dimen/margin_medium"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical">
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/subject_wrapper"
            android:id="@+id/sender_name_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true">
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/star_image_view"
@@ -82,43 +80,24 @@
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorSecondary" />

            <LinearLayout
            <TextView
                android:id="@+id/from_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@id/star_image_view"
                android:layout_toStartOf="@id/date"
                android:gravity="center_vertical">

                <TextView
                    android:id="@+id/sender_compact"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                android:ellipsize="end"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="?android:attr/textColorPrimary" />

                <TextView
                    android:id="@+id/subject"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:ellipsize="marquee"
                    android:singleLine="true"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="?android:attr/textColorPrimary" />

            </LinearLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/preview_wrapper"
            android:id="@+id/subject_and_preview_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/subject_wrapper"
            android:orientation="horizontal">

            <ImageView
@@ -130,16 +109,26 @@
                android:layout_marginEnd="@dimen/margin_small" />

            <TextView
                android:id="@+id/preview"
                android:id="@+id/subject_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@id/reply_status_image_view"
                android:layout_toStartOf="@id/metadata_linear_layout"
                android:bufferType="spannable"
                android:gravity="top"
                android:longClickable="false"
                android:singleLine="false"
                android:ellipsize="end"
                android:maxLines="1"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorPrimary" />

            <TextView
                android:id="@+id/preview_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/subject_text_view"
                android:layout_toEndOf="@id/reply_status_image_view"
                android:layout_toStartOf="@id/metadata_linear_layout"
                android:ellipsize="end"
                android:maxLines="1"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorSecondary" />

@@ -172,6 +161,6 @@

        </RelativeLayout>

    </RelativeLayout>
    </LinearLayout>

</LinearLayout>