Remember previous sort method as a secondary ordering

pull/660/head
Quentin Hibon 5 years ago
parent 02bfa3952e
commit 7db6f600fb

@ -31,6 +31,11 @@ import static org.isoron.uhabits.core.utils.StringUtils.defaultToStringStyle;
* While repetitions simply record that the habit was performed at a given date,
* a checkmark provides more information, such as whether a repetition was
* expected at that day or not.
* </p>
* <p>
* Note that the priority comparator in
* {@link org.isoron.uhabits.core.models.memory.MemoryHabitList}
* relies on SKIP > YES_MANUAL > YES_AUTO > NO.
* <p>
* Checkmarks are computed automatically from the list of repetitions.
*/

@ -252,6 +252,6 @@ public abstract class HabitList implements Iterable<Habit>
BY_SCORE_DESC,
BY_PRIORITY_ASC,
BY_PRIORITY_DESC,
BY_POSITION;
BY_POSITION
}
}

@ -40,6 +40,9 @@ public class MemoryHabitList extends HabitList
@NonNull
private Order order = Order.BY_POSITION;
@NonNull
private Order previousOrder = Order.BY_POSITION;
@Nullable
private MemoryHabitList parent = null;
@ -113,14 +116,27 @@ public class MemoryHabitList extends HabitList
@Override
public synchronized void setOrder(@NonNull Order order)
{
this.previousOrder = this.order;
this.order = order;
this.comparator = getComparatorByOrder(order);
this.comparator = getComposedComparatorByOrder(order, this.previousOrder);
resort();
getObservable().notifyListeners();
}
private Comparator<Habit> getComparatorByOrder(Order order)
private Comparator<Habit> getComposedComparatorByOrder(Order firstOrder, Order secondOrder)
{
return (h1, h2) -> {
int firstResult = getComparatorByOrder(firstOrder).compare(h1, h2);
if (firstResult != 0 || secondOrder == null) {
return firstResult;
}
return getComparatorByOrder(secondOrder).compare(h1, h2);
};
}
private Comparator<Habit> getComparatorByOrder(Order order) {
Comparator<Habit> nameComparatorAsc =
(h1, h2) -> h1.getName().compareTo(h2.getName());

@ -128,10 +128,10 @@ public class ListHabitsMenuBehavior
}
public void onSortByPriority() {
if (adapter.getOrder() != HabitList.Order.BY_PRIORITY_DESC) {
adapter.setOrder(HabitList.Order.BY_PRIORITY_DESC);
} else {
if (adapter.getOrder() != HabitList.Order.BY_PRIORITY_ASC) {
adapter.setOrder(HabitList.Order.BY_PRIORITY_ASC);
} else {
adapter.setOrder(HabitList.Order.BY_PRIORITY_DESC);
}
}

@ -136,7 +136,7 @@ public class ListHabitsMenuBehaviorTest extends BaseUnitTest
{
behavior.onSortByPriority();
verify(adapter).setOrder(orderCaptor.capture());
assertThat(orderCaptor.getValue(), equalTo(BY_PRIORITY_DESC));
assertThat(orderCaptor.getValue(), equalTo(BY_PRIORITY_ASC));
}
@Test

Loading…
Cancel
Save