Rebuild order after removing habit

pull/312/head
Alinson S. Xavier 8 years ago
parent 6801d1d1ae
commit ea640a8a17

@ -45,7 +45,7 @@ public class PebbleReceiver extends PebbleDataReceiver
@NonNull @NonNull
private Context context; private Context context;
private HabitList allHabits; private HabitList habitList;
private CommandRunner commandRunner; private CommandRunner commandRunner;
@ -76,10 +76,10 @@ public class PebbleReceiver extends PebbleDataReceiver
HabitsApplicationComponent component = app.getComponent(); HabitsApplicationComponent component = app.getComponent();
commandRunner = component.getCommandRunner(); commandRunner = component.getCommandRunner();
taskRunner = component.getTaskRunner(); taskRunner = component.getTaskRunner();
allHabits = component.getHabitList(); habitList = component.getHabitList();
prefs = component.getPreferences(); prefs = component.getPreferences();
if(prefs.isSyncEnabled()) if (prefs.isSyncEnabled())
context.startService(new Intent(context, SyncService.class)); context.startService(new Intent(context, SyncService.class));
HabitMatcher build = new HabitMatcherBuilder() HabitMatcher build = new HabitMatcherBuilder()
@ -87,7 +87,7 @@ public class PebbleReceiver extends PebbleDataReceiver
.setCompletedAllowed(false) .setCompletedAllowed(false)
.build(); .build();
filteredHabits = allHabits.getFiltered(build); filteredHabits = habitList.getFiltered(build);
PebbleKit.sendAckToPebble(context, transactionId); PebbleKit.sendAckToPebble(context, transactionId);
Log.d("PebbleReceiver", "<-- " + data.getString(0)); Log.d("PebbleReceiver", "<-- " + data.getString(0));
@ -117,8 +117,6 @@ public class PebbleReceiver extends PebbleDataReceiver
if (position < 0 || position >= filteredHabits.size()) return; if (position < 0 || position >= filteredHabits.size()) return;
Habit habit = filteredHabits.getByPosition(position.intValue()); Habit habit = filteredHabits.getByPosition(position.intValue());
if (habit == null) return;
sendHabit(habit); sendHabit(habit);
} }
@ -127,12 +125,12 @@ public class PebbleReceiver extends PebbleDataReceiver
Long habitId = dict.getInteger(1); Long habitId = dict.getInteger(1);
if (habitId == null) return; if (habitId == null) return;
Habit habit = allHabits.getById(habitId); Habit habit = habitList.getById(habitId);
if (habit == null) return; if (habit == null) return;
long today = DateUtils.getStartOfToday(); long today = DateUtils.getStartOfToday();
commandRunner.execute(new ToggleRepetitionCommand(habit, today), commandRunner.execute(
habitId); new ToggleRepetitionCommand(habitList, habit, today), habitId);
sendOK(); sendOK();
} }
@ -150,8 +148,7 @@ public class PebbleReceiver extends PebbleDataReceiver
private void sendDict(@NonNull PebbleDictionary dict) private void sendDict(@NonNull PebbleDictionary dict)
{ {
PebbleKit.sendDataToPebble(context, PebbleKit.sendDataToPebble(context, PebbleReceiver.WATCHAPP_UUID, dict);
PebbleReceiver.WATCHAPP_UUID, dict);
} }
private void sendHabit(@NonNull Habit habit) private void sendHabit(@NonNull Habit habit)

@ -53,7 +53,8 @@ public class WidgetControllerTest extends BaseAndroidUnitTest
habit = fixtures.createEmptyHabit(); habit = fixtures.createEmptyHabit();
commandRunner = mock(CommandRunner.class); commandRunner = mock(CommandRunner.class);
notificationTray = mock(NotificationTray.class); notificationTray = mock(NotificationTray.class);
controller = new WidgetBehavior(commandRunner, notificationTray); controller =
new WidgetBehavior(habitList, commandRunner, notificationTray);
} }
@Test @Test

