Chat scroll to last message + specif date display for messages received/sent today
This commit is contained in:
parent
2ef850574f
commit
506c56eee0
3 changed files with 33 additions and 4 deletions
|
@ -2,6 +2,8 @@
|
|||
<resources>
|
||||
|
||||
<string name="messages_date_format">HH:mm:ss d MMM</string>
|
||||
<string name="now_date_format">few seconds ago</string>
|
||||
<string name="today_date_format">HH:mm:ss</string>
|
||||
|
||||
<string name="app_name">Linphone 2</string>
|
||||
<string name="notification_title">Linphone</string>
|
||||
|
|
|
@ -83,6 +83,12 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
|||
List<ChatMessage> messagesList = LinphoneActivity.instance().getChatMessages(sipUri);
|
||||
|
||||
messagesScrollView = (ScrollView) view.findViewById(R.id.chatScrollView);
|
||||
messagesScrollView.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
scrollToEnd();
|
||||
}
|
||||
});
|
||||
|
||||
previousMessageID = -1;
|
||||
for (ChatMessage msg : messagesList) {
|
||||
|
@ -129,7 +135,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
|||
LinphoneActivity.instance().onMessageSent(sipUri, messageToSend);
|
||||
}
|
||||
|
||||
displayMessage(previousMessageID + 1, messageToSend, "few seconds ago", false, messagesLayout);
|
||||
displayMessage(previousMessageID + 1, messageToSend, getString(R.string.now_date_format), false, messagesLayout);
|
||||
scrollToEnd();
|
||||
}
|
||||
}
|
||||
|
@ -145,7 +151,7 @@ public class ChatFragment extends Fragment implements OnClickListener, LinphoneO
|
|||
|
||||
@Override
|
||||
public void onMessageReceived(LinphoneAddress from, String message) {
|
||||
displayMessage(previousMessageID + 1, message, "few seconds ago", true, messagesLayout);
|
||||
displayMessage(previousMessageID + 1, message, getString(R.string.now_date_format), true, messagesLayout);
|
||||
scrollToEnd();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,15 +83,36 @@ public class BubbleChat {
|
|||
|
||||
private String timestampToHumanDate(Context context, String timestamp) {
|
||||
try {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat(context.getResources().getString(R.string.messages_date_format));
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(Long.parseLong(timestamp));
|
||||
|
||||
SimpleDateFormat dateFormat;
|
||||
if (isToday(cal)) {
|
||||
dateFormat = new SimpleDateFormat(context.getResources().getString(R.string.today_date_format));
|
||||
} else {
|
||||
dateFormat = new SimpleDateFormat(context.getResources().getString(R.string.messages_date_format));
|
||||
}
|
||||
|
||||
return dateFormat.format(cal.getTime());
|
||||
} catch (NumberFormatException nfe) {
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isToday(Calendar cal) {
|
||||
return isSameDay(cal, Calendar.getInstance());
|
||||
}
|
||||
|
||||
private boolean isSameDay(Calendar cal1, Calendar cal2) {
|
||||
if (cal1 == null || cal2 == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
|
||||
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
|
||||
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
|
||||
}
|
||||
|
||||
private int pixelsToDpi(Context context, int pixels) {
|
||||
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) pixels, context.getResources().getDisplayMetrics());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue