mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Sort by score
This commit is contained in:
@@ -124,6 +124,10 @@ public class ListHabitsMenu extends BaseMenu
|
|||||||
adapter.setOrder(HabitList.Order.BY_NAME);
|
adapter.setOrder(HabitList.Order.BY_NAME);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
case R.id.actionSortScore:
|
||||||
|
adapter.setOrder(HabitList.Order.BY_SCORE);
|
||||||
|
return true;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -240,6 +240,7 @@ public abstract class HabitList implements Iterable<Habit>
|
|||||||
{
|
{
|
||||||
BY_NAME,
|
BY_NAME,
|
||||||
BY_COLOR,
|
BY_COLOR,
|
||||||
|
BY_SCORE,
|
||||||
BY_POSITION
|
BY_POSITION
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,9 +161,16 @@ public class MemoryHabitList extends HabitList
|
|||||||
else return c1.compareTo(c2);
|
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_POSITION) return null;
|
||||||
if (order == BY_NAME) return nameComparator;
|
if (order == BY_NAME) return nameComparator;
|
||||||
if (order == BY_COLOR) return colorComparator;
|
if (order == BY_COLOR) return colorComparator;
|
||||||
|
if (order == BY_SCORE) return scoreComparator;
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -277,6 +277,15 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,6 +298,7 @@ public class SQLiteHabitList extends HabitList
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case BY_NAME:
|
case BY_NAME:
|
||||||
|
case BY_SCORE:
|
||||||
query.append("order by name ");
|
query.append("order by name ");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,10 @@
|
|||||||
<item
|
<item
|
||||||
android:id="@+id/actionSortColor"
|
android:id="@+id/actionSortColor"
|
||||||
android:title="@string/by_color"/>
|
android:title="@string/by_color"/>
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/actionSortScore"
|
||||||
|
android:title="@string/by_score"/>
|
||||||
</menu>
|
</menu>
|
||||||
</item>
|
</item>
|
||||||
</menu>
|
</menu>
|
||||||
|
|||||||
@@ -201,4 +201,5 @@
|
|||||||
<string name="manually">Manually</string>
|
<string name="manually">Manually</string>
|
||||||
<string name="by_name">By name</string>
|
<string name="by_name">By name</string>
|
||||||
<string name="by_color">By color</string>
|
<string name="by_color">By color</string>
|
||||||
|
<string name="by_score">By score</string>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user