|
|
@ -27,6 +27,7 @@ import com.activeandroid.*;
|
|
|
|
|
|
|
|
|
|
|
|
import org.isoron.uhabits.models.*;
|
|
|
|
import org.isoron.uhabits.models.*;
|
|
|
|
import org.isoron.uhabits.models.sqlite.records.*;
|
|
|
|
import org.isoron.uhabits.models.sqlite.records.*;
|
|
|
|
|
|
|
|
import org.isoron.uhabits.utils.*;
|
|
|
|
import org.jetbrains.annotations.*;
|
|
|
|
import org.jetbrains.annotations.*;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
@ -36,6 +37,11 @@ import java.util.*;
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public class SQLiteScoreList extends ScoreList
|
|
|
|
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
|
|
|
|
@Nullable
|
|
|
|
private HabitRecord habitRecord;
|
|
|
|
private HabitRecord habitRecord;
|
|
|
@ -43,23 +49,17 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private final SQLiteUtils<ScoreRecord> sqlite;
|
|
|
|
private final SQLiteUtils<ScoreRecord> sqlite;
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
|
|
|
private Integer todayValue;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private final SQLiteStatement invalidateStatement;
|
|
|
|
private final SQLiteStatement invalidateStatement;
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@NonNull
|
|
|
|
private final SQLiteStatement addStatement;
|
|
|
|
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;
|
|
|
|
private final SQLiteDatabase db;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
|
|
|
private CachedData cache = null;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Constructs a new ScoreList associated with the given habit.
|
|
|
|
* Constructs a new ScoreList associated with the given habit.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -105,8 +105,7 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
check(habit.getId());
|
|
|
|
check(habit.getId());
|
|
|
|
compute(fromTimestamp, toTimestamp);
|
|
|
|
compute(fromTimestamp, toTimestamp);
|
|
|
|
|
|
|
|
|
|
|
|
String query = "select habit, timestamp, score " +
|
|
|
|
String query = "select habit, timestamp, score from Score " +
|
|
|
|
"from Score " +
|
|
|
|
|
|
|
|
"where habit = ? and timestamp >= ? and timestamp <= ? " +
|
|
|
|
"where habit = ? and timestamp >= ? and timestamp <= ? " +
|
|
|
|
"order by timestamp desc";
|
|
|
|
"order by timestamp desc";
|
|
|
|
|
|
|
|
|
|
|
@ -137,10 +136,19 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public int getTodayValue()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (cache == null || cache.expired())
|
|
|
|
|
|
|
|
cache = new CachedData(super.getTodayValue());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cache.todayValue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void invalidateNewerThan(long timestamp)
|
|
|
|
public void invalidateNewerThan(long timestamp)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
todayValue = null;
|
|
|
|
cache = null;
|
|
|
|
invalidateStatement.bindLong(1, habit.getId());
|
|
|
|
invalidateStatement.bindLong(1, habit.getId());
|
|
|
|
invalidateStatement.bindLong(2, timestamp);
|
|
|
|
invalidateStatement.bindLong(2, timestamp);
|
|
|
|
invalidateStatement.execute();
|
|
|
|
invalidateStatement.execute();
|
|
|
@ -171,8 +179,7 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
{
|
|
|
|
{
|
|
|
|
check(habit.getId());
|
|
|
|
check(habit.getId());
|
|
|
|
String query = "select habit, timestamp, score from Score " +
|
|
|
|
String query = "select habit, timestamp, score from Score " +
|
|
|
|
"where habit = ? order by timestamp desc " +
|
|
|
|
"where habit = ? order by timestamp desc limit 1";
|
|
|
|
"limit 1";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String params[] = { Long.toString(habit.getId()) };
|
|
|
|
String params[] = { Long.toString(habit.getId()) };
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
@ -184,8 +191,7 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
{
|
|
|
|
{
|
|
|
|
check(habit.getId());
|
|
|
|
check(habit.getId());
|
|
|
|
String query = "select habit, timestamp, score from Score " +
|
|
|
|
String query = "select habit, timestamp, score from Score " +
|
|
|
|
"where habit = ? order by timestamp asc " +
|
|
|
|
"where habit = ? order by timestamp asc limit 1";
|
|
|
|
"limit 1";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String params[] = { Long.toString(habit.getId()) };
|
|
|
|
String params[] = { Long.toString(habit.getId()) };
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
|
return getScoreFromQuery(query, params);
|
|
|
@ -217,10 +223,21 @@ public class SQLiteScoreList extends ScoreList
|
|
|
|
return scores;
|
|
|
|
return scores;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private static class CachedData
|
|
|
|
public int getTodayValue()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (todayValue == null) todayValue = super.getTodayValue();
|
|
|
|
int todayValue;
|
|
|
|
return todayValue;
|
|
|
|
|
|
|
|
|
|
|
|
private long today;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CachedData(int todayValue)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
this.todayValue = todayValue;
|
|
|
|
|
|
|
|
this.today = DateUtils.getStartOfToday();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean expired()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return today != DateUtils.getStartOfToday();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|