Fetch all the data with one call

pull/30/head
Alinson S. Xavier 10 years ago
parent af0ef90e4d
commit 5e21d877c5

@ -294,6 +294,16 @@ public class Habit extends Model
return checks;
}
public int[] getAllCheckmarks()
{
Repetition oldestRep = getOldestRep();
if(oldestRep == null) return new int[0];
Long toTimestamp = DateHelper.getStartOfToday();
Long fromTimestamp = oldestRep.timestamp;
return getCheckmarks(fromTimestamp, toTimestamp);
}
public void updateCheckmarks()
{
long beginning;
@ -512,13 +522,40 @@ public class Habit extends Model
return lastScore;
}
public List<Score> getScores(long fromTimestamp, long toTimestamp, int divisor, long offset)
public int[] getScores(Long fromTimestamp, Long toTimestamp, Integer divisor, Long offset)
{
return new Select().from(Score.class)
.where("habit = ? and timestamp > ? and " +
"timestamp <= ? and (timestamp - ?) % ? = 0", getId(), fromTimestamp,
toTimestamp, offset, divisor)
.execute();
String query = "select score from Score where habit = ? and timestamp > ? and " +
"timestamp <= ? and (timestamp - ?) % ? = 0 order by timestamp desc";
String params[] = {getId().toString(), fromTimestamp.toString(), toTimestamp.toString(),
offset.toString(), divisor.toString()};
SQLiteDatabase db = Cache.openDatabase();
Cursor c = db.rawQuery(query, params);
if(!c.moveToFirst()) return new int[0];
int k = 0;
int[] scores = new int[c.getCount()];
do
{
scores[k++] = c.getInt(0);
}
while (c.moveToNext());
return scores;
}
public int[] getAllScores(int divisor)
{
Repetition oldestRep = getOldestRep();
if(oldestRep == null) return new int[0];
long fromTimestamp = oldestRep.timestamp;
long toTimestamp = DateHelper.getStartOfToday();
return getScores(fromTimestamp, toTimestamp, divisor, toTimestamp);
}
public List<Streak> getStreaks()

