Merge pull request #1 from iSoron/dev

Update local code
pull/216/head
Blue Padge 9 years ago committed by GitHub
commit 6c4f37fd44

@ -63,6 +63,8 @@ public class BaseAndroidTest
protected AndroidTestComponent component; protected AndroidTestComponent component;
protected ModelFactory modelFactory;
@Before @Before
public void setUp() public void setUp()
{ {
@ -89,7 +91,7 @@ public class BaseAndroidTest
taskRunner = component.getTaskRunner(); taskRunner = component.getTaskRunner();
logger = component.getHabitsLogger(); logger = component.getHabitsLogger();
ModelFactory modelFactory = component.getModelFactory(); modelFactory = component.getModelFactory();
fixtures = new HabitFixtures(modelFactory, habitList); fixtures = new HabitFixtures(modelFactory, habitList);
latch = new CountDownLatch(1); latch = new CountDownLatch(1);

@ -39,12 +39,18 @@ public class HabitFixtures
} }
public Habit createEmptyHabit() public Habit createEmptyHabit()
{
return createEmptyHabit(null);
}
public Habit createEmptyHabit(Long id)
{ {
Habit habit = modelFactory.buildHabit(); Habit habit = modelFactory.buildHabit();
habit.setName("Meditate"); habit.setName("Meditate");
habit.setDescription("Did you meditate this morning?"); habit.setDescription("Did you meditate this morning?");
habit.setColor(3); habit.setColor(3);
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habit.setId(id);
habitList.add(habit); habitList.add(habit);
return habit; return habit;
} }

