Skip to content

Commit

Permalink
Merge branch 'hotfix/1.7.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jun 4, 2017
2 parents 534e6c2 + cf66587 commit be3d714
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 53 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<manifest
package="org.isoron.uhabits"
xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="30"
android:versionName="1.7.3">
android:versionCode="31"
android:versionName="1.7.4">

<uses-permission android:name="android.permission.VIBRATE"/>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/isoron/uhabits/models/ScoreList.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public int getTodayValue()
* @param timestamp the timestamp of a day
* @return score value for that day
*/
public final int getValue(long timestamp)
public final synchronized int getValue(long timestamp)
{
compute(timestamp, timestamp);
Score s = getComputedByTimestamp(timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@
public class SQLiteCheckmarkList extends CheckmarkList
{

private static final String ADD_QUERY =
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";

private static final String INVALIDATE_QUERY =
"delete from Checkmarks where habit = ? and timestamp >= ?";

@Nullable
private HabitRecord habitRecord;

@NonNull
private final SQLiteUtils<CheckmarkRecord> sqlite;

@Nullable
private Integer todayValue;
private CachedData cache;

@NonNull
private final SQLiteStatement invalidateStatement;
Expand All @@ -56,12 +62,6 @@ public class SQLiteCheckmarkList extends CheckmarkList
@NonNull
private final SQLiteDatabase db;

private static final String ADD_QUERY =
"insert into Checkmarks(habit, timestamp, value) values (?,?,?)";

private static final String INVALIDATE_QUERY =
"delete from Checkmarks where habit = ? and timestamp >= ?";

public SQLiteCheckmarkList(Habit habit)
{
super(habit);
Expand Down Expand Up @@ -102,8 +102,7 @@ public List<Checkmark> getByInterval(long fromTimestamp, long toTimestamp)
check(habit.getId());
compute(fromTimestamp, toTimestamp);

String query = "select habit, timestamp, value " +
"from checkmarks " +
String query = "select habit, timestamp, value from checkmarks " +
"where habit = ? and timestamp >= ? and timestamp <= ? " +
"order by timestamp desc";

Expand All @@ -127,10 +126,19 @@ public List<Checkmark> getByInterval(long fromTimestamp, long toTimestamp)
return toCheckmarks(records);
}

@Override
public int getTodayValue()
{
if (cache == null || cache.expired())
cache = new CachedData(super.getTodayValue());

return cache.todayValue;
}

@Override
public void invalidateNewerThan(long timestamp)
{
todayValue = null;
cache = null;
invalidateStatement.bindLong(1, habit.getId());
invalidateStatement.bindLong(2, timestamp);
invalidateStatement.execute();
Expand All @@ -142,10 +150,8 @@ public void invalidateNewerThan(long timestamp)
protected Checkmark getNewestComputed()
{
check(habit.getId());
String query = "select habit, timestamp, value " +
"from checkmarks " +
"where habit = ? " +
"order by timestamp desc " +
String query = "select habit, timestamp, value from checkmarks " +
"where habit = ? " + "order by timestamp desc " +
"limit 1";

String params[] = { Long.toString(habit.getId()) };
Expand All @@ -157,10 +163,8 @@ protected Checkmark getNewestComputed()
protected Checkmark getOldestComputed()
{
check(habit.getId());
String query = "select habit, timestamp, value " +
"from checkmarks " +
"where habit = ? " +
"order by timestamp asc " +
String query = "select habit, timestamp, value from checkmarks " +
"where habit = ? " + "order by timestamp asc " +
"limit 1";

String params[] = { Long.toString(habit.getId()) };
Expand Down Expand Up @@ -194,10 +198,21 @@ private List<Checkmark> toCheckmarks(@NonNull List<CheckmarkRecord> records)
return checkmarks;
}

@Override
public int getTodayValue()
private static class CachedData
{
if(todayValue == null) todayValue = super.getTodayValue();
return todayValue;
int todayValue;

private long today;

CachedData(int todayValue)
{
this.todayValue = todayValue;
this.today = DateUtils.getStartOfToday();
}

boolean expired()
{
return today != DateUtils.getStartOfToday();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ protected synchronized List<Habit> toList()
for (HabitRecord record : recordList)
{
Habit habit = getById(record.getId());
if (habit == null)
throw new RuntimeException("habit not in database");

if (habit == null) continue;
if (!filter.matches(habit)) continue;
habits.add(habit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.isoron.uhabits.models.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.jetbrains.annotations.*;

import java.util.*;
Expand All @@ -36,30 +37,29 @@
*/
public class SQLiteScoreList extends ScoreList
{
public static final String ADD_QUERY =
"insert into Score(habit, timestamp, score) values (?,?,?)";

public static final String INVALIDATE_QUERY =
"delete from Score where habit = ? and timestamp >= ?";

@Nullable
private HabitRecord habitRecord;

@NonNull
private final SQLiteUtils<ScoreRecord> sqlite;

@Nullable
private Integer todayValue;

@NonNull
private final SQLiteStatement invalidateStatement;

@NonNull
private final SQLiteStatement addStatement;

public static final String ADD_QUERY =
"insert into Score(habit, timestamp, score) values (?,?,?)";

public static final String INVALIDATE_QUERY =
"delete from Score where habit = ? " + "and timestamp >= ?";

private final SQLiteDatabase db;

@Nullable
private CachedData cache = null;

/**
* Constructs a new ScoreList associated with the given habit.
*
Expand Down Expand Up @@ -105,15 +105,14 @@ public List<Score> getByInterval(long fromTimestamp, long toTimestamp)
check(habit.getId());
compute(fromTimestamp, toTimestamp);

String query = "select habit, timestamp, score " +
"from Score " +
"where habit = ? and timestamp >= ? and timestamp <= ? " +
"order by timestamp desc";
String query = "select habit, timestamp, score from Score " +
"where habit = ? and timestamp >= ? and timestamp <= ? " +
"order by timestamp desc";

String params[] = {
Long.toString(habit.getId()),
Long.toString(fromTimestamp),
Long.toString(toTimestamp)
Long.toString(habit.getId()),
Long.toString(fromTimestamp),
Long.toString(toTimestamp)
};

List<ScoreRecord> records = sqlite.query(query, params);
Expand All @@ -137,10 +136,19 @@ public Score getComputedByTimestamp(long timestamp)
return getScoreFromQuery(query, params);
}

@Override
public int getTodayValue()
{
if (cache == null || cache.expired())
cache = new CachedData(super.getTodayValue());

return cache.todayValue;
}

@Override
public void invalidateNewerThan(long timestamp)
{
todayValue = null;
cache = null;
invalidateStatement.bindLong(1, habit.getId());
invalidateStatement.bindLong(2, timestamp);
invalidateStatement.execute();
Expand Down Expand Up @@ -171,8 +179,7 @@ protected Score getNewestComputed()
{
check(habit.getId());
String query = "select habit, timestamp, score from Score " +
"where habit = ? order by timestamp desc " +
"limit 1";
"where habit = ? order by timestamp desc limit 1";

String params[] = { Long.toString(habit.getId()) };
return getScoreFromQuery(query, params);
Expand All @@ -184,8 +191,7 @@ protected Score getOldestComputed()
{
check(habit.getId());
String query = "select habit, timestamp, score from Score " +
"where habit = ? order by timestamp asc " +
"limit 1";
"where habit = ? order by timestamp asc limit 1";

String params[] = { Long.toString(habit.getId()) };
return getScoreFromQuery(query, params);
Expand Down Expand Up @@ -217,10 +223,21 @@ private List<Score> toScores(@NonNull List<ScoreRecord> records)
return scores;
}

@Override
public int getTodayValue()
private static class CachedData
{
if (todayValue == null) todayValue = super.getTodayValue();
return todayValue;
int todayValue;

private long today;

CachedData(int todayValue)
{
this.todayValue = todayValue;
this.today = DateUtils.getStartOfToday();
}

boolean expired()
{
return today != DateUtils.getStartOfToday();
}
}
}

0 comments on commit be3d714

Please sign in to comment.