Replace Long by Timestamp

This commit is contained in:
2017-07-22 11:01:35 -04:00
parent 882ddba324
commit a8aa6f192c
80 changed files with 672 additions and 526 deletions

View File

@@ -210,9 +210,8 @@ public class BaseAndroidTest extends TestCase
Debug.stopMethodTracing();
}
protected Long day(int offset)
protected Timestamp day(int offset)
{
return DateUtils.getStartOfToday() -
offset * DateUtils.millisecondsInOneDay;
return DateUtils.getToday().minus(offset);
}
}

View File

@@ -86,7 +86,7 @@ public class BaseUserInterfaceTest
{
prefs.reset();
prefs.setFirstRun(false);
prefs.updateLastHint(100, DateUtils.getStartOfToday());
prefs.updateLastHint(100, DateUtils.getToday());
habitList.removeAll();
cache.refreshAllHabits();
Thread.sleep(1000);

View File

@@ -66,14 +66,13 @@ public class HabitFixtures
habit.setFrequency(new Frequency(3, 7));
habit.setColor(7);
long day = DateUtils.millisecondsInOneDay;
long today = DateUtils.getStartOfToday();
Timestamp today = DateUtils.getToday();
int marks[] = { 0, 1, 3, 5, 7, 8, 9, 10, 12, 14, 15, 17, 19, 20, 26, 27,
28, 50, 51, 52, 53, 54, 58, 60, 63, 65, 70, 71, 72, 73, 74, 75, 80,
81, 83, 89, 90, 91, 95, 102, 103, 108, 109, 120};
for (int mark : marks)
habit.getRepetitions().toggle(today - mark * day);
habit.getRepetitions().toggle(today.minus(mark));
return habit;
}
@@ -89,12 +88,12 @@ public class HabitFixtures
habit.setUnit("steps");
habitList.add(habit);
long timestamp = DateUtils.getStartOfToday();
Timestamp timestamp = DateUtils.getToday();
for (int value : LONG_NUMERICAL_HABIT_CHECKS)
{
Repetition r = new Repetition(timestamp, value);
habit.getRepetitions().add(r);
timestamp -= DateUtils.millisecondsInOneDay;
timestamp = timestamp.minus(1);
}
return habit;
@@ -108,11 +107,11 @@ public class HabitFixtures
habit.setFrequency(new Frequency(2, 3));
habitList.add(habit);
long timestamp = DateUtils.getStartOfToday();
Timestamp timestamp = DateUtils.getToday();
for (boolean c : LONG_HABIT_CHECKS)
{
if (c) habit.getRepetitions().toggle(timestamp);
timestamp -= DateUtils.millisecondsInOneDay;
timestamp = timestamp.minus(1);
}
return habit;

View File

@@ -44,10 +44,9 @@ public class BarChartTest extends BaseViewTest
super.setUp();
Habit habit = fixtures.createLongNumericalHabit();
view = new BarChart(targetContext);
long today = DateUtils.getStartOfToday();
long day = DateUtils.millisecondsInOneDay;
Timestamp today = DateUtils.getToday();
CheckmarkList checkmarks = habit.getCheckmarks();
view.setCheckmarks(checkmarks.getByInterval(today - 20 * day, today));
view.setCheckmarks(checkmarks.getByInterval(today.minus(20), today));
view.setColor(PaletteUtils.getColor(targetContext, habit.getColor()));
view.setTarget(200.0);
measureView(view, dpToPixels(300), dpToPixels(200));

View File