@ -0,0 +1,240 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.models;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import org.hamcrest.*;
import org.isoron.uhabits.*;
import org.junit.*;
import org.junit.runner.*;
import java.io.*;
import java.util.*;
import static junit.framework.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.isoron.uhabits.models.HabitList.Order.*;
@SuppressWarnings("JavaDoc")
@RunWith(AndroidJUnit4.class)
@MediumTest
public class HabitListTest extends BaseAndroidTest
{
private ArrayList<Habit> habitsArray;
private HabitList activeHabits;
private HabitList reminderHabits;
@Override
public void setUp()
{
super.setUp();
habitList.removeAll();
habitsArray = new ArrayList<>();
for (int i = 0; i < 10; i++)
{
Habit habit = fixtures.createEmptyHabit((long) i);
habitsArray.add(habit);
if (i % 3 == 0)
habit.setReminder(new Reminder(8, 30, WeekdayList.EVERY_DAY));
habitList.update(habit);
}
habitsArray.get(0).setArchived(true);
habitsArray.get(1).setArchived(true);
habitsArray.get(4).setArchived(true);
habitsArray.get(7).setArchived(true);
activeHabits = habitList.getFiltered(new HabitMatcherBuilder().build());
reminderHabits = habitList.getFiltered(new HabitMatcherBuilder()
.setArchivedAllowed(true)
.setReminderRequired(true)
.build());
}
@Test
public void test_size()
{
assertThat(habitList.size(), equalTo(10));
}
@Test
public void test_countActive()
{
assertThat(activeHabits.size(), equalTo(6));
}
@Test
public void test_getByPosition()
{
assertThat(habitList.getByPosition(0), equalTo(habitsArray.get(0)));
assertThat(habitList.getByPosition(3), equalTo(habitsArray.get(3)));
assertThat(habitList.getByPosition(9), equalTo(habitsArray.get(9)));
assertThat(activeHabits.getByPosition(0), equalTo(habitsArray.get(2)));
}
@Test
public void test_getHabitsWithReminder()
{
assertThat(reminderHabits.size(), equalTo(4));
assertThat(reminderHabits.getByPosition(1),
equalTo(habitsArray.get(3)));
}
@Test
public void test_get_withInvalidId()
{
assertThat(habitList.getById(100L), is(nullValue()));
}
@Test
public void test_get_withValidId()
{
Habit habit1 = habitsArray.get(0);
Habit habit2 = habitList.getById(habit1.getId());
assertThat(habit1, equalTo(habit2));
}
@Test
public void test_reorder()
{
int operations[][] = {
{ 5, 2 }, { 3, 7 }, { 4, 4 }, { 3, 2 }
};
int expectedPosition[][] = {
{ 0, 1, 3, 4, 5, 2, 6, 7, 8, 9 },
{ 0, 1, 7, 3, 4, 2, 5, 6, 8, 9 },
{ 0, 1, 7, 3, 4, 2, 5, 6, 8, 9 },
{ 0, 1, 7, 2, 4, 3, 5, 6, 8, 9 },
};
for (int i = 0; i < operations.length; i++)
{
int from = operations[i][0];
int to = operations[i][1];
Habit fromHabit = habitList.getByPosition(from);
Habit toHabit = habitList.getByPosition(to);
habitList.reorder(fromHabit, toHabit);
int actualPositions[] = new int[10];
for (int j = 0; j < 10; j++)
{
Habit h = habitList.getById(j);
assertNotNull(h);
actualPositions[j] = habitList.indexOf(h);
}
assertThat(actualPositions, equalTo(expectedPosition[i]));
}
}
@Test
public void test_writeCSV() throws IOException
{
habitList.removeAll();
Habit h1 = fixtures.createEmptyHabit();
h1.setName("Meditate");
h1.setDescription("Did you meditate this morning?");
h1.setFrequency(Frequency.DAILY);
h1.setColor(3);
Habit h2 = fixtures.createEmptyHabit();
h2.setName("Wake up early");
h2.setDescription("Did you wake up before 6am?");
h2.setFrequency(new Frequency(2, 3));
h2.setColor(5);
habitList.update(h1);
habitList.update(h2);
String expectedCSV =
"Position,Name,Description,NumRepetitions,Interval,Color\n" +
"001,Meditate,Did you meditate this morning?,1,1,#AFB42B\n" +
"002,Wake up early,Did you wake up before 6am?,2,3,#00897B\n";
StringWriter writer = new StringWriter();
habitList.writeCSV(writer);
MatcherAssert.assertThat(writer.toString(), equalTo(expectedCSV));
}
@Test
public void test_ordering()
{
habitList.removeAll();
Habit h3 = fixtures.createEmptyHabit();
h3.setName("C Habit");
h3.setColor(0);
habitList.update(h3);
Habit h1 = fixtures.createEmptyHabit();
h1.setName("A Habit");
h1.setColor(2);
habitList.update(h1);
Habit h4 = fixtures.createEmptyHabit();
h4.setName("D Habit");
h4.setColor(1);
habitList.update(h4);
Habit h2 = fixtures.createEmptyHabit();
h2.setName("B Habit");
h2.setColor(2);
habitList.update(h2);
habitList.setOrder(BY_POSITION);
assertThat(habitList.getByPosition(0), equalTo(h3));
assertThat(habitList.getByPosition(1), equalTo(h1));
assertThat(habitList.getByPosition(2), equalTo(h4));
assertThat(habitList.getByPosition(3), equalTo(h2));
habitList.setOrder(BY_NAME);
assertThat(habitList.getByPosition(0), equalTo(h1));
assertThat(habitList.getByPosition(1), equalTo(h2));
assertThat(habitList.getByPosition(2), equalTo(h3));
assertThat(habitList.getByPosition(3), equalTo(h4));
habitList.remove(h1);
habitList.add(h1);
assertThat(habitList.getByPosition(0), equalTo(h1));
habitList.setOrder(BY_COLOR);
assertThat(habitList.getByPosition(0), equalTo(h3));
assertThat(habitList.getByPosition(1), equalTo(h4));
assertThat(habitList.getByPosition(2), equalTo(h1));
assertThat(habitList.getByPosition(3), equalTo(h2));
}
}

