Skip to content

Commit

Permalink
Refactored code to use plurals instead of simple strings
Browse files Browse the repository at this point in the history
  • Loading branch information
damien5314 committed Jan 27, 2016
1 parent af5ea7a commit 1982486
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 392 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ddiehl.timesincetextview;

import android.content.Context;
import android.support.annotation.PluralsRes;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

Expand All @@ -17,29 +18,6 @@

@RunWith(AndroidJUnit4.class)
public class TimeSinceTextViewAndroidTest {
private static final String TIMESPAN_NOW = "just now";
private static final String TIMESPAN_YEARS = "%s years ago";
private static final String TIMESPAN_MONTHS = "%s months ago";
private static final String TIMESPAN_WEEKS = "%s weeks ago";
private static final String TIMESPAN_DAYS = "%s days ago";
private static final String TIMESPAN_HOURS = "%s hours ago";
private static final String TIMESPAN_MINUTES = "%s minutes ago";
private static final String TIMESPAN_SECONDS = "%s seconds ago";
private static final String TIMESPAN_YEAR = "%s year ago";
private static final String TIMESPAN_MONTH = "%s month ago";
private static final String TIMESPAN_WEEK = "%s week ago";
private static final String TIMESPAN_DAY = "%s day ago";
private static final String TIMESPAN_HOUR = "%s hour ago";
private static final String TIMESPAN_MINUTE = "%s minute ago";
private static final String TIMESPAN_SECOND = "%s second ago";
private static final String TIMESPAN_YEARS_ABBR = "%syr";
private static final String TIMESPAN_MONTHS_ABBR = "%smo";
private static final String TIMESPAN_WEEKS_ABBR = "%swk";
private static final String TIMESPAN_DAYS_ABBR = "%sd";
private static final String TIMESPAN_HOURS_ABBR = "%sh";
private static final String TIMESPAN_MINUTES_ABBR = "%sm";
private static final String TIMESPAN_SECONDS_ABBR = "%ss";

private Context mContext;
private final long mTime = new Date().getTime() / 1000;

Expand All @@ -58,151 +36,151 @@ public void getFormattedDateString_now() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 10, mTime, false, mContext),
is(TIMESPAN_NOW));
is(mContext.getString(R.string.timespan_now)));
}

@Test
public void getFormattedDateString_11seconds() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 11, mTime, false, mContext),
is(TIMESPAN_SECONDS, String.valueOf(11)));
is(getQuantityString(R.plurals.timespan_seconds, 11)));
}

@Test
public void getFormattedDateString_59seconds() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 59, mTime, false, mContext),
is(TIMESPAN_SECONDS, String.valueOf(59)));
is(getQuantityString(R.plurals.timespan_seconds, 59)));
}

@Test
public void getFormattedDateString_1minute() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 60, mTime, false, mContext),
is(TIMESPAN_MINUTE, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_minutes, 1)));
}

@Test
public void getFormattedDateString_59minutes() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 60 * 60 + 1, mTime, false, mContext),
is(TIMESPAN_MINUTES, String.valueOf(59)));
is(getQuantityString(R.plurals.timespan_minutes, 59)));
}

@Test
public void getFormattedDateString_1hour() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_HOUR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_hours, 1)));
}

@Test
public void getFormattedDateString_23hours() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 24 * 60 * 60 + 1, mTime, false, mContext),
is(TIMESPAN_HOURS, String.valueOf(23)));
is(getQuantityString(R.plurals.timespan_hours, 23)));
}

@Test
public void getFormattedDateString_1day() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 24 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_DAY, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_days, 1)));
}

@Test
public void getFormattedDateString_6days() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 7 * 24 * 60 * 60 + 1, mTime, false, mContext),
is(TIMESPAN_DAYS, String.valueOf(6)));
is(getQuantityString(R.plurals.timespan_days, 6)));
}

@Test
public void getFormattedDateString_1week() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 7 * 24 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_WEEK, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_weeks, 1)));
}

@Test
public void getFormattedDateString_4weeks() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 30 * 24 * 60 * 60 + 1, mTime, false, mContext),
is(TIMESPAN_WEEKS, String.valueOf(4)));
is(getQuantityString(R.plurals.timespan_weeks, 4)));
}

@Test
public void getFormattedDateString_1month() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 30 * 24 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_MONTH, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_months, 1)));
}

@Test
public void getFormattedDateString_12months() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 365 * 24 * 60 * 60 + 1, mTime, false, mContext),
is(TIMESPAN_MONTHS, String.valueOf(12)));
is(getQuantityString(R.plurals.timespan_months, 12)));
}

@Test
public void getFormattedDateString_1year() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 365 * 24 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_YEAR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_years, 1)));
}

@Test
public void getFormattedDateString_30years() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 30 * 365 * 24 * 60 * 60, mTime, false, mContext),
is(TIMESPAN_YEARS, String.valueOf(30)));
is(getQuantityString(R.plurals.timespan_years, 30)));
}

