Fix order by position

This commit is contained in:
2017-06-22 23:21:44 -04:00
parent ea640a8a17
commit 7872983064
6 changed files with 56 additions and 40 deletions

View File

@@ -343,6 +343,16 @@ public class Habit
return new HabitData(data);
}
public Integer getPosition()
{
return data.position;
}
public void setPosition(int newPosition)
{
data.position = newPosition;
}
public static class HabitData
{
@NonNull
@@ -370,6 +380,8 @@ public class Habit
@Nullable
public Reminder reminder;
public int position;
public HabitData()
{
this.color = 8;
@@ -381,6 +393,7 @@ public class Habit
this.targetType = AT_LEAST;
this.targetValue = 100;
this.unit = "";
this.position = 0;
}
public HabitData(@NonNull HabitData model)
@@ -395,6 +408,7 @@ public class Habit
this.type = model.type;
this.unit = model.unit;
this.reminder = model.reminder;
this.position = model.position;
}
@Override
@@ -411,6 +425,7 @@ public class Habit
.append("type", type)
.append("unit", unit)
.append("reminder", reminder)
.append("position", position)
.toString();
}
@@ -434,6 +449,7 @@ public class Habit
.append(frequency, habitData.frequency)
.append(unit, habitData.unit)
.append(reminder, habitData.reminder)
.append(position, habitData.position)
.isEquals();
}
@@ -451,6 +467,7 @@ public class Habit
.append(type)
.append(unit)
.append(reminder)
.append(position)
.toHashCode();
}
}

View File

@@ -139,10 +139,18 @@ public class MemoryHabitList extends HabitList
Double s1 = h1.getScores().getTodayValue();
Double s2 = h2.getScores().getTodayValue();
if (s1.equals(s2)) return nameComparator.compare(h1, h2);
return s2.compareTo(s1);
else return s2.compareTo(s1);
};
if (order == BY_POSITION) return null;
Comparator<Habit> positionComparator = (h1, h2) ->
{
Integer p1 = h1.getPosition();
Integer p2 = h2.getPosition();
if (p1.equals(p2)) return nameComparator.compare(h1, h2);
else return p1.compareTo(p2);
};
if (order == BY_POSITION) return positionComparator;
if (order == BY_NAME) return nameComparator;
if (order == BY_COLOR) return colorComparator;
if (order == BY_SCORE) return scoreComparator;

View File

@@ -85,11 +85,11 @@ public class SQLiteHabitList extends HabitList
public synchronized void add(@NonNull Habit habit)
{
loadRecords();
habit.setPosition(size());
list.add(habit);
HabitRecord record = new HabitRecord();
record.copyFrom(habit);
record.position = size();
repository.save(record);
rebuildOrder();
@@ -173,8 +173,8 @@ public class SQLiteHabitList extends HabitList
((SQLiteRepetitionList) habit.getRepetitions()).removeAll();
repository.remove(record);
});
rebuildOrder();
rebuildOrder();
getObservable().notifyListeners();
}
@@ -213,7 +213,7 @@ public class SQLiteHabitList extends HabitList
{
repository.execSQL("update habits set position = position - 1 " +
"where position > ? and position <= ?",
fromPos, toPos);
fromPos, toPos);
}
fromRecord.position = toPos;

View File

@@ -91,6 +91,7 @@ public class HabitRecord
this.targetType = model.getTargetType();
this.targetValue = model.getTargetValue();
this.unit = model.getUnit();
this.position = model.getPosition();
Frequency freq = model.getFrequency();
this.freqNum = freq.getNumerator();
@@ -120,6 +121,7 @@ public class HabitRecord
habit.setTargetType(this.targetType);
habit.setTargetValue(this.targetValue);
habit.setUnit(this.unit);
habit.setPosition(this.position);
if (reminderHour != null && reminderMin != null)
{