@ -125,17 +125,6 @@ public class SQLiteHabitListTest extends BaseAndroidTest
assertThat(habits.get(3).getName(), equalTo("habit 3")); assertThat(habits.get(3).getName(), equalTo("habit 3"));
} }
// @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 @Test
public void testGetById() public void testGetById()
{ {
@ -178,45 +167,6 @@ public class SQLiteHabitListTest extends BaseAndroidTest
assertThat(habitList.indexOf(h2), equalTo(-1)); assertThat(habitList.indexOf(h2), equalTo(-1));
} }
@Test
public void test_reorder()
{
// Same as HabitListTest.java
// TODO: remove duplication
int operations[][] = {
{5, 2}, {3, 7}, {4, 4}, {3, 2}
};
int expectedPosition[][] = {
{0, 1, 3, 4, 5, 2, 6, 7, 8, 9},
{0, 1, 7, 3, 4, 2, 5, 6, 8, 9},
{0, 1, 7, 3, 4, 2, 5, 6, 8, 9},
{0, 1, 7, 2, 4, 3, 5, 6, 8, 9},
};
for (int i = 0; i < operations.length; i++)
{
int from = operations[i][0];
int to = operations[i][1];
Habit fromHabit = habitList.getByPosition(from);
Habit toHabit = habitList.getByPosition(to);
habitList.reorder(fromHabit, toHabit);
int actualPositions[] = new int[10];
for (int j = 0; j < 10; j++)
{
Habit h = habitList.getById(j);
assertNotNull(h);
actualPositions[j] = habitList.indexOf(h);
}
assertThat(actualPositions, equalTo(expectedPosition[i]));
}
}
private HabitRecord getRecord(long id) private HabitRecord getRecord(long id)
{ {
return new Select() return new Select()

@ -112,6 +112,22 @@ public class ListHabitsMenu extends BaseMenu
invalidate(); invalidate();
return true; return true;
case R.id.actionSortColor:
adapter.setOrder(HabitList.Order.BY_COLOR);
return true;
case R.id.actionSortManual:
adapter.setOrder(HabitList.Order.BY_POSITION);
return true;
case R.id.actionSortName:
adapter.setOrder(HabitList.Order.BY_NAME);
return true;
case R.id.actionSortScore:
adapter.setOrder(HabitList.Order.BY_SCORE);
return true;
default: default:
return false; return false;
} }