@ -28,14 +28,20 @@ import org.isoron.uhabits.core.models.*;
*/ */
public class ToggleRepetitionCommand extends Command public class ToggleRepetitionCommand extends Command
{ {
@NonNull
private HabitList list;
final long timestamp; final long timestamp;
@NonNull @NonNull
final Habit habit; final Habit habit;
public ToggleRepetitionCommand(@NonNull Habit habit, long timestamp) public ToggleRepetitionCommand(@NonNull HabitList list,
@NonNull Habit habit,
long timestamp)
{ {
super(); super();
this.list = list;
this.timestamp = timestamp; this.timestamp = timestamp;
this.habit = habit; this.habit = habit;
} }
@ -44,6 +50,7 @@ public class ToggleRepetitionCommand extends Command
public void execute() public void execute()
{ {
habit.getRepetitions().toggle(timestamp); habit.getRepetitions().toggle(timestamp);
list.update(habit);
} }
@NonNull @NonNull
@ -81,7 +88,7 @@ public class ToggleRepetitionCommand extends Command
{ {
id = command.getId(); id = command.getId();
Long habitId = command.habit.getId(); Long habitId = command.habit.getId();
if(habitId == null) throw new RuntimeException("Habit not saved"); if (habitId == null) throw new RuntimeException("Habit not saved");
this.repTimestamp = command.timestamp; this.repTimestamp = command.timestamp;
this.habit = habitId; this.habit = habitId;
@ -90,10 +97,10 @@ public class ToggleRepetitionCommand extends Command
public ToggleRepetitionCommand toCommand(@NonNull HabitList habitList) public ToggleRepetitionCommand toCommand(@NonNull HabitList habitList)
{ {
Habit h = habitList.getById(habit); Habit h = habitList.getById(habit);
if(h == null) throw new HabitNotFoundException(); if (h == null) throw new HabitNotFoundException();
ToggleRepetitionCommand command; ToggleRepetitionCommand command;
command = new ToggleRepetitionCommand(h, repTimestamp); command = new ToggleRepetitionCommand(habitList, h, repTimestamp);
command.setId(id); command.setId(id);
return command; return command;
} }

@ -118,6 +118,7 @@ public class MemoryHabitList extends HabitList
this.order = order; this.order = order;
this.comparator = getComparatorByOrder(order); this.comparator = getComparatorByOrder(order);
resort(); resort();
getObservable().notifyListeners();
} }
private Comparator<Habit> getComparatorByOrder(Order order) private Comparator<Habit> getComparatorByOrder(Order order)
@ -135,9 +136,10 @@ public class MemoryHabitList extends HabitList
Comparator<Habit> scoreComparator = (h1, h2) -> Comparator<Habit> scoreComparator = (h1, h2) ->
{ {
double s1 = h1.getScores().getTodayValue(); Double s1 = h1.getScores().getTodayValue();
double s2 = h2.getScores().getTodayValue(); Double s2 = h2.getScores().getTodayValue();
return Double.compare(s2, s1); if (s1.equals(s2)) return nameComparator.compare(h1, h2);
return s2.compareTo(s1);
}; };
if (order == BY_POSITION) return null; if (order == BY_POSITION) return null;
@ -165,6 +167,7 @@ public class MemoryHabitList extends HabitList
{ {
throwIfHasParent(); throwIfHasParent();
list.remove(habit); list.remove(habit);
getObservable().notifyListeners();
} }
@Override @Override
@ -182,6 +185,7 @@ public class MemoryHabitList extends HabitList
list.remove(from); list.remove(from);
list.add(toPos, from); list.add(toPos, from);
getObservable().notifyListeners();
} }
@Override @Override
@ -193,7 +197,8 @@ public class MemoryHabitList extends HabitList
@Override @Override
public void update(List<Habit> habits) public void update(List<Habit> habits)
{ {
// NOP resort();
getObservable().notifyListeners();
} }
private void throwIfHasParent() private void throwIfHasParent()

@ -127,5 +127,6 @@ public class MemoryRepetitionList extends RepetitionList
public void removeAll() public void removeAll()
{ {
list.clear(); list.clear();
getObservable().notifyListeners();
} }
} }

