diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/RepetitionList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/RepetitionList.java index 45a988426..7ff5ffbf2 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/RepetitionList.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/RepetitionList.java @@ -25,11 +25,13 @@ import org.isoron.uhabits.core.utils.*; import java.util.*; -public abstract class RepetitionList +public class RepetitionList { @NonNull protected final Habit habit; + private final ArrayList list = new ArrayList<>(); + public RepetitionList(@NonNull Habit habit) { this.habit = habit; @@ -43,7 +45,10 @@ public abstract class RepetitionList * * @param entry the checkmark to be added. */ - public abstract void add(Entry entry); + public void add(Entry entry) + { + list.add(entry); + } /** * Returns the list of checkmarks that happened within the given time @@ -57,8 +62,22 @@ public abstract class RepetitionList * @param toTimestamp timestamp of the end of the interval * @return list of checkmarks within given time interval */ - public abstract List getByInterval(Timestamp fromTimestamp, - Timestamp toTimestamp); + public List getByInterval(Timestamp fromTimestamp, Timestamp toTimestamp) + { + ArrayList filtered = new ArrayList<>(); + + for (Entry r : list) + { + Timestamp t = r.getTimestamp(); + if (t.isOlderThan(fromTimestamp) || t.isNewerThan(toTimestamp)) continue; + filtered.add(r); + } + + Collections.sort(filtered, + (r1, r2) -> r1.getTimestamp().compare(r2.getTimestamp())); + + return filtered; + } /** * Returns the checkmark that has the given timestamp, or null if none @@ -68,7 +87,13 @@ public abstract class RepetitionList * @return the checkmark that has the given timestamp. */ @Nullable - public abstract Entry getByTimestamp(Timestamp timestamp); + public Entry getByTimestamp(Timestamp timestamp) + { + for (Entry r : list) + if (r.getTimestamp().equals(timestamp)) return r; + + return null; + } /** * If a checkmark with the given timestamp exists, return its value. Otherwise, returns @@ -91,9 +116,23 @@ public abstract class RepetitionList * @return oldest checkmark in the list, or null if list is empty. */ @Nullable - public abstract Entry getOldest(); + public Entry getOldest() + { + Timestamp oldestTimestamp = Timestamp.ZERO.plus(1000000); + Entry oldestRep = null; + + for (Entry rep : list) + { + if (rep.getTimestamp().isOlderThan(oldestTimestamp)) + { + oldestRep = rep; + oldestTimestamp = rep.getTimestamp(); + } + } + + return oldestRep; + } - @Nullable /** * Returns the newest checkmark in the list. *

@@ -102,7 +141,23 @@ public abstract class RepetitionList * * @return newest checkmark in the list, or null if list is empty. */ - public abstract Entry getNewest(); + @Nullable + public Entry getNewest() + { + Timestamp newestTimestamp = Timestamp.ZERO; + Entry newestRep = null; + + for (Entry rep : list) + { + if (rep.getTimestamp().isNewerThan(newestTimestamp)) + { + newestRep = rep; + newestTimestamp = rep.getTimestamp(); + } + } + + return newestRep; + } /** * Returns the total number of successful checkmarks for each month, from the first @@ -159,9 +214,20 @@ public abstract class RepetitionList * * @param entry the checkmark to be removed */ - public abstract void remove(@NonNull Entry entry); + public void remove(@NonNull Entry entry) + { + list.remove(entry); + } - public abstract long getTotalCount(); + + public long getTotalCount() + { + int count = 0; + for (Entry rep : list) + if (rep.getValue() == Entry.YES_MANUAL) + count++; + return count; + } public void setValue(Timestamp timestamp, int value) { @@ -171,5 +237,8 @@ public abstract class RepetitionList habit.invalidateNewerThan(timestamp); } - public abstract void removeAll(); + public void removeAll() + { + list.clear(); + } } diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.java index f5b56a4ea..da7161b69 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryModelFactory.java @@ -40,7 +40,7 @@ public class MemoryModelFactory implements ModelFactory @Override public RepetitionList buildRepetitionList(Habit habit) { - return new MemoryRepetitionList(habit); + return new RepetitionList(habit); } @Override diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryRepetitionList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryRepetitionList.java deleted file mode 100644 index 6980f85dd..000000000 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/memory/MemoryRepetitionList.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2016 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.core.models.memory; - -import androidx.annotation.*; - -import org.isoron.uhabits.core.models.*; - -import java.util.*; - -/** - * In-memory implementation of {@link RepetitionList}. - */ -public class MemoryRepetitionList extends RepetitionList -{ - ArrayList list; - - public MemoryRepetitionList(Habit habit) - { - super(habit); - list = new ArrayList<>(); - } - - @Override - public void add(Entry repetition) - { - list.add(repetition); - } - - @Override - public List getByInterval(Timestamp fromTimestamp, Timestamp toTimestamp) - { - ArrayList filtered = new ArrayList<>(); - - for (Entry r : list) - { - Timestamp t = r.getTimestamp(); - if (t.isOlderThan(fromTimestamp) || t.isNewerThan(toTimestamp)) continue; - filtered.add(r); - } - - Collections.sort(filtered, - (r1, r2) -> r1.getTimestamp().compare(r2.getTimestamp())); - - return filtered; - } - - @Nullable - @Override - public Entry getByTimestamp(Timestamp timestamp) - { - for (Entry r : list) - if (r.getTimestamp().equals(timestamp)) return r; - - return null; - } - - @Nullable - @Override - public Entry getOldest() - { - Timestamp oldestTimestamp = Timestamp.ZERO.plus(1000000); - Entry oldestRep = null; - - for (Entry rep : list) - { - if (rep.getTimestamp().isOlderThan(oldestTimestamp)) - { - oldestRep = rep; - oldestTimestamp = rep.getTimestamp(); - } - } - - return oldestRep; - } - - @Nullable - @Override - public Entry getNewest() - { - Timestamp newestTimestamp = Timestamp.ZERO; - Entry newestRep = null; - - for (Entry rep : list) - { - if (rep.getTimestamp().isNewerThan(newestTimestamp)) - { - newestRep = rep; - newestTimestamp = rep.getTimestamp(); - } - } - - return newestRep; - } - - @Override - public void remove(@NonNull Entry repetition) - { - list.remove(repetition); - } - - @Override - public long getTotalCount() - { - int count = 0; - for (Entry rep : list) - if (rep.getValue() == Entry.YES_MANUAL) - count++; - return count; - } - - @Override - public void removeAll() - { - list.clear(); - } -} diff --git a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteRepetitionList.java b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteRepetitionList.java index 68ffaf11a..11f9d2af6 100644 --- a/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteRepetitionList.java +++ b/android/uhabits-core/src/main/java/org/isoron/uhabits/core/models/sqlite/SQLiteRepetitionList.java @@ -21,12 +21,11 @@ package org.isoron.uhabits.core.models.sqlite; -import androidx.annotation.*; import androidx.annotation.Nullable; +import androidx.annotation.*; import org.isoron.uhabits.core.database.*; import org.isoron.uhabits.core.models.*; -import org.isoron.uhabits.core.models.memory.*; import org.isoron.uhabits.core.models.sqlite.records.*; import org.jetbrains.annotations.*; @@ -39,8 +38,6 @@ public class SQLiteRepetitionList extends RepetitionList { private final Repository repository; - private final MemoryRepetitionList list; - private boolean loaded = false; public SQLiteRepetitionList(@NonNull Habit habit, @@ -48,7 +45,6 @@ public class SQLiteRepetitionList extends RepetitionList { super(habit); repository = modelFactory.buildRepetitionListRepository(); - list = new MemoryRepetitionList(habit); } private void loadRecords() @@ -58,18 +54,18 @@ public class SQLiteRepetitionList extends RepetitionList check(habit.getId()); List records = - repository.findAll("where habit = ? order by timestamp", - habit.getId().toString()); + repository.findAll("where habit = ? order by timestamp", + habit.getId().toString()); for (RepetitionRecord rec : records) - list.add(rec.toCheckmark()); + super.add(rec.toCheckmark()); } @Override public void add(Entry entry) { loadRecords(); - list.add(entry); + super.add(entry); check(habit.getId()); RepetitionRecord record = new RepetitionRecord(); record.habit_id = habit.getId(); @@ -81,7 +77,7 @@ public class SQLiteRepetitionList extends RepetitionList public List getByInterval(Timestamp timeFrom, Timestamp timeTo) { loadRecords(); - return list.getByInterval(timeFrom, timeTo); + return super.getByInterval(timeFrom, timeTo); } @Override @@ -89,53 +85,48 @@ public class SQLiteRepetitionList extends RepetitionList public Entry getByTimestamp(Timestamp timestamp) { loadRecords(); - return list.getByTimestamp(timestamp); + return super.getByTimestamp(timestamp); } @Override public Entry getOldest() { loadRecords(); - return list.getOldest(); + return super.getOldest(); } @Override public Entry getNewest() { loadRecords(); - return list.getNewest(); + return super.getNewest(); } @Override public void remove(@NonNull Entry entry) { loadRecords(); - list.remove(entry); + super.remove(entry); check(habit.getId()); repository.execSQL( - "delete from repetitions where habit = ? and timestamp = ?", - habit.getId(), entry.getTimestamp().getUnixTime()); + "delete from repetitions where habit = ? and timestamp = ?", + habit.getId(), entry.getTimestamp().getUnixTime()); } public void removeAll() { loadRecords(); - list.removeAll(); + super.removeAll(); check(habit.getId()); repository.execSQL("delete from repetitions where habit = ?", - habit.getId()); + habit.getId()); } @Override public long getTotalCount() { loadRecords(); - return list.getTotalCount(); - } - - public void reload() - { - loaded = false; + return super.getTotalCount(); } @Contract("null -> fail")