@ -27,6 +27,7 @@ import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.activities.habits.list.*; import org.isoron.uhabits.activities.habits.list.*;
import org.isoron.uhabits.activities.habits.list.views.*; import org.isoron.uhabits.activities.habits.list.views.*;
import org.isoron.uhabits.models.*; import org.isoron.uhabits.models.*;
import org.isoron.uhabits.preferences.*;
import java.util.*; import java.util.*;
@ -55,15 +56,21 @@ public class HabitCardListAdapter
@NonNull @NonNull
private final HabitCardListCache cache; private final HabitCardListCache cache;
@NonNull
private Preferences preferences;
@Inject @Inject
public HabitCardListAdapter(@NonNull HabitCardListCache cache) public HabitCardListAdapter(@NonNull HabitCardListCache cache,
@NonNull Preferences preferences)
{ {
this.preferences = preferences;
this.selected = new LinkedList<>(); this.selected = new LinkedList<>();
this.observable = new ModelObservable(); this.observable = new ModelObservable();
this.cache = cache; this.cache = cache;
cache.setListener(this); cache.setListener(this);
cache.setCheckmarkCount(ListHabitsRootView.MAX_CHECKMARK_COUNT); cache.setCheckmarkCount(ListHabitsRootView.MAX_CHECKMARK_COUNT);
cache.setOrder(preferences.getDefaultOrder());
setHasStableIds(true); setHasStableIds(true);
} }
@ -130,6 +137,11 @@ public class HabitCardListAdapter
return selected.isEmpty(); return selected.isEmpty();
} }
public boolean isSortable()
{
return cache.getOrder() == HabitList.Order.BY_POSITION;
}
/** /**
* Notify the adapter that it has been attached to a ListView. * Notify the adapter that it has been attached to a ListView.
*/ */
@ -260,6 +272,12 @@ public class HabitCardListAdapter
this.listView = listView; this.listView = listView;
} }
public void setOrder(HabitList.Order order)
{
cache.setOrder(order);
preferences.setDefaultOrder(order);
}
/** /**
* Selects or deselects the item at a given position. * Selects or deselects the item at a given position.
* *

@ -107,6 +107,11 @@ public class HabitCardListCache implements CommandRunner.Listener
return data.habits.size(); return data.habits.size();
} }
public HabitList.Order getOrder()
{
return filteredHabits.getOrder();
}
public int getScore(long habitId) public int getScore(long habitId)
{ {
return data.scores.get(habitId); return data.scores.get(habitId);
@ -180,6 +185,13 @@ public class HabitCardListCache implements CommandRunner.Listener
this.listener = listener; this.listener = listener;
} }
public void setOrder(HabitList.Order order)
{
allHabits.setOrder(order);
filteredHabits.setOrder(order);
refreshAllHabits();
}
/** /**
* Interface definition for a callback to be invoked when the data on the * Interface definition for a callback to be invoked when the data on the
* cache has been modified. * cache has been modified.

@ -168,7 +168,7 @@ public class HabitCardListView extends RecyclerView
{ {
int position = holder.getAdapterPosition(); int position = holder.getAdapterPosition();
if (controller != null) controller.onItemLongClick(position); if (controller != null) controller.onItemLongClick(position);
touchHelper.startDrag(holder); if(adapter.isSortable()) touchHelper.startDrag(holder);
} }
@Override @Override

@ -48,9 +48,7 @@ public abstract class HabitList implements Iterable<Habit>
public HabitList() public HabitList()
{ {
observable = new ModelObservable(); observable = new ModelObservable();
filter = new HabitMatcherBuilder() filter = new HabitMatcherBuilder().setArchivedAllowed(true).build();
.setArchivedAllowed(true)
.build();
} }
protected HabitList(@NonNull HabitMatcher filter) protected HabitList(@NonNull HabitMatcher filter)
@ -106,6 +104,15 @@ public abstract class HabitList implements Iterable<Habit>
return observable; return observable;
} }
public abstract Order getOrder();
/**
* Changes the order of the elements on the list.
*
* @param order the new order criterea
*/
public abstract void setOrder(@NonNull Order order);
/** /**
* Returns the index of the given habit in the list, or -1 if the list does * Returns the index of the given habit in the list, or -1 if the list does
* not contain the habit. * not contain the habit.
@ -149,7 +156,7 @@ public abstract class HabitList implements Iterable<Habit>
public void repair() public void repair()
{ {
for(Habit h : this) for (Habit h : this)
{ {
h.getCheckmarks().invalidateNewerThan(0); h.getCheckmarks().invalidateNewerThan(0);
h.getStreaks().invalidateNewerThan(0); h.getStreaks().invalidateNewerThan(0);
@ -228,4 +235,12 @@ public abstract class HabitList implements Iterable<Habit>
csv.close(); csv.close();
} }
public enum Order
{
BY_NAME,
BY_COLOR,
BY_SCORE,
BY_POSITION
}
} }

@ -25,6 +25,8 @@ import org.isoron.uhabits.models.*;
import java.util.*; import java.util.*;
import static org.isoron.uhabits.models.HabitList.Order.*;
/** /**
* In-memory implementation of {@link HabitList}. * In-memory implementation of {@link HabitList}.
*/ */
@ -33,16 +35,23 @@ public class MemoryHabitList extends HabitList
@NonNull @NonNull
private LinkedList<Habit> list; private LinkedList<Habit> list;
private Comparator<Habit> comparator = null;
@NonNull
private Order order;
public MemoryHabitList() public MemoryHabitList()
{ {
super(); super();
list = new LinkedList<>(); list = new LinkedList<>();
order = Order.BY_POSITION;
} }
protected MemoryHabitList(@NonNull HabitMatcher matcher) protected MemoryHabitList(@NonNull HabitMatcher matcher)
{ {
super(matcher); super(matcher);
list = new LinkedList<>(); list = new LinkedList<>();
order = Order.BY_POSITION;
} }
@Override @Override
@ -57,6 +66,7 @@ public class MemoryHabitList extends HabitList
if (id == null) habit.setId((long) list.size()); if (id == null) habit.setId((long) list.size());
list.addLast(habit); list.addLast(habit);
resort();
} }
@Override @Override
@ -82,10 +92,17 @@ public class MemoryHabitList extends HabitList
public HabitList getFiltered(HabitMatcher matcher) public HabitList getFiltered(HabitMatcher matcher)
{ {
MemoryHabitList habits = new MemoryHabitList(matcher); MemoryHabitList habits = new MemoryHabitList(matcher);
for(Habit h : this) if (matcher.matches(h)) habits.add(h); habits.comparator = comparator;
for (Habit h : this) if (matcher.matches(h)) habits.add(h);
return habits; return habits;
} }
@Override
public Order getOrder()
{
return order;
}
@Override @Override
public int indexOf(@NonNull Habit h) public int indexOf(@NonNull Habit h)
{ {
@ -112,6 +129,14 @@ public class MemoryHabitList extends HabitList
list.add(toPos, from); list.add(toPos, from);
} }
@Override
public void setOrder(@NonNull Order order)
{
this.order = order;
this.comparator = getComparatorByOrder(order);
resort();
}
@Override @Override
public int size() public int size()
{ {
@ -123,4 +148,34 @@ public class MemoryHabitList extends HabitList
{ {
// NOP // NOP
} }
private Comparator<Habit> getComparatorByOrder(Order order)
{
Comparator<Habit> nameComparator =
(h1, h2) -> h1.getName().compareTo(h2.getName());
Comparator<Habit> colorComparator = (h1, h2) -> {
Integer c1 = h1.getColor();
Integer c2 = h2.getColor();
if (c1.equals(c2)) return nameComparator.compare(h1, h2);
else return c1.compareTo(c2);
};
Comparator<Habit> scoreComparator = (h1, h2) -> {
int s1 = h1.getScores().getTodayValue();
int s2 = h2.getScores().getTodayValue();
return Integer.compare(s2, s1);
};
if (order == BY_POSITION) return null;
if (order == BY_NAME) return nameComparator;
if (order == BY_COLOR) return colorComparator;
if (order == BY_SCORE) return scoreComparator;
throw new IllegalStateException();
}
private void resort()
{
if (comparator != null) Collections.sort(list, comparator);
}
} }