@ -41,6 +41,7 @@ public class MemoryScoreList extends ScoreList
list.addAll(scores); list.addAll(scores);
Collections.sort(list, Collections.sort(list,
(s1, s2) -> Long.signum(s2.getTimestamp() - s1.getTimestamp())); (s1, s2) -> Long.signum(s2.getTimestamp() - s1.getTimestamp()));
getObservable().notifyListeners();
} }
@NonNull @NonNull
@ -71,13 +72,7 @@ public class MemoryScoreList extends ScoreList
@Override @Override
public void invalidateNewerThan(long timestamp) public void invalidateNewerThan(long timestamp)
{ {
List<Score> discard = new LinkedList<>(); list.clear();
for (Score s : list)
if (s.getTimestamp() >= timestamp) discard.add(s);
list.removeAll(discard);
getObservable().notifyListeners(); getObservable().notifyListeners();
} }

@ -19,8 +19,9 @@
package org.isoron.uhabits.core.models.memory; package org.isoron.uhabits.core.models.memory;
import android.support.annotation.*;
import org.isoron.uhabits.core.models.*; import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import java.util.*; import java.util.*;
@ -48,21 +49,16 @@ public class MemoryStreakList extends StreakList
@Override @Override
public void invalidateNewerThan(long timestamp) public void invalidateNewerThan(long timestamp)
{ {
LinkedList<Streak> discard = new LinkedList<>(); list.clear();
for (Streak s : list)
if (s.getEnd() >= timestamp - DateUtils.millisecondsInOneDay)
discard.add(s);
list.removeAll(discard);
observable.notifyListeners(); observable.notifyListeners();
} }
@Override @Override
protected void add(List<Streak> streaks) protected void add(@NonNull List<Streak> streaks)
{ {
list.addAll(streaks); list.addAll(streaks);
Collections.sort(list, (s1, s2) -> s2.compareNewer(s1)); Collections.sort(list, (s1, s2) -> s2.compareNewer(s1));
observable.notifyListeners();
} }
@Override @Override

@ -89,8 +89,9 @@ public class SQLiteHabitList extends HabitList
HabitRecord record = new HabitRecord(); HabitRecord record = new HabitRecord();
record.copyFrom(habit); record.copyFrom(habit);
record.position = list.indexOf(habit); record.position = size();
repository.save(record); repository.save(record);
rebuildOrder();
getObservable().notifyListeners(); getObservable().notifyListeners();
} }
@ -130,6 +131,7 @@ public class SQLiteHabitList extends HabitList
public void setOrder(@NonNull Order order) public void setOrder(@NonNull Order order)
{ {
list.setOrder(order); list.setOrder(order);
getObservable().notifyListeners();
} }
@Override @Override
@ -146,22 +148,16 @@ public class SQLiteHabitList extends HabitList
return list.iterator(); return list.iterator();
} }
private void rebuildOrder() private synchronized void rebuildOrder()
{ {
// List<Habit> habits = toList(); List<HabitRecord> records = repository.findAll("order by position");
// repository.executeAsTransaction(() -> {
// int i = 0; int pos = 0;
// for (Habit h : habits) for (HabitRecord r : records) {
// { r.position = pos++;
// HabitRecord record = repository.find(h.getId()); repository.save(r);
// if (record == null) }
// throw new RuntimeException("habit not in database"); });
//
// record.position = i++;
// repository.save(record);
// }
//
// update(habits);
} }
@Override @Override
@ -178,6 +174,7 @@ public class SQLiteHabitList extends HabitList
repository.remove(record); repository.remove(record);
}); });
rebuildOrder(); rebuildOrder();
getObservable().notifyListeners(); getObservable().notifyListeners();
} }
@ -231,6 +228,7 @@ public class SQLiteHabitList extends HabitList
{ {
loadRecords(); loadRecords();
rebuildOrder(); rebuildOrder();
getObservable().notifyListeners();
} }
@Override @Override
@ -244,11 +242,12 @@ public class SQLiteHabitList extends HabitList
public synchronized void update(List<Habit> habits) public synchronized void update(List<Habit> habits)
{ {
loadRecords(); loadRecords();
list.update(habits);
for (Habit h : habits) for (Habit h : habits)
{ {
HabitRecord record = repository.find(h.getId()); HabitRecord record = repository.find(h.getId());
if (record == null) if (record == null) continue;
throw new RuntimeException("habit not in database");
record.copyFrom(h); record.copyFrom(h);
repository.save(record); repository.save(record);
} }

@ -151,7 +151,8 @@ public class ListHabitsBehavior
public void onToggle(@NonNull Habit habit, long timestamp) public void onToggle(@NonNull Habit habit, long timestamp)
{ {
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp), commandRunner.execute(
new ToggleRepetitionCommand(habitList, habit, timestamp),
habit.getId()); habit.getId());
} }
@ -168,6 +169,11 @@ public class ListHabitsBehavior
String getBugReport() throws IOException; String getBugReport() throws IOException;
} }
public interface DirFinder
{
File getCSVOutputDir();
}
public interface NumberPickerCallback public interface NumberPickerCallback
{ {
void onNumberPicked(double newValue); void onNumberPicked(double newValue);
@ -189,9 +195,4 @@ public class ListHabitsBehavior
void showSendFileScreen(@NonNull String filename); void showSendFileScreen(@NonNull String filename);
} }
public interface DirFinder
{
File getCSVOutputDir();
}
} }