@Test
public void getFormattedDateString_abbrev() {
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 59, mTime, true, mContext),
is(TIMESPAN_SECONDS_ABBR, String.valueOf(59)));
is(getQuantityString(R.plurals.timespan_seconds_abbr, 59)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 60, mTime, true, mContext),
is(TIMESPAN_MINUTES_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_minutes_abbr, 1)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 60 * 60, mTime, true, mContext),
is(TIMESPAN_HOURS_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_hours_abbr, 1)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 24 * 60 * 60, mTime, true, mContext),
is(TIMESPAN_DAYS_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_days_abbr, 1)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 7 * 24 * 60 * 60, mTime, true, mContext),
is(TIMESPAN_WEEKS_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_weeks_abbr, 1)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 30 * 24 * 60 * 60, mTime, true, mContext),
is(TIMESPAN_MONTHS_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_months_abbr, 1)));
assertThat(
TimeSinceTextView.getFormattedDateString(
mTime - 1 * 365 * 24 * 60 * 60, mTime, true, mContext),
is(TIMESPAN_YEARS_ABBR, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_years_abbr, 1)));
}

@Test
Expand All @@ -211,7 +189,7 @@ public void setDate_long() {
view.setDate(mTime - 1 * 24 * 60 * 60); // 1 day
assertThat(
view.getText().toString(),
is(TIMESPAN_DAY, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_days, 1)));
}

@Test
Expand All @@ -220,11 +198,15 @@ public void setDate_java_util_date() {
view.setDate(new Date((mTime - 1 * 24 * 60 * 60) * 1000)); // 1 day
assertThat(
view.getText().toString(),
is(TIMESPAN_DAY, String.valueOf(1)));
is(getQuantityString(R.plurals.timespan_days, 1)));
}

private static Matcher<String> is(String matcher, String... args) {
private static Matcher<String> is(String matcher, Object... args) {
return Is.is(
String.format(matcher, args));
}

private String getQuantityString(@PluralsRes int resId, int n) {
return mContext.getResources().getQuantityString(resId, n, n);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,23 @@

public class TimeSinceTextView extends TextView {
private static final int[] TIMESPAN_IDS = {
R.string.timespan_years,
R.string.timespan_months,
R.string.timespan_weeks,
R.string.timespan_days,
R.string.timespan_hours,
R.string.timespan_minutes,
R.string.timespan_seconds,
R.string.timespan_now
};

private static final int[] TIMESPAN_IDS_PLURAL = {
R.string.timespan_years_plural,
R.string.timespan_months_plural,
R.string.timespan_weeks_plural,
R.string.timespan_days_plural,
R.string.timespan_hours_plural,
R.string.timespan_minutes_plural,
R.string.timespan_seconds_plural,
R.string.timespan_now
R.plurals.timespan_years,
R.plurals.timespan_months,
R.plurals.timespan_weeks,
R.plurals.timespan_days,
R.plurals.timespan_hours,
R.plurals.timespan_minutes,
R.plurals.timespan_seconds
};

private static final int[] TIMESPAN_IDS_ABBR = {
R.string.timespan_years_abbr,
R.string.timespan_months_abbr,
R.string.timespan_weeks_abbr,
R.string.timespan_days_abbr,
R.string.timespan_hours_abbr,
R.string.timespan_minutes_abbr,
R.string.timespan_seconds_abbr,
R.string.timespan_now
R.plurals.timespan_years_abbr,
R.plurals.timespan_months_abbr,
R.plurals.timespan_weeks_abbr,
R.plurals.timespan_days_abbr,
R.plurals.timespan_hours_abbr,
R.plurals.timespan_minutes_abbr,
R.plurals.timespan_seconds_abbr
};

private static int NOW_THRESHOLD_SECONDS = 10;
Expand Down Expand Up @@ -68,36 +55,32 @@ public static String getFormattedDateString(long utc, boolean abbreviated, Conte
return getFormattedDateString(utc, currentTime, abbreviated, context);
}

protected static String getFormattedDateString(
public static String getFormattedDateString(
long start, long end, boolean abbreviated, Context context) {
long seconds = end - start;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
long weeks = days / 7;
long months = days / 30;
long years = days / 365;

long[] units = new long[] { years, months, weeks, days, hours, minutes, seconds };
int seconds = (int) (end - start);
int[] units = new int[] {
seconds / 31536000, // years
seconds / 2592000, // months
seconds / 604800, // weeks
seconds / 86400, // days
seconds / 3600, // hours
seconds / 60, // minutes
seconds };

String output = "";
long unit = 0;
int unit = 0;
for (int i = 0; i < units.length; i++) {
unit = units[i];
if (unit > 0) {
if (abbreviated) {
output = context.getString(TIMESPAN_IDS_ABBR[i]);
} else if (unit == 1) {
output = context.getString(TIMESPAN_IDS[i]);
} else {
output = context.getString(TIMESPAN_IDS_PLURAL[i]);
}
output = context.getResources().getQuantityString(
abbreviated ? TIMESPAN_IDS_ABBR[i] : TIMESPAN_IDS[i],
unit, unit);
break;
}
}

if (unit == seconds && seconds <= NOW_THRESHOLD_SECONDS) {
output = context.getString(TIMESPAN_IDS[TIMESPAN_IDS.length - 1]);
output = context.getString(R.string.timespan_now);
}

return String.format(output, unit);
Expand Down
Loading

0 comments on commit 1982486

Please sign in to comment.