@ -39,10 +39,15 @@ public class SQLiteHabitList extends HabitList
private static SQLiteHabitList instance; private static SQLiteHabitList instance;
@NonNull
private final SQLiteUtils<HabitRecord> sqlite; private final SQLiteUtils<HabitRecord> sqlite;
@NonNull
private final ModelFactory modelFactory; private final ModelFactory modelFactory;
@NonNull
private Order order;
public SQLiteHabitList(@NonNull ModelFactory modelFactory) public SQLiteHabitList(@NonNull ModelFactory modelFactory)
{ {
super(); super();
@ -50,16 +55,19 @@ public class SQLiteHabitList extends HabitList
if (cache == null) cache = new HashMap<>(); if (cache == null) cache = new HashMap<>();
sqlite = new SQLiteUtils<>(HabitRecord.class); sqlite = new SQLiteUtils<>(HabitRecord.class);
order = Order.BY_POSITION;
} }
protected SQLiteHabitList(@NonNull ModelFactory modelFactory, protected SQLiteHabitList(@NonNull ModelFactory modelFactory,
@NonNull HabitMatcher filter) @NonNull HabitMatcher filter,
@NonNull Order order)
{ {
super(filter); super(filter);
this.modelFactory = modelFactory; this.modelFactory = modelFactory;
if (cache == null) cache = new HashMap<>(); if (cache == null) cache = new HashMap<>();
sqlite = new SQLiteUtils<>(HabitRecord.class); sqlite = new SQLiteUtils<>(HabitRecord.class);
this.order = order;
} }
public static SQLiteHabitList getInstance( public static SQLiteHabitList getInstance(
@ -118,7 +126,20 @@ public class SQLiteHabitList extends HabitList
@Override @Override
public HabitList getFiltered(HabitMatcher filter) public HabitList getFiltered(HabitMatcher filter)
{ {
return new SQLiteHabitList(modelFactory, filter); return new SQLiteHabitList(modelFactory, filter, order);
}
@Override
@NonNull
public Order getOrder()
{
return order;
}
@Override
public void setOrder(@NonNull Order order)
{
this.order = order;
} }
@Override @Override
@ -214,6 +235,13 @@ public class SQLiteHabitList extends HabitList
getObservable().notifyListeners(); getObservable().notifyListeners();
} }
@Override
public void repair()
{
super.repair();
rebuildOrder();
}
@Override @Override
public int size() public int size()
{ {
@ -249,12 +277,38 @@ public class SQLiteHabitList extends HabitList
habits.add(habit); habits.add(habit);
} }
if(order == Order.BY_SCORE)
{
Collections.sort(habits, (lhs, rhs) -> {
int s1 = lhs.getScores().getTodayValue();
int s2 = rhs.getScores().getTodayValue();
return Integer.compare(s2, s1);
});
}
return habits; return habits;
} }
private void appendOrderBy(StringBuilder query) private void appendOrderBy(StringBuilder query)
{ {
query.append("order by position "); switch (order)
{
case BY_POSITION:
query.append("order by position ");
break;
case BY_NAME:
case BY_SCORE:
query.append("order by name ");
break;
case BY_COLOR:
query.append("order by color, name ");
break;
default:
throw new IllegalStateException();
}
} }
private void appendSelect(StringBuilder query) private void appendSelect(StringBuilder query)
@ -282,11 +336,4 @@ public class SQLiteHabitList extends HabitList
appendOrderBy(query); appendOrderBy(query);
return query.toString(); return query.toString();
} }
@Override
public void repair()
{
super.repair();
rebuildOrder();
}
} }

