Skip to content

Commit

Permalink
Replace Long by Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jul 22, 2017
1 parent 882ddba commit a8aa6f1
Show file tree
Hide file tree
Showing 80 changed files with 672 additions and 526 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,8 @@ protected void stopTracing()
Debug.stopMethodTracing();
}

protected Long day(int offset)
protected Timestamp day(int offset)
{
return DateUtils.getStartOfToday() -
offset * DateUtils.millisecondsInOneDay;
return DateUtils.getToday().minus(offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void resetState() throws Exception
{
prefs.reset();
prefs.setFirstRun(false);
prefs.updateLastHint(100, DateUtils.getStartOfToday());
prefs.updateLastHint(100, DateUtils.getToday());
habitList.removeAll();
cache.refreshAllHabits();
Thread.sleep(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ public Habit createLongHabit()
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;
}
Expand All @@ -89,12 +88,12 @@ public Habit createLongNumericalHabit()
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;
Expand All @@ -108,11 +107,11 @@ public Habit createShortHabit()
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ public void setUp()
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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()
Expand All @@ -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))))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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))))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.junit.runner.*;

@RunWith(AndroidJUnit4.class)
@LargeTest
@MediumTest
public class PerformanceTest extends BaseAndroidTest
{
private Habit habit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,12 @@ public void populateWithRandomData()
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);
Expand Down Expand Up @@ -205,7 +203,7 @@ protected void onDraw(Canvas canvas)
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);
Expand Down Expand Up @@ -286,13 +284,13 @@ private void drawBar(Canvas canvas, RectF rect, double value)
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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.*;

Expand Down Expand Up @@ -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)
Expand All @@ -90,23 +91,21 @@ public void setColor(int color)
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;
}

Expand Down Expand Up @@ -194,7 +193,7 @@ protected void onSizeChanged(int width,

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);

Expand Down Expand Up @@ -324,7 +323,7 @@ public void populateWithRandomData()
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@

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
{
Expand Down Expand Up @@ -135,10 +136,11 @@ public boolean onSingleTapUp(MotionEvent e)
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;
Expand Down Expand Up @@ -435,7 +437,8 @@ private void initRects()
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);
Expand All @@ -450,15 +453,7 @@ private Long positionToTimestamp(float x, float y)
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()
Expand All @@ -478,6 +473,6 @@ private void updateDate()

public interface Controller
{
default void onToggleCheckmark(long timestamp) {}
default void onToggleCheckmark(Timestamp timestamp) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,15 @@ public void populateWithRandomData()
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;
}
}

Expand Down Expand Up @@ -189,7 +187,7 @@ protected void onDraw(Canvas canvas)
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);

Expand Down Expand Up @@ -258,13 +256,13 @@ protected void onSizeChanged(int width,
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);
Expand Down
Loading

0 comments on commit a8aa6f1

Please sign in to comment.