@@ -24,6 +24,7 @@ import android.support.test.runner.*
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.*
import org.isoron.uhabits.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.models.Checkmark.*
import org.isoron.uhabits.utils.*
import org.junit.*
@@ -89,7 +90,7 @@ class CheckmarkPanelViewTest : BaseViewTest() {
@Test
fun testToggle() {
var timestamps = mutableListOf<Long>()
var timestamps = mutableListOf<Timestamp>()
view.onToggle = { timestamps.add(it) }
view.buttons[0].performLongClick()
view.buttons[2].performLongClick()
@@ -99,12 +100,12 @@ class CheckmarkPanelViewTest : BaseViewTest() {
@Test
fun testToggle_withOffset() {
var timestamps = LongArray(0)
val timestamps = mutableListOf<Timestamp>()
view.dataOffset = 3
view.onToggle = { timestamps += it }
view.buttons[0].performLongClick()
view.buttons[2].performLongClick()
view.buttons[3].performLongClick()
assertThat(timestamps, equalTo(longArrayOf(day(3), day(5), day(6))))
assertThat(timestamps, equalTo(listOf(day(3), day(5), day(6))))
}
}

View File

@@ -24,6 +24,7 @@ import android.support.test.runner.*
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.*
import org.isoron.uhabits.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.utils.*
import org.junit.*
import org.junit.runner.*
@@ -84,22 +85,22 @@ class NumberPanelViewTest : BaseViewTest() {
@Test
fun testEdit() {
var timestamps = LongArray(0)
view.onEdit = { timestamps += it }
val timestamps = mutableListOf<Timestamp>()
view.onEdit = { timestamps.plusAssign(it) }
view.buttons[0].performLongClick()
view.buttons[2].performLongClick()
view.buttons[3].performLongClick()
assertThat(timestamps, equalTo(longArrayOf(day(0), day(2), day(3))))
assertThat(timestamps, equalTo(listOf(day(0), day(2), day(3))))
}
@Test
fun testEdit_withOffset() {
var timestamps = LongArray(0)
val timestamps = mutableListOf<Timestamp>()
view.dataOffset = 3
view.onEdit = { timestamps += it }
view.buttons[0].performLongClick()
view.buttons[2].performLongClick()
view.buttons[3].performLongClick()
assertThat(timestamps, equalTo(longArrayOf(day(3), day(5), day(6))))
assertThat(timestamps, equalTo(listOf(day(3), day(5), day(6))))
}
}

View File

@@ -28,7 +28,7 @@ import org.junit.*;
import org.junit.runner.*;
@RunWith(AndroidJUnit4.class)
@LargeTest
@MediumTest
public class PerformanceTest extends BaseAndroidTest
{
private Habit habit;

View File

@@ -115,14 +115,12 @@ public class BarChart extends ScrollableChart
Random random = new Random();
List<Checkmark> checkmarks = new LinkedList<>();
long timestamp = DateUtils.getStartOfToday();
long day = DateUtils.millisecondsInOneDay;
Timestamp today = DateUtils.getToday();
for (int i = 1; i < 100; i++)
{
int value = random.nextInt(1000);
checkmarks.add(new Checkmark(timestamp, value));
timestamp -= day;
checkmarks.add(new Checkmark(today.minus(i), value));
}
setCheckmarks(checkmarks);
@@ -205,7 +203,7 @@ public class BarChart extends ScrollableChart
if (offset >= checkmarks.size()) continue;
double value = checkmarks.get(offset).getValue();
long timestamp = checkmarks.get(offset).getTimestamp();
Timestamp timestamp = checkmarks.get(offset).getTimestamp();
int height = (int) (columnHeight * value / maxValue);
rect.set(0, 0, baseSize, height);
@@ -286,13 +284,13 @@ public class BarChart extends ScrollableChart
if (isTransparencyEnabled) pGraph.setXfermode(XFERMODE_SRC);
}
private void drawFooter(Canvas canvas, RectF rect, long currentDate)
private void drawFooter(Canvas canvas, RectF rect, Timestamp currentDate)
{
String yearText = dfYear.format(currentDate);
String monthText = dfMonth.format(currentDate);
String dayText = dfDay.format(currentDate);
String yearText = dfYear.format(currentDate.toJavaDate());
String monthText = dfMonth.format(currentDate.toJavaDate());
String dayText = dfDay.format(currentDate.toJavaDate());
GregorianCalendar calendar = DateUtils.getCalendar(currentDate);
GregorianCalendar calendar = currentDate.toCalendar();
pText.setColor(textColor);
String text;

View File

@@ -26,6 +26,7 @@ import android.util.*;
import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.utils.*;
@@ -67,7 +68,7 @@ public class FrequencyChart extends ScrollableChart
private boolean isBackgroundTransparent;
@NonNull
private HashMap<Long, Integer[]> frequency;
private HashMap<Timestamp, Integer[]> frequency;
private int maxFreq;
public FrequencyChart(Context context)
@@ -90,23 +91,21 @@ public class FrequencyChart extends ScrollableChart
postInvalidate();
}
public void setFrequency(HashMap<Long, Integer[]> frequency)
public void setFrequency(HashMap<Timestamp, Integer[]> frequency)
{
this.frequency = frequency;
maxFreq = getMaxFreq(frequency);
postInvalidate();
}
private int getMaxFreq(HashMap<Long, Integer[]> frequency)
private int getMaxFreq(HashMap<Timestamp, Integer[]> frequency)
{
int maxValue = 1;
for (Integer[] values : frequency.values())
{
for (Integer value : values)
{
maxValue = Math.max(value, maxValue);
}
}
return maxValue;
}
@@ -194,7 +193,7 @@ public class FrequencyChart extends ScrollableChart
private void drawColumn(Canvas canvas, RectF rect, GregorianCalendar date)
{
Integer values[] = frequency.get(date.getTimeInMillis());
Integer values[] = frequency.get(new Timestamp(date));
float rowHeight = rect.height() / 8.0f;
prevRect.set(rect);
@@ -324,7 +323,7 @@ public class FrequencyChart extends ScrollableChart
for (int j = 0; j < 7; j++)
values[j] = rand.nextInt(5);
frequency.put(date.getTimeInMillis(), values);
frequency.put(new Timestamp(date), values);
date.add(Calendar.MONTH, -1);
}
}

View File

@@ -28,14 +28,15 @@ import android.view.*;
import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.utils.*;
import java.text.*;
import java.util.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
import static org.isoron.androidbase.utils.InterfaceUtils.*;
import static org.isoron.uhabits.core.models.Checkmark.*;
public class HistoryChart extends ScrollableChart
{
@@ -135,10 +136,11 @@ public class HistoryChart extends ScrollableChart
return false;
}
final Long timestamp = positionToTimestamp(x, y);
final Timestamp timestamp = positionToTimestamp(x, y);
if (timestamp == null) return false;
int offset = timestampToOffset(timestamp);
Timestamp today = DateUtils.getToday();
int offset = timestamp.daysUntil(today);
if (offset < checkmarks.length)
{
boolean isChecked = checkmarks[offset] == CHECKED_EXPLICITLY;
@@ -435,7 +437,8 @@ public class HistoryChart extends ScrollableChart
baseLocation = new RectF();
}
private Long positionToTimestamp(float x, float y)
@Nullable
private Timestamp positionToTimestamp(float x, float y)
{
int col = (int) (x / columnWidth);
int row = (int) (y / columnWidth);
@@ -450,15 +453,7 @@ public class HistoryChart extends ScrollableChart
if (DateUtils.getStartOfDay(date.getTimeInMillis()) >
DateUtils.getStartOfToday()) return null;
return date.getTimeInMillis();
}
private int timestampToOffset(Long timestamp)
{
Long day = DateUtils.millisecondsInOneDay;
Long today = DateUtils.getStartOfToday();
return (int) ((today - timestamp) / day);
return new Timestamp(date.getTimeInMillis());
}
private void updateDate()
@@ -478,6 +473,6 @@ public class HistoryChart extends ScrollableChart
public interface Controller
{
default void onToggleCheckmark(long timestamp) {}
default void onToggleCheckmark(Timestamp timestamp) {}
}
}