@ -24,6 +24,7 @@ import android.preference.*;
import org.isoron.uhabits.*; import org.isoron.uhabits.*;
import org.isoron.uhabits.activities.*; import org.isoron.uhabits.activities.*;
import org.isoron.uhabits.models.*;
import java.util.*; import java.util.*;
@ -61,6 +62,21 @@ public class Preferences
return prefs.getInt("pref_default_habit_palette_color", fallbackColor); return prefs.getInt("pref_default_habit_palette_color", fallbackColor);
} }
public HabitList.Order getDefaultOrder()
{
String name = prefs.getString("pref_default_order", "BY_POSITION");
try
{
return HabitList.Order.valueOf(name);
}
catch (IllegalArgumentException e)
{
setDefaultOrder(HabitList.Order.BY_POSITION);
return HabitList.Order.BY_POSITION;
}
}
public int getDefaultScoreSpinnerPosition() public int getDefaultScoreSpinnerPosition()
{ {
int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1); int defaultScoreInterval = prefs.getInt("pref_score_view_interval", 1);
@ -69,6 +85,11 @@ public class Preferences
return defaultScoreInterval; return defaultScoreInterval;
} }
public void setDefaultOrder(HabitList.Order order)
{
prefs.edit().putString("pref_default_order", order.name()).apply();
}
public void setDefaultScoreSpinnerPosition(int position) public void setDefaultScoreSpinnerPosition(int position)
{ {
prefs.edit().putInt("pref_score_view_interval", position).apply(); prefs.edit().putInt("pref_score_view_interval", position).apply();

@ -79,7 +79,7 @@
<EditText <EditText
android:id="@+id/tvFreqNum" android:id="@+id/tvFreqNum"
style="@style/dialogFormInputSmallNumber"/> style="@style/dialogFormInputLargeNumber"/>
<TextView <TextView
android:id="@+id/textView3" android:id="@+id/textView3"
@ -89,7 +89,7 @@
<EditText <EditText
android:id="@+id/tvFreqDen" android:id="@+id/tvFreqDen"
style="@style/dialogFormInputSmallNumber"/> style="@style/dialogFormInputLargeNumber"/>
<TextView <TextView
android:id="@+id/textView5" android:id="@+id/textView5"

@ -45,6 +45,26 @@
android:checkable="true" android:checkable="true"
android:enabled="true" android:enabled="true"
android:title="@string/hide_completed"/> android:title="@string/hide_completed"/>
<item android:title="@string/sort">
<menu>
<item
android:id="@+id/actionSortManual"
android:title="@string/manually"/>
<item
android:id="@+id/actionSortName"
android:title="@string/by_name"/>
<item
android:id="@+id/actionSortColor"
android:title="@string/by_color"/>
<item
android:id="@+id/actionSortScore"
android:title="@string/by_score"/>
</menu>
</item>
</menu> </menu>
</item> </item>
@ -73,5 +93,4 @@
android:orderInCategory="100" android:orderInCategory="100"
android:title="@string/about" android:title="@string/about"
app:showAsAction="never"/> app:showAsAction="never"/>
</menu> </menu>

@ -196,4 +196,10 @@
<string name="toggle">Toggle</string> <string name="toggle">Toggle</string>
<string name="action">Action</string> <string name="action">Action</string>
<string name="habit">Habit</string> <string name="habit">Habit</string>
<string name="sort">Sort</string>
<string name="manually">Manually</string>
<string name="by_name">By name</string>
<string name="by_color">By color</string>
<string name="by_score">By score</string>
</resources> </resources>

@ -34,13 +34,13 @@
<item name="android:background">?android:attr/selectableItemBackground</item> <item name="android:background">?android:attr/selectableItemBackground</item>
</style> </style>
<style name="dialogFormInputSmallNumber" parent="dialogFormInput"> <style name="dialogFormInputLargeNumber" parent="dialogFormInput">
<item name="android:layout_width">wrap_content</item> <item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item> <item name="android:layout_height">wrap_content</item>
<item name="android:ems">2</item> <item name="android:ems">2</item>
<item name="android:gravity">center</item> <item name="android:gravity">center</item>
<item name="android:inputType">number</item> <item name="android:inputType">number</item>
<item name="android:maxLength">2</item> <item name="android:maxLength">3</item>
</style> </style>
<style name="dialogFormText"> <style name="dialogFormText">

@ -30,6 +30,7 @@ import static junit.framework.Assert.*;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsEqual.equalTo;
import static org.isoron.uhabits.models.HabitList.Order.*;
@SuppressWarnings("JavaDoc") @SuppressWarnings("JavaDoc")
public class HabitListTest extends BaseUnitTest public class HabitListTest extends BaseUnitTest
@ -70,7 +71,7 @@ public class HabitListTest extends BaseUnitTest
} }
@Test @Test
public void testSize() public void test_size()
{ {
assertThat(habitList.size(), equalTo(10)); assertThat(habitList.size(), equalTo(10));
} }
@ -179,4 +180,52 @@ public class HabitListTest extends BaseUnitTest
MatcherAssert.assertThat(writer.toString(), equalTo(expectedCSV)); MatcherAssert.assertThat(writer.toString(), equalTo(expectedCSV));
} }
@Test
public void test_ordering()
{
HabitList list = modelFactory.buildHabitList();
Habit h1 = fixtures.createEmptyHabit();
h1.setName("A Habit");
h1.setColor(2);
Habit h2 = fixtures.createEmptyHabit();
h2.setName("B Habit");
h2.setColor(2);
Habit h3 = fixtures.createEmptyHabit();
h3.setName("C Habit");
h3.setColor(0);
Habit h4 = fixtures.createEmptyHabit();
h4.setName("D Habit");
h4.setColor(1);
list.add(h3);
list.add(h1);
list.add(h4);
list.add(h2);
list.setOrder(BY_POSITION);
assertThat(list.getByPosition(0), equalTo(h3));
assertThat(list.getByPosition(1), equalTo(h1));
assertThat(list.getByPosition(2), equalTo(h4));
assertThat(list.getByPosition(3), equalTo(h2));
list.setOrder(BY_NAME);
assertThat(list.getByPosition(0), equalTo(h1));
assertThat(list.getByPosition(1), equalTo(h2));
assertThat(list.getByPosition(2), equalTo(h3));
assertThat(list.getByPosition(3), equalTo(h4));
list.remove(h1);
list.add(h1);
assertThat(list.getByPosition(0), equalTo(h1));
list.setOrder(BY_COLOR);
assertThat(list.getByPosition(0), equalTo(h3));
assertThat(list.getByPosition(1), equalTo(h4));
assertThat(list.getByPosition(2), equalTo(h1));
assertThat(list.getByPosition(3), equalTo(h2));
}
} }
Loading…
Cancel
Save