@ -126,22 +126,7 @@ public class HabitHistoryView extends ScrollableDataView
protected void fetchData()
{
Calendar currentDate = new GregorianCalendar();
currentDate.add(Calendar.DAY_OF_YEAR, -dataOffset * 7);
int dayOfWeek = currentDate.get(Calendar.DAY_OF_WEEK) % 7;
long dateTo = DateHelper.getStartOfToday();
for (int i = 0; i < 7 - dayOfWeek; i++)
dateTo += DateHelper.millisecondsInOneDay;
for (int i = 0; i < dataOffset * 7; i++)
dateTo -= DateHelper.millisecondsInOneDay;
long dateFrom = dateTo;
for (int i = 0; i < (nColumns - 1) * 7; i++)
dateFrom -= DateHelper.millisecondsInOneDay;
checkmarks = habit.getCheckmarks(dateFrom, dateTo);
checkmarks = habit.getAllCheckmarks();
updateDate();
}
@ -160,6 +145,7 @@ public class HabitHistoryView extends ScrollableDataView
previousYear = "";
justPrintedYear = false;
updateDate();
GregorianCalendar currentDate = (GregorianCalendar) baseDate.clone();
for (int column = 0; column < nColumns - 1; column++)
@ -180,7 +166,7 @@ public class HabitHistoryView extends ScrollableDataView
{
if (!(column == nColumns - 2 && dataOffset == 0 && j > todayWeekday))
{
int checkmarkOffset = nDays - 7 * column - j;
int checkmarkOffset = dataOffset * 7 + nDays - 7 * (column + 1) + todayWeekday - j;
drawSquare(canvas, location, date, checkmarkOffset);
}

@ -25,10 +25,8 @@ import android.graphics.RectF;
import org.isoron.helpers.ColorHelper;
import org.isoron.helpers.DateHelper;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.models.Score;
import java.text.SimpleDateFormat;
import java.util.List;
public class HabitScoreView extends ScrollableDataView
{
@ -41,7 +39,7 @@ public class HabitScoreView extends ScrollableDataView
private Paint pText, pGraph;
private int[] colors;
private List<Score> scores;
private int[] scores;
public HabitScoreView(Context context, Habit habit, int columnWidth)
{
@ -82,17 +80,7 @@ public class HabitScoreView extends ScrollableDataView
protected void fetchData()
{
long toTimestamp = DateHelper.getStartOfToday();
for (int i = 0; i < dataOffset * BUCKET_SIZE; i++)
toTimestamp -= DateHelper.millisecondsInOneDay;
long fromTimestamp = toTimestamp;
for (int i = 0; i < nColumns * BUCKET_SIZE; i++)
fromTimestamp -= DateHelper.millisecondsInOneDay;
scores = habit.getScores(fromTimestamp, toTimestamp,
BUCKET_SIZE * DateHelper.millisecondsInOneDay, toTimestamp);
scores = habit.getAllScores(BUCKET_SIZE * DateHelper.millisecondsInOneDay);
}
@Override
@ -114,19 +102,25 @@ public class HabitScoreView extends ScrollableDataView
pGraph.setColor(habit.color);
RectF prevR = null;
for (int offset = nColumns - scores.size(); offset < nColumns; offset++)
long currentDate = DateHelper.getStartOfToday();
for(int k = 0; k < nColumns + dataOffset - 1; k++)
currentDate -= 7 * DateHelper.millisecondsInOneDay;
for (int k = 0; k < nColumns; k++)
{
Score score = scores.get(offset - nColumns + scores.size());
String month = dfMonth.format(score.timestamp);
String day = dfDay.format(score.timestamp);
String month = dfMonth.format(currentDate);
String day = dfDay.format(currentDate);
long s = score.score;
double sRelative = ((double) s) / Habit.MAX_SCORE;
int score = 0;
int offset = nColumns - k - 1 + dataOffset;
if(offset < scores.length) score = scores[offset];
double sRelative = ((double) score) / Habit.MAX_SCORE;
int height = (int) (columnHeight * sRelative);
RectF r = new RectF(0, 0, columnWidth, columnWidth);
r.offset(offset * columnWidth,
r.offset(k * columnWidth,
headerHeight + columnHeight - height - columnWidth / 2);
if (prevR != null)
@ -135,19 +129,19 @@ public class HabitScoreView extends ScrollableDataView
drawMarker(canvas, prevR);
}
if (offset == nColumns - 1) drawMarker(canvas, r);
if (k == nColumns - 1) drawMarker(canvas, r);
prevR = r;
r = new RectF(0, 0, columnWidth, columnHeight);
r.offset(offset * columnWidth, headerHeight);
r.offset(k * columnWidth, headerHeight);
if (!month.equals(previousMonth))
canvas.drawText(month, r.centerX(), r.bottom + lineHeight * 1.2f, pText);
else
canvas.drawText(day, r.centerX(), r.bottom + lineHeight * 1.2f, pText);
previousMonth = month;
currentDate += 7 * DateHelper.millisecondsInOneDay;
}
}

@ -96,13 +96,14 @@ public class HabitStreakView extends ScrollableDataView
float barHeaderOffset = lineHeight * 0.4f;
int nStreaks = streaks.size();
int start = Math.max(0, nStreaks - nColumns - dataOffset);
int start = nStreaks - nColumns - dataOffset;
SimpleDateFormat dfMonth = new SimpleDateFormat("MMM");
String previousMonth = "";
for (int offset = 0; offset < nColumns && start + offset < nStreaks; offset++)
{
if(start + offset < 0) continue;
String month = dfMonth.format(streaks.get(start + offset).start);
long l = streaks.get(offset + start).length;

@ -49,7 +49,6 @@ public abstract class ScrollableDataView extends View
if (newDataOffset != dataOffset)
{
dataOffset = newDataOffset;
fetchData();
invalidate();
return true;
}

Loading…
Cancel
Save