Skip to content

Commit

Permalink
Initial implementation of filters
Browse files Browse the repository at this point in the history
  • Loading branch information
iSoron committed Jun 29, 2016
1 parent 31fdae1 commit 922b234
Show file tree
Hide file tree
Showing 48 changed files with 803 additions and 599 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Habit createShortHabit()

public void purgeHabits(HabitList habitList)
{
for (Habit h : habitList.getAll(true))
for (Habit h : habitList)
habitList.remove(h);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,22 @@

package org.isoron.uhabits.io;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import org.isoron.uhabits.BaseAndroidTest;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.utils.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static junit.framework.Assert.assertTrue;
import android.content.*;
import android.support.test.*;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;

import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;

import java.io.*;
import java.util.*;
import java.util.zip.*;

import static junit.framework.Assert.*;

@RunWith(AndroidJUnit4.class)
@SmallTest
Expand All @@ -63,9 +58,11 @@ public void setUp()
@Test
public void testExportCSV() throws IOException
{
List<Habit> habits = habitList.getAll(true);
List<Habit> selected = new LinkedList<>();
for (Habit h : habitList) selected.add(h);

HabitsCSVExporter exporter = new HabitsCSVExporter(habits, baseDir);
HabitsCSVExporter exporter =
new HabitsCSVExporter(habitList, selected, baseDir);
String filename = exporter.writeArchive();
assertAbsolutePathExists(filename);

Expand Down
25 changes: 11 additions & 14 deletions app/src/androidTest/java/org/isoron/uhabits/io/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class ImportTest extends BaseAndroidTest

private Context context;

@Override
@Before
public void setUp()
{
Expand All @@ -61,10 +62,9 @@ public void testHabitBullCSV() throws IOException
{
importFromFile("habitbull.csv");

List<Habit> habits = habitList.getAll(true);
assertThat(habits.size(), equalTo(4));
assertThat(habitList.size(), equalTo(4));

Habit habit = habits.get(0);
Habit habit = habitList.getByPosition(0);
assertThat(habit.getName(), equalTo("Breed dragons"));
assertThat(habit.getDescription(), equalTo("with love and fire"));
assertThat(habit.getFrequency(), equalTo(Frequency.DAILY));
Expand All @@ -78,10 +78,9 @@ public void testLoopDB() throws IOException
{
importFromFile("loop.db");

List<Habit> habits = habitList.getAll(true);
assertThat(habits.size(), equalTo(9));
assertThat(habitList.size(), equalTo(9));

Habit habit = habits.get(0);
Habit habit = habitList.getByPosition(0);
assertThat(habit.getName(), equalTo("Wake up early"));
assertThat(habit.getFrequency(), equalTo(Frequency.THREE_TIMES_PER_WEEK));
assertTrue(containsRepetition(habit, 2016, 3, 14));
Expand All @@ -94,10 +93,9 @@ public void testRewireDB() throws IOException
{
importFromFile("rewire.db");

List<Habit> habits = habitList.getAll(true);
assertThat(habits.size(), equalTo(3));
assertThat(habitList.size(), equalTo(3));

Habit habit = habits.get(0);
Habit habit = habitList.getByPosition(0);
assertThat(habit.getName(), equalTo("Wake up early"));
assertThat(habit.getFrequency(),
equalTo(Frequency.THREE_TIMES_PER_WEEK));
Expand All @@ -107,7 +105,7 @@ public void testRewireDB() throws IOException
assertTrue(containsRepetition(habit, 2016, 1, 28));
assertFalse(containsRepetition(habit, 2016, 3, 10));

habit = habits.get(1);
habit = habitList.getByPosition(1);
assertThat(habit.getName(), equalTo("brush teeth"));
assertThat(habit.getFrequency(),
equalTo(Frequency.THREE_TIMES_PER_WEEK));
Expand All @@ -126,10 +124,9 @@ public void testTickmateDB() throws IOException
{
importFromFile("tickmate.db");

List<Habit> habits = habitList.getAll(true);
assertThat(habits.size(), equalTo(3));
assertThat(habitList.size(), equalTo(3));

Habit h = habits.get(0);
Habit h = habitList.getByPosition(0);
assertThat(h.getName(), equalTo("Vegan"));
assertTrue(containsRepetition(h, 2016, 1, 24));
assertTrue(containsRepetition(h, 2016, 2, 5));
Expand Down Expand Up @@ -158,7 +155,7 @@ private void importFromFile(String assetFilename) throws IOException
assertTrue(file.exists());
assertTrue(file.canRead());

GenericImporter importer = new GenericImporter();
GenericImporter importer = new GenericImporter(habitList);
assertThat(importer.canHandle(file), is(true));

importer.importHabitsFromFile(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ public class SQLiteHabitListTest extends BaseAndroidTest
@Rule
public ExpectedException exception = ExpectedException.none();

private SQLiteHabitList habitList;

@Override
public void setUp()
{
super.setUp();
this.habitList = (SQLiteHabitList) super.habitList;
fixtures.purgeHabits(habitList);

for (int i = 0; i < 10; i++)
Expand Down Expand Up @@ -105,35 +108,29 @@ public void testAdd_withoutId()
}

@Test
public void testCountActive()
{
assertThat(habitList.countActive(), equalTo(5));
}

@Test
public void testCountWithArchived()
public void testSize()
{
assertThat(habitList.countWithArchived(), equalTo(10));
assertThat(habitList.size(), equalTo(10));
}

@Test
public void testGetAll_withArchived()
{
List<Habit> habits = habitList.getAll(true);
List<Habit> habits = habitList.toList();
assertThat(habits.size(), equalTo(10));
assertThat(habits.get(3).getName(), equalTo("habit 3"));
}

@Test
public void testGetAll_withoutArchived()
{
List<Habit> habits = habitList.getAll(false);
assertThat(habits.size(), equalTo(5));
assertThat(habits.get(3).getName(), equalTo("habit 7"));

List<Habit> another = habitList.getAll(false);
assertThat(habits, equalTo(another));
}
// @Test
// public void testGetAll_withoutArchived()
// {
// List<Habit> habits = habitList.toList();
// assertThat(habits.size(), equalTo(5));
// assertThat(habits.get(3).getName(), equalTo("habit 7"));
//
// List<Habit> another = habitList.toList();
// assertThat(habits, equalTo(another));
// }

@Test
public void testGetById()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public void setUp()
public void testExportCSV() throws Throwable
{
fixtures.createShortHabit();
List<Habit> habits = habitList.getAll(true);
List<Habit> selected = new LinkedList<>();
for(Habit h : habitList) selected.add(h);

ExportCSVTask task = new ExportCSVTask(habits, null);
ExportCSVTask task = new ExportCSVTask(habitList, selected, null);
task.setListener(archiveFilename -> {
assertThat(archiveFilename, is(not(nullValue())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private ImportDataTask createTask(String assetFilename) throws IOException
{
File file = new File(String.format("%s/%s", baseDir.getPath(), assetFilename));
copyAssetToFile(assetFilename, file);
return new ImportDataTask(file, null);
return new ImportDataTask(habitList, file, null);
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/org/isoron/uhabits/HabitBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class HabitBroadcastReceiver extends BroadcastReceiver
"org.isoron.uhabits.ACTION_SNOOZE";

@Inject
HabitList habitList;
HabitList habits;

@Inject
CommandRunner commandRunner;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void onReceive(final Context context, Intent intent)
break;

case Intent.ACTION_BOOT_COMPLETED:
ReminderUtils.createReminderAlarms(context, habitList);
ReminderUtils.createReminderAlarms(context, habits);
break;
}
}
Expand All @@ -111,7 +111,7 @@ private void checkHabit(Context context, Intent intent)
Long timestamp = intent.getLongExtra("timestamp", today);

long habitId = ContentUris.parseId(data);
Habit habit = habitList.getById(habitId);
Habit habit = habits.getById(habitId);
if (habit != null)
{
ToggleRepetitionCommand command =
Expand Down Expand Up @@ -140,7 +140,7 @@ private boolean checkWeekday(Intent intent, Habit habit)
private void createNotification(final Context context, final Intent intent)
{
final Uri data = intent.getData();
final Habit habit = habitList.getById(ContentUris.parseId(data));
final Habit habit = habits.getById(ContentUris.parseId(data));
final Long timestamp =
intent.getLongExtra("timestamp", DateUtils.getStartOfToday());
final Long reminderTime =
Expand Down Expand Up @@ -223,7 +223,7 @@ protected void onPostExecute(Void aVoid)
private void createReminderAlarmsDelayed(final Context context)
{
new Handler().postDelayed(
() -> ReminderUtils.createReminderAlarms(context, habitList), 5000);
() -> ReminderUtils.createReminderAlarms(context, habits), 5000);
}

private void dismissAllHabits()
Expand All @@ -250,7 +250,7 @@ private void snoozeHabit(Context context, Intent intent)
Long.parseLong(prefs.getString("pref_snooze_interval", "15"));

long habitId = ContentUris.parseId(data);
Habit habit = habitList.getById(habitId);
Habit habit = habits.getById(habitId);
if (habit != null) ReminderUtils.createReminderAlarm(context, habit,
new Date().getTime() + delayMinutes * 60 * 1000);
dismissNotification(context, habitId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,32 @@

package org.isoron.uhabits.commands;

import org.isoron.uhabits.HabitsApplication;
import org.isoron.uhabits.R;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.models.HabitList;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;

import java.util.List;

import javax.inject.Inject;
import java.util.*;

/**
* Command to archive a list of habits.
*/
public class ArchiveHabitsCommand extends Command
{
@Inject
HabitList habitList;
private List<Habit> selectedHabits;

private List<Habit> habits;
private final HabitList habitList;

public ArchiveHabitsCommand(List<Habit> habits)
public ArchiveHabitsCommand(HabitList habitList, List<Habit> selectedHabits)
{
this.habitList = habitList;
HabitsApplication.getComponent().inject(this);
this.habits = habits;
this.selectedHabits = selectedHabits;
}

@Override
public void execute()
{
for(Habit h : habits) h.setArchived(true);
habitList.update(habits);
}

@Override
public void undo()
{
for(Habit h : habits) h.setArchived(false);
habitList.update(habits);
for (Habit h : selectedHabits) h.setArchived(true);
habitList.update(selectedHabits);
}

@Override
Expand All @@ -69,4 +58,11 @@ public Integer getUndoStringId()
{
return R.string.toast_habit_unarchived;
}

@Override
public void undo()
{
for (Habit h : selectedHabits) h.setArchived(false);
habitList.update(selectedHabits);
}
}
Loading

0 comments on commit 922b234

Please sign in to comment.