From 79b6ef820073f9ac1680425c324ce21e5161ef69 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Thu, 17 Mar 2016 06:39:29 -0400 Subject: [PATCH] Improve null check for Checkmark and CheckmarkList --- .../org/isoron/uhabits/models/Checkmark.java | 7 +++++++ .../isoron/uhabits/models/CheckmarkList.java | 18 +++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/isoron/uhabits/models/Checkmark.java b/app/src/main/java/org/isoron/uhabits/models/Checkmark.java index 089008efd..a6c5ec06f 100644 --- a/app/src/main/java/org/isoron/uhabits/models/Checkmark.java +++ b/app/src/main/java/org/isoron/uhabits/models/Checkmark.java @@ -43,9 +43,16 @@ public class Checkmark extends Model */ public static final int CHECKED_EXPLICITLY = 2; + /** + * The habit to which this checkmark belongs. + */ @Column(name = "habit") public Habit habit; + /** + * Timestamp of the day to which this checkmark corresponds. Time of the day must be midnight + * (UTC). + */ @Column(name = "timestamp") public Long timestamp; diff --git a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java index af638e903..cb96f74af 100644 --- a/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java +++ b/app/src/main/java/org/isoron/uhabits/models/CheckmarkList.java @@ -21,6 +21,8 @@ package org.isoron.uhabits.models; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import com.activeandroid.ActiveAndroid; import com.activeandroid.Cache; @@ -66,7 +68,8 @@ public class CheckmarkList * @param toTimestamp timestamp for the newest checkmark * @return values for the checkmarks inside the given interval */ - public int[] getValues(Long fromTimestamp, Long toTimestamp) + @NonNull + public int[] getValues(long fromTimestamp, long toTimestamp) { buildCache(fromTimestamp, toTimestamp); if(fromTimestamp > toTimestamp) return new int[0]; @@ -75,8 +78,8 @@ public class CheckmarkList "habit = ? and timestamp >= ? and timestamp <= ?"; SQLiteDatabase db = Cache.openDatabase(); - String args[] = { habit.getId().toString(), fromTimestamp.toString(), - toTimestamp.toString() }; + String args[] = { habit.getId().toString(), Long.toString(fromTimestamp), + Long.toString(toTimestamp) }; Cursor cursor = db.rawQuery(query, args); long day = DateHelper.millisecondsInOneDay; @@ -108,6 +111,7 @@ public class CheckmarkList * * @return values for the checkmarks in the interval */ + @NonNull public int[] getAllValues() { Repetition oldestRep = habit.repetitions.getOldest(); @@ -188,6 +192,7 @@ public class CheckmarkList * Returns newest checkmark that has already been computed. Ignores any checkmark that has * timestamp in the future. This does not update the cache. */ + @Nullable private Checkmark findNewest() { return new Select().from(Checkmark.class) @@ -201,6 +206,7 @@ public class CheckmarkList /** * Returns the checkmark for today. */ + @Nullable public Checkmark getToday() { long today = DateHelper.getStartOfToday(); @@ -209,10 +215,12 @@ public class CheckmarkList } /** - * Returns the value of today's checkmark. + * Returns the value of today's checkmark. If there is no checkmark today, returns UNCHECKED. */ public int getTodayValue() { - return getToday().value; + Checkmark today = getToday(); + if(today != null) return today.value; + else return Checkmark.UNCHECKED; } }