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 | |
parent | dbb0cf8ead030a624954d7e07161f71038d008bc (diff) |
update datebubbles
Diffstat (limited to 'src/main/java/de/pixart')
5 files changed, 72 insertions, 43 deletions
diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index 544640ab0..d13e90743 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -10,6 +10,7 @@ import java.net.URL; import de.pixart.messenger.Config; import de.pixart.messenger.crypto.axolotl.FingerprintStatus; import de.pixart.messenger.http.AesGcmURLStreamHandler; +import de.pixart.messenger.ui.adapter.MessageAdapter; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.GeoHelper; import de.pixart.messenger.utils.MimeUtils; @@ -190,13 +191,6 @@ public class Message extends AbstractEntity { cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE))); } - public static Message createDateMessage(Conversation conversation, String body) { - final Message message = new Message(conversation); - message.setType(Message.TYPE_STATUS); - message.setBody(body); - return message; - } - public static Message createStatusMessage(Conversation conversation, String body) { final Message message = new Message(conversation); message.setType(Message.TYPE_STATUS); @@ -211,6 +205,14 @@ public class Message extends AbstractEntity { return message; } + public static Message createDateSeparator(Message message) { + final Message separator = new Message(message.getConversation()); + separator.setType(Message.TYPE_STATUS); + separator.setBody(MessageAdapter.DATE_SEPARATOR_BODY); + separator.setTime(message.getTimeSent()); + return separator; + } + @Override public ContentValues getContentValues() { ContentValues values = new ContentValues(); @@ -503,7 +505,8 @@ public class Message extends AbstractEntity { !message.bodyIsHeart() && !this.bodyIsXmpp() && !message.bodyIsXmpp() && - ((this.axolotlFingerprint == null && message.axolotlFingerprint == null) || this.axolotlFingerprint.equals(message.getFingerprint())) + ((this.axolotlFingerprint == null && message.axolotlFingerprint == null) || this.axolotlFingerprint.equals(message.getFingerprint())) && + UIHelper.sameDay(message.getTimeSent(), this.getTimeSent()) ); } diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 06db41562..1464d8ec0 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -3422,7 +3422,7 @@ public class XmppConnectionService extends Service { updateConversationUi(); } - private SharedPreferences getPreferences() { + public SharedPreferences getPreferences() { return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); } 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 { diff --git a/src/main/java/de/pixart/messenger/utils/UIHelper.java b/src/main/java/de/pixart/messenger/utils/UIHelper.java index 53727b8ee..ab749ca2d 100644 --- a/src/main/java/de/pixart/messenger/utils/UIHelper.java +++ b/src/main/java/de/pixart/messenger/utils/UIHelper.java @@ -100,6 +100,24 @@ public class UIHelper { return sameDay(date, new Date(System.currentTimeMillis())); } + public static boolean today(long date) { + return sameDay(date, System.currentTimeMillis()); + } + + public static boolean yesterday(long date) { + Calendar cal1 = Calendar.getInstance(); + Calendar cal2 = Calendar.getInstance(); + cal1.add(Calendar.DAY_OF_YEAR, -1); + cal2.setTime(new Date(date)); + return cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) + && cal1.get(Calendar.DAY_OF_YEAR) == cal2 + .get(Calendar.DAY_OF_YEAR); + } + + public static boolean sameDay(long a, long b) { + return sameDay(new Date(a), new Date(b)); + } + private static boolean sameDay(Date a, Date b) { Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); |