more succinct relative dates

// FREEBIE
This commit is contained in:
Jake McGinty 2015-02-05 15:16:57 -08:00
parent 8031c788d9
commit 0fd52ad1fe
5 changed files with 62 additions and 19 deletions

13
res/values/plurals.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals name="minutes_ago">
<item quantity="one">@string/plurals__one_minute</item>
<item quantity="other">@string/plurals__many_minutes</item>
</plurals>
<plurals name="hours_ago">
<item quantity="one">@string/plurals__one_hour</item>
<item quantity="other">@string/plurals__many_hours</item>
</plurals>
</resources>

View File

@ -142,6 +142,9 @@
<!-- ConversationListItem -->
<string name="ConversationListItem_key_exchange_message">Key exchange message...</string>
<!-- DateUtils -->
<string name="DateUtils_now">Now</string>
<!-- ShareActivity -->
<string name="ShareActivity_share_with">Share with</string>
@ -652,6 +655,13 @@
<string name="arrays__import_export">Import / export</string>
<string name="arrays__my_identity_key">My identity key</string>
<!-- plurals.xml -->
<string name="plurals__one_minute">%d min</string>
<string name="plurals__many_minutes">%d mins</string>
<string name="plurals__one_hour">%d hour</string>
<string name="plurals__many_hours">%d hours</string>
<!-- preferences.xml -->
<string name="preferences__general">General</string>
<string name="preferences__sms_mms">SMS and MMS</string>

View File

@ -309,7 +309,7 @@ public class ConversationItem extends LinearLayout {
if (messageRecord.isPush()) timestamp = messageRecord.getDateSent();
else timestamp = messageRecord.getDateReceived();
dateText.setText(DateUtils.getBetterRelativeTimeSpanString(getContext(), timestamp));
dateText.setText(DateUtils.getExtendedRelativeTimeSpanString(getContext(), timestamp));
}
}

View File

@ -112,10 +112,7 @@ public class ConversationListItem extends RelativeLayout
this.subjectView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
if (thread.getDate() > 0) {
CharSequence date = DateUtils.getRelativeTimeSpanString(thread.getDate(),
System.currentTimeMillis(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE);
CharSequence date = DateUtils.getBriefRelativeTimeSpanString(context, thread.getDate());
dateView.setText(read ? date : color(getResources().getColor(R.color.textsecure_primary), date));
dateView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE);
}

View File

@ -18,34 +18,57 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import org.thoughtcrime.securesms.R;
import java.util.concurrent.TimeUnit;
/**
* Utility methods to help display dates in a nice, easily readable way.
*/
public class DateUtils extends android.text.format.DateUtils {
private final static long DAY_IN_MILLIS = 86400000L;
private final static long WEEK_IN_MILLIS = 7 * DAY_IN_MILLIS;
private final static long YEAR_IN_MILLIS = (long)(52.1775 * WEEK_IN_MILLIS);
private static boolean isWithinWeek(final long millis) {
return System.currentTimeMillis() - millis <= (WEEK_IN_MILLIS - DAY_IN_MILLIS);
private static boolean isWithin(final long millis, final long span, final TimeUnit unit) {
return System.currentTimeMillis() - millis <= unit.toMillis(span);
}
private static boolean isWithinYear(final long millis) {
return System.currentTimeMillis() - millis <= YEAR_IN_MILLIS;
private static int convertDelta(final long millis, TimeUnit to) {
return (int) to.convert(System.currentTimeMillis() - millis, TimeUnit.MILLISECONDS);
}
public static String getBetterRelativeTimeSpanString(final Context c, final long millis) {
public static String getBriefRelativeTimeSpanString(final Context c, final long timestamp) {
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
return c.getString(R.string.DateUtils_now);
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
int mins = convertDelta(timestamp, TimeUnit.MINUTES);
return c.getResources().getQuantityString(R.plurals.minutes_ago, mins, mins);
} else if (isWithin(timestamp, 1, TimeUnit.DAYS)) {
int hours = convertDelta(timestamp, TimeUnit.HOURS);
return c.getResources().getQuantityString(R.plurals.hours_ago, hours, hours);
} else if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY);
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL);
} else {
return formatDateTime(c, timestamp, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
}
}
public static String getExtendedRelativeTimeSpanString(final Context c, final long timestamp) {
if (isWithin(timestamp, 1, TimeUnit.MINUTES)) {
return c.getString(R.string.DateUtils_now);
} else if (isWithin(timestamp, 1, TimeUnit.HOURS)) {
int mins = (int)TimeUnit.MINUTES.convert(System.currentTimeMillis() - timestamp, TimeUnit.MILLISECONDS);
return c.getResources().getQuantityString(R.plurals.minutes_ago, mins, mins);
} else {
int formatFlags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_TIME;
if (!isToday(millis)) {
if (isWithinWeek(millis)) {
if (isWithin(timestamp, 6, TimeUnit.DAYS)) {
formatFlags |= DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_WEEKDAY;
} else if (isWithinYear(millis)) {
} else if (isWithin(timestamp, 365, TimeUnit.DAYS)) {
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL;
} else {
formatFlags |= DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL;
}
}
return DateUtils.formatDateTime(c, millis, formatFlags);
return DateUtils.formatDateTime(c, timestamp, formatFlags);
}
}
}