@ -28,6 +28,8 @@ import javax.inject.*;
public class ShowHabitBehavior public class ShowHabitBehavior
{ {
private HabitList habitList;
@NonNull @NonNull
private final Habit habit; private final Habit habit;
@ -38,10 +40,12 @@ public class ShowHabitBehavior
private Screen screen; private Screen screen;
@Inject @Inject
public ShowHabitBehavior(@NonNull CommandRunner commandRunner, public ShowHabitBehavior(@NonNull HabitList habitList,
@NonNull CommandRunner commandRunner,
@NonNull Habit habit, @NonNull Habit habit,
@NonNull Screen screen) @NonNull Screen screen)
{ {
this.habitList = habitList;
this.habit = habit; this.habit = habit;
this.commandRunner = commandRunner; this.commandRunner = commandRunner;
this.screen = screen; this.screen = screen;
@ -54,8 +58,8 @@ public class ShowHabitBehavior
public void onToggleCheckmark(long timestamp) public void onToggleCheckmark(long timestamp)
{ {
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp), commandRunner.execute(
null); new ToggleRepetitionCommand(habitList, habit, timestamp), null);
} }
public interface Screen public interface Screen

@ -29,15 +29,19 @@ import javax.inject.*;
public class WidgetBehavior public class WidgetBehavior
{ {
private HabitList habitList;
@NonNull @NonNull
private final CommandRunner commandRunner; private final CommandRunner commandRunner;
private NotificationTray notificationTray; private NotificationTray notificationTray;
@Inject @Inject
public WidgetBehavior(@NonNull CommandRunner commandRunner, public WidgetBehavior(@NonNull HabitList habitList,
@NonNull CommandRunner commandRunner,
@NonNull NotificationTray notificationTray) @NonNull NotificationTray notificationTray)
{ {
this.habitList = habitList;
this.commandRunner = commandRunner; this.commandRunner = commandRunner;
this.notificationTray = notificationTray; this.notificationTray = notificationTray;
} }
@ -64,7 +68,8 @@ public class WidgetBehavior
private void performToggle(@NonNull Habit habit, long timestamp) private void performToggle(@NonNull Habit habit, long timestamp)
{ {
commandRunner.execute(new ToggleRepetitionCommand(habit, timestamp), commandRunner.execute(
new ToggleRepetitionCommand(habitList, habit, timestamp),
habit.getId()); habit.getId());
} }
} }

@ -140,7 +140,7 @@ public class CommandParserTest extends BaseUnitTest
public void testDecodeToggleCommand() throws JSONException public void testDecodeToggleCommand() throws JSONException
{ {
ToggleRepetitionCommand original, decoded; ToggleRepetitionCommand original, decoded;
original = new ToggleRepetitionCommand(habit, 1000); original = new ToggleRepetitionCommand(habitList, habit, 1000);
decoded = (ToggleRepetitionCommand) parser.parse(original.toJson()); decoded = (ToggleRepetitionCommand) parser.parse(original.toJson());
MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId())); MatcherAssert.assertThat(decoded.getId(), equalTo(original.getId()));

@ -45,7 +45,7 @@ public class ToggleRepetitionCommandTest extends BaseUnitTest
habitList.add(habit); habitList.add(habit);
today = DateUtils.getStartOfToday(); today = DateUtils.getStartOfToday();
command = new ToggleRepetitionCommand(habit, today); command = new ToggleRepetitionCommand(habitList, habit, today);
} }
@Test @Test

