diff options
author | Christian Schneppe <christian@pix-art.de> | 2017-08-01 11:33:39 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2017-08-01 11:34:59 +0200 |
commit | 23752c581f79640a38d87aa2a6914deac475447d (patch) | |
tree | cf55b54ad2ea305812beb919fa90ab2c9f82fd27 /src/main/java/de/pixart/messenger/ui | |
parent | dbb0cf8ead030a624954d7e07161f71038d008bc (diff) |
update datebubbles
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/ConversationFragment.java | 42 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java | 34 |
2 files changed, 42 insertions, 34 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 7deb54173..a243688d7 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -46,7 +46,6 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Locale; import java.util.UUID; @@ -159,18 +158,19 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa @Override public void run() { final int oldPosition = messagesView.getFirstVisiblePosition(); - final Message message; - if (oldPosition < messageList.size()) { - message = messageList.get(oldPosition); - } else { - message = null; + Message message = null; + int childPos; + for (childPos = 0; childPos + oldPosition < messageList.size(); ++childPos) { + message = messageList.get(oldPosition + childPos); + if (message.getType() != Message.TYPE_STATUS) { + break; + } } - String uuid = message != null ? message.getUuid() : null; - View v = messagesView.getChildAt(0); + final String uuid = message != null ? message.getUuid() : null; + View v = messagesView.getChildAt(childPos); final int pxOffset = (v == null) ? 0 : v.getTop(); ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList); try { - updateDateBubbles(); updateStatusMessages(); } catch (IllegalStateException e) { Log.d(Config.LOGTAG, "caught illegal state exception while updating status messages"); @@ -1137,7 +1137,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa if (this.conversation != null) { conversation.populateWithMessages(ConversationFragment.this.messageList); updateSnackBar(conversation); - updateDateBubbles(); updateStatusMessages(); this.messageListAdapter.notifyDataSetChanged(); updateChatMsgHint(); @@ -1333,6 +1332,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } protected void updateStatusMessages() { + updateDateBubbles(); synchronized (this.messageList) { if (showLoadMoreMessages(conversation)) { this.messageList.add(0, Message.createLoadMoreMessage(conversation)); @@ -1341,22 +1341,12 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } protected void updateDateBubbles() { - String first = null; - String today = sdf.format(System.currentTimeMillis()); - int max = this.messageList.size(); - if (max == 0 || (max <= 1 && showLoadMoreMessages(conversation))) { - this.messageList.add(0, Message.createDateMessage(conversation, getString(R.string.start_chatting))); - } else if ((max > 0 && !showLoadMoreMessages(conversation)) || (max > 1 && showLoadMoreMessages(conversation))) { - for (int i = 0; i < this.messageList.size(); i++) { - Date date = new Date(this.messageList.get(i).getTimeSent()); - String last = sdf.format(date); - if (first == null || !first.equals(last)) { - first = last; - String dateString = first; - if (today.equals(first)) { - dateString = getString(R.string.today); - } - this.messageList.add(i, Message.createDateMessage(conversation, dateString)); + synchronized (this.messageList) { + for (int i = 0; i < this.messageList.size(); ++i) { + final Message current = this.messageList.get(i); + if (i == 0 || !UIHelper.sameDay(this.messageList.get(i - 1).getTimeSent(), current.getTimeSent())) { + this.messageList.add(i, Message.createDateSeparator(current)); + i++; } } } diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index d0bacc245..8320ac8ad 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -18,6 +18,7 @@ import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.Spanned; +import android.text.format.DateUtils; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; @@ -87,6 +88,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie private static final int SENT = 0; private static final int RECEIVED = 1; private static final int STATUS = 2; + private static final int DATE_SEPARATOR = 3; + + public static final String DATE_SEPARATOR_BODY = "DATE_SEPARATOR"; + private static final Pattern XMPP_PATTERN = Pattern .compile("xmpp\\:(?:(?:[" + Patterns.GOOD_IRI_CHAR @@ -151,12 +156,16 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie @Override public int getViewTypeCount() { - return 3; + return 4; } public int getItemViewType(Message message) { if (message.getType() == Message.TYPE_STATUS) { - return STATUS; + if (DATE_SEPARATOR_BODY.equals(message.getBody())) { + return DATE_SEPARATOR; + } else { + return STATUS; + } } else if (message.getStatus() <= Message.STATUS_RECEIVED) { return RECEIVED; } @@ -778,6 +787,10 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie View view; viewHolder = new ViewHolder(); switch (type) { + case DATE_SEPARATOR: + view = activity.getLayoutInflater().inflate(R.layout.message_date_bubble, parent, false); + viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); + break; case SENT: view = activity.getLayoutInflater().inflate( R.layout.message_sent, parent, false); @@ -829,8 +842,6 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie break; case STATUS: view = activity.getLayoutInflater().inflate(R.layout.message_status, parent, false); - viewHolder.contact_picture = (ImageView) view.findViewById(R.id.message_photo); - viewHolder.status_message = (TextView) view.findViewById(R.id.status_message); viewHolder.load_more_messages = (Button) view.findViewById(R.id.load_more_messages); break; default: @@ -850,9 +861,18 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie boolean darkBackground = (type == SENT && (!isInValidSession || !mUseWhiteBackground)); - if (type == STATUS) { + if (type == DATE_SEPARATOR) { + if (UIHelper.today(message.getTimeSent())) { + viewHolder.status_message.setText(R.string.today); + } else if (UIHelper.yesterday(message.getTimeSent())) { + viewHolder.status_message.setText(R.string.yesterday); + } else { + viewHolder.status_message.setText(DateUtils.formatDateTime(activity, message.getTimeSent(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR)); + } + viewHolder.status_message.setTextColor(activity.getSecondaryTextColor()); + return view; + } else if (type == STATUS) { if ("LOAD_MORE".equals(message.getBody())) { - viewHolder.status_message.setVisibility(View.GONE); viewHolder.load_more_messages.setVisibility(View.VISIBLE); viewHolder.load_more_messages.setOnClickListener(new OnClickListener() { @Override @@ -861,9 +881,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } }); } else { - viewHolder.status_message.setVisibility(View.VISIBLE); viewHolder.load_more_messages.setVisibility(View.GONE); - viewHolder.status_message.setText(message.getBody()); } return view; } else { |