View File

@@ -111,17 +111,15 @@ public class ScoreChart extends ScrollableChart
scores = new LinkedList<>();
double previous = 0.5f;
long timestamp = DateUtils.getStartOfToday();
long day = DateUtils.millisecondsInOneDay;
Timestamp timestamp = DateUtils.getToday();
for (int i = 1; i < 100; i++)
{
double step = 0.1f;
double current = previous + random.nextDouble() * step * 2 - step;
current = Math.max(0, Math.min(1.0f, current));
scores.add(new Score(timestamp, current));
scores.add(new Score(timestamp.minus(i), current));
previous = current;
timestamp -= day;
}
}
@@ -189,7 +187,7 @@ public class ScoreChart extends ScrollableChart
if (offset >= scores.size()) continue;
double score = scores.get(offset).getValue();
long timestamp = scores.get(offset).getTimestamp();
Timestamp timestamp = scores.get(offset).getTimestamp();
int height = (int) (columnHeight * score);
@@ -258,13 +256,13 @@ public class ScoreChart extends ScrollableChart
if (isTransparencyEnabled) initCache(width, height);
}
private void drawFooter(Canvas canvas, RectF rect, long currentDate)
private void drawFooter(Canvas canvas, RectF rect, Timestamp currentDate)
{
String yearText = dfYear.format(currentDate);
String monthText = dfMonth.format(currentDate);
String dayText = dfDay.format(currentDate);
String yearText = dfYear.format(currentDate.toJavaDate());
String monthText = dfMonth.format(currentDate.toJavaDate());
String dayText = dfDay.format(currentDate.toJavaDate());
GregorianCalendar calendar = DateUtils.getCalendar(currentDate);
GregorianCalendar calendar = currentDate.toCalendar();
String text;
int year = calendar.get(Calendar.YEAR);

View File

@@ -97,16 +97,15 @@ public class StreakChart extends View
public void populateWithRandomData()
{
long day = DateUtils.millisecondsInOneDay;
long start = DateUtils.getStartOfToday();
Timestamp start = DateUtils.getToday();
LinkedList<Streak> streaks = new LinkedList<>();
for (int i = 0; i < 10; i++)
{
int length = new Random().nextInt(100);
long end = start + length * day;
Timestamp end = start.plus(length);
streaks.add(new Streak(start, end));
start = end + day;
start = end.plus(1);
}
setStreaks(streaks);
@@ -215,8 +214,8 @@ public class StreakChart extends View
if (shouldShowLabels)
{
String startLabel = dateFormat.format(new Date(streak.getStart()));
String endLabel = dateFormat.format(new Date(streak.getEnd()));
String startLabel = dateFormat.format(streak.getStart().toJavaDate());
String endLabel = dateFormat.format(streak.getEnd().toJavaDate());
paint.setColor(textColor);
paint.setTextAlign(Paint.Align.RIGHT);
@@ -284,9 +283,9 @@ public class StreakChart extends View
minLength = Math.min(minLength, s.getLength());
float lw1 =
paint.measureText(dateFormat.format(new Date(s.getStart())));
paint.measureText(dateFormat.format(s.getStart().toJavaDate()));
float lw2 =
paint.measureText(dateFormat.format(new Date(s.getEnd())));
paint.measureText(dateFormat.format(s.getEnd().toJavaDate()));
maxLabelWidth = Math.max(maxLabelWidth, Math.max(lw1, lw2));
}

View File

@@ -22,6 +22,7 @@ package org.isoron.uhabits.activities.habits.list.views
import android.content.*
import com.google.auto.factory.*
import org.isoron.androidbase.activities.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.models.Checkmark.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.utils.*
@@ -45,7 +46,7 @@ class CheckmarkPanelView(
setupButtons()
}
var onToggle: (Long) -> Unit = {}
var onToggle: (Timestamp) -> Unit = {}
set(value) {
field = value
setupButtons()
@@ -55,11 +56,10 @@ class CheckmarkPanelView(
@Synchronized
override fun setupButtons() {
val today = DateUtils.getStartOfToday()
val day = DateUtils.millisecondsInOneDay
val today = DateUtils.getToday()
buttons.forEachIndexed { index, button ->
val timestamp = today - (index + dataOffset) * day
val timestamp = today.minus(index + dataOffset)
button.value = when {
index + dataOffset < values.size -> values[index + dataOffset]
else -> UNCHECKED

View File

@@ -171,10 +171,9 @@ class HabitCardView(
updateBackground(isSelected)
}
fun triggerRipple(timestamp: Long) {
val today = DateUtils.getStartOfToday()
val day = DateUtils.millisecondsInOneDay
val offset = ((today - timestamp) / day).toInt() - dataOffset
fun triggerRipple(timestamp: Timestamp) {
val today = DateUtils.getToday()
val offset = timestamp.daysUntil(today) - dataOffset
val button = checkmarkPanel.buttons[offset]
val y = button.height / 2.0f
val x = checkmarkPanel.x + button.x + (button.width / 2).toFloat()

View File

@@ -22,6 +22,7 @@ package org.isoron.uhabits.activities.habits.list.views
import android.content.*
import com.google.auto.factory.*
import org.isoron.androidbase.activities.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.utils.*
@@ -56,7 +57,7 @@ class NumberPanelView(
setupButtons()
}
var onEdit: (Long) -> Unit = {}
var onEdit: (Timestamp) -> Unit = {}
set(value) {
field = value
setupButtons()
@@ -66,11 +67,10 @@ class NumberPanelView(
@Synchronized
override fun setupButtons() {
val day = DateUtils.millisecondsInOneDay
val today = DateUtils.getStartOfToday()
val today = DateUtils.getToday()
buttons.forEachIndexed { index, button ->
val timestamp = today - (index + dataOffset) * day
val timestamp = today.minus(index + dataOffset)
button.value = when {
index + dataOffset < values.size -> values[index + dataOffset]
else -> 0.0

View File

@@ -73,7 +73,7 @@ public class ShowHabitScreen extends BaseScreen
}
@Override
public void onToggleCheckmark(long timestamp)
public void onToggleCheckmark(Timestamp timestamp)
{
behavior.get().onToggleCheckmark(timestamp);
}

View File

@@ -98,9 +98,9 @@ public class BarCard extends HabitCard
@Override
public void doInBackground()
{
long today = DateUtils.getStartOfToday();
Timestamp today = DateUtils.getToday();
List<Checkmark> checkmarks =
habit.getCheckmarks().getByInterval(0, today);
habit.getCheckmarks().getByInterval(Timestamp.ZERO, today);
chart.setCheckmarks(checkmarks);
}

View File

@@ -94,7 +94,7 @@ public class FrequencyCard extends HabitCard
public void doInBackground()
{
RepetitionList reps = getHabit().getRepetitions();
HashMap<Long, Integer[]> frequency = reps.getWeekdayFrequency();
HashMap<Timestamp, Integer[]> frequency = reps.getWeekdayFrequency();
chart.setFrequency(frequency);
}

View File

@@ -164,9 +164,9 @@ public class OverviewCard extends HabitCard
ScoreList scores = habit.getScores();
long today = DateUtils.getStartOfToday();
long lastMonth = today - 30 * DateUtils.millisecondsInOneDay;
long lastYear = today - 365 * DateUtils.millisecondsInOneDay;
Timestamp today = DateUtils.getToday();
Timestamp lastMonth = today.minus(30);
Timestamp lastYear = today.minus(365);
cache.todayScore = (float) scores.getTodayValue();
cache.lastMonthScore = (float) scores.getValue(lastMonth);

View File

@@ -45,7 +45,7 @@ class FireSettingReceiver : BroadcastReceiver() {
.build()
allHabits = app.component.habitList
val args = parseIntent(intent) ?: return
val timestamp = DateUtils.getStartOfToday()
val timestamp = DateUtils.getToday()
val controller = component.widgetController
when (args.action) {

View File

@@ -42,16 +42,16 @@ class IntentParser
return habit
}
private fun parseTimestamp(intent: Intent): Long {
val today = DateUtils.getStartOfToday()
private fun parseTimestamp(intent: Intent): Timestamp {
val today = DateUtils.getToday().unixTime
var timestamp = intent.getLongExtra("timestamp", today)
timestamp = DateUtils.getStartOfDay(timestamp)
if (timestamp < 0 || timestamp > today)
throw IllegalArgumentException("timestamp is not valid")
return timestamp
return Timestamp(timestamp)
}
class CheckmarkIntentData(var habit: Habit, var timestamp: Long)
class CheckmarkIntentData(var habit: Habit, var timestamp: Timestamp)
}

View File

@@ -35,13 +35,13 @@ class PendingIntentFactory
@AppContext private val context: Context,
private val intentFactory: IntentFactory) {
fun addCheckmark(habit: Habit, timestamp: Long?): PendingIntent =
fun addCheckmark(habit: Habit, timestamp: Timestamp?): PendingIntent =
PendingIntent.getBroadcast(
context, 1,
Intent(context, WidgetReceiver::class.java).apply {
data = Uri.parse(habit.uriString)
action = WidgetReceiver.ACTION_ADD_REPETITION
if (timestamp != null) putExtra("timestamp", timestamp)
if (timestamp != null) putExtra("timestamp", timestamp.unixTime)
},
FLAG_UPDATE_CURRENT)

View File

@@ -48,7 +48,7 @@ class AndroidNotificationTray
override fun showNotification(habit: Habit,
notificationId: Int,
timestamp: Long,
timestamp: Timestamp,
reminderTime: Long) {
val checkAction = Action(

View File

@@ -128,7 +128,7 @@ public class PebbleReceiver extends PebbleDataReceiver
Habit habit = habitList.getById(habitId);
if (habit == null) return;
long today = DateUtils.getStartOfToday();
Timestamp today = DateUtils.getToday();
commandRunner.execute(
new ToggleRepetitionCommand(habitList, habit, today), habitId);

View File

@@ -58,7 +58,7 @@ public class ReminderController
}
public void onShowReminder(@NonNull Habit habit,
long timestamp,
Timestamp timestamp,
long reminderTime)
{
notificationTray.show(habit, timestamp, reminderTime);

View File

@@ -79,8 +79,8 @@ public class ReminderReceiver extends BroadcastReceiver
{
case ACTION_SHOW_REMINDER:
if (habit == null) return;
reminderController.onShowReminder(habit, timestamp,
reminderTime);
reminderController.onShowReminder(habit,
new Timestamp(timestamp), reminderTime);
break;
case ACTION_DISMISS_REMINDER:

View File

@@ -80,8 +80,8 @@ public class ReminderControllerTest extends BaseAndroidJVMTest
public void testOnShowReminder() throws Exception
{
Habit habit = mock(Habit.class);
controller.onShowReminder(habit, 123, 456);
verify(notificationTray).show(habit, 123, 456);
controller.onShowReminder(habit, Timestamp.ZERO.plus(100), 456);
verify(notificationTray).show(habit, Timestamp.ZERO.plus(100), 456);
verify(reminderScheduler).scheduleAll();
}

View File

@@ -40,7 +40,7 @@ public class WidgetControllerTest extends BaseAndroidJVMTest
private Habit habit;
private long today;
private Timestamp today;
private NotificationTray notificationTray;
@@ -49,7 +49,7 @@ public class WidgetControllerTest extends BaseAndroidJVMTest
{
super.setUp();
today = DateUtils.getStartOfToday();
today = DateUtils.getToday();
habit = fixtures.createEmptyHabit();
commandRunner = mock(CommandRunner.class);
notificationTray = mock(NotificationTray.class);