@ -25,6 +25,7 @@ import org.junit.*;
import java.util.*; import java.util.*;
import static junit.framework.TestCase.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
@ -70,11 +71,6 @@ public class StreakListTest extends BaseUnitTest
public void testFindBeginning_withLongHistory() public void testFindBeginning_withLongHistory()
{ {
streaks.rebuild(); streaks.rebuild();
assertThat(streaks.findBeginning(), equalTo(today - day));
streaks.invalidateNewerThan(today - 20 * day);
assertThat(streaks.findBeginning(), equalTo(today - 28 * day));
streaks.invalidateNewerThan(0); streaks.invalidateNewerThan(0);
assertThat(streaks.findBeginning(), equalTo(today - 120 * day)); assertThat(streaks.findBeginning(), equalTo(today - 120 * day));
} }
@ -111,20 +107,6 @@ public class StreakListTest extends BaseUnitTest
assertThat(best.get(1).getLength(), equalTo(6L)); assertThat(best.get(1).getLength(), equalTo(6L));
} }
@Test
public void testGetNewestComputed() throws Exception
{
Streak s = streaks.getNewestComputed();
assertThat(s.getEnd(), equalTo(today));
assertThat(s.getStart(), equalTo(today - day));
streaks.invalidateNewerThan(today - 8 * day);
s = streaks.getNewestComputed();
assertThat(s.getEnd(), equalTo(today - 12 * day));
assertThat(s.getStart(), equalTo(today - 12 * day));
}
@Test @Test
public void testInvalidateNewer() public void testInvalidateNewer()
{ {
@ -135,6 +117,6 @@ public class StreakListTest extends BaseUnitTest
verify(listener).onModelChange(); verify(listener).onModelChange();
s = streaks.getNewestComputed(); s = streaks.getNewestComputed();
assertThat(s.getEnd(), equalTo(today - 12 * day)); assertNull(s);
} }
} }

@ -168,4 +168,19 @@ public class SQLiteHabitListTest extends BaseUnitTest
h2.setId(1000L); h2.setId(1000L);
assertThat(habitList.indexOf(h2), equalTo(-1)); assertThat(habitList.indexOf(h2), equalTo(-1));
} }
@Test
public void testRemove() throws Exception
{
Habit h = habitList.getByPosition(2);
habitList.remove(h);
assertThat(habitList.indexOf(h), equalTo(-1));
HabitRecord rec = repository.find(2L);
assertNull(rec);
rec = repository.find(3L);
assertNotNull(rec);
assertThat(rec.position, equalTo(2));
}
} }

@ -86,7 +86,7 @@ public class HabitCardListCacheTest extends BaseUnitTest
{ {
Habit h2 = habitList.getByPosition(2); Habit h2 = habitList.getByPosition(2);
long today = DateUtils.getStartOfToday(); long today = DateUtils.getStartOfToday();
commandRunner.execute(new ToggleRepetitionCommand(h2, today), commandRunner.execute(new ToggleRepetitionCommand(habitList, h2, today),
h2.getId()); h2.getId());
verify(listener).onItemChanged(2); verify(listener).onItemChanged(2);

Loading…
Cancel
Save