Implement saving of sub habits but breaks updating group score ring

pull/2020/head
Dharanish 1 year ago
parent c060249806
commit c77da1803f

@ -97,7 +97,7 @@ class HabitCardView(
set(value) { set(value) {
scoreRing.setPercentage(value.toFloat()) scoreRing.setPercentage(value.toFloat())
scoreRing.setPrecision(1.0f / 16) scoreRing.setPrecision(1.0f / 16)
behavior.onChangeScore(habit!!) // behavior.onChangeScore(habit!!)
} }
var unit var unit

@ -19,6 +19,7 @@
package org.isoron.uhabits.core.commands package org.isoron.uhabits.core.commands
import org.isoron.uhabits.core.models.Habit import org.isoron.uhabits.core.models.Habit
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitList import org.isoron.uhabits.core.models.HabitList
data class DeleteHabitsCommand( data class DeleteHabitsCommand(
@ -30,7 +31,7 @@ data class DeleteHabitsCommand(
if (!h.isSubHabit()) { if (!h.isSubHabit()) {
habitList.remove(h) habitList.remove(h)
} else { } else {
val list = h.parent!!.habitList val list = (h.parent as HabitGroup).habitList
list.remove(h) list.remove(h)
} }
} }

@ -19,7 +19,7 @@ data class HabitGroup(
) { ) {
init { init {
if (uuid == null) this.uuid = UUID.randomUUID().toString().replace("-", "") if (uuid == null) this.uuid = UUID.randomUUID().toString().replace("-", "")
habitList.groupUUID = this.uuid // habitList.groupID = this.id
} }
var observable = ModelObservable() var observable = ModelObservable()
@ -78,7 +78,7 @@ data class HabitGroup(
this.question = other.question this.question = other.question
this.reminder = other.reminder this.reminder = other.reminder
this.uuid = other.uuid this.uuid = other.uuid
this.habitList.groupUUID = this.uuid this.habitList.groupID = this.id
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {

@ -34,7 +34,7 @@ abstract class HabitList : Iterable<Habit> {
@JvmField @JvmField
protected val filter: HabitMatcher protected val filter: HabitMatcher
var groupUUID: String? = null var groupID: Long? = null
/** /**
* Creates a new HabitList. * Creates a new HabitList.

@ -40,6 +40,7 @@ class SQLiteHabitGroupList @Inject constructor(private val modelFactory: ModelFa
record.copyFrom(habitGroup) record.copyFrom(habitGroup)
repository.save(record) repository.save(record)
habitGroup.id = record.id habitGroup.id = record.id
habitGroup.habitList.groupID = record.id
list.add(habitGroup) list.add(habitGroup)
observable.notifyListeners() observable.notifyListeners()
} }

@ -37,18 +37,15 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
private fun loadRecords() { private fun loadRecords() {
if (loaded) return if (loaded) return
loaded = true loaded = true
list.groupUUID = this.groupUUID list.groupID = this.groupID
list.removeAll() list.removeAll()
val records = repository.findAll("order by position") val records = repository.findAll("order by position")
var shouldRebuildOrder = false for (rec in records) {
for ((expectedPosition, rec) in records.withIndex()) {
if (rec.position != expectedPosition) shouldRebuildOrder = true
val h = modelFactory.buildHabit() val h = modelFactory.buildHabit()
rec.copyTo(h) rec.copyTo(h)
(h.originalEntries as SQLiteEntryList).habitId = h.id (h.originalEntries as SQLiteEntryList).habitId = h.id
if (h.parentUUID == list.groupUUID) list.add(h) if (h.parentID == list.groupID) list.add(h)
} }
if (shouldRebuildOrder) rebuildOrder()
} }
@Synchronized @Synchronized
@ -129,19 +126,6 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
return list.iterator() return list.iterator()
} }
@Synchronized
private fun rebuildOrder() {
val records = repository.findAll("order by position")
repository.executeAsTransaction {
for ((pos, r) in records.withIndex()) {
if (r.position != pos) {
r.position = pos
repository.save(r)
}
}
}
}
@Synchronized @Synchronized
override fun remove(h: Habit) { override fun remove(h: Habit) {
loadRecords() loadRecords()
@ -153,7 +137,6 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
h.originalEntries.clear() h.originalEntries.clear()
repository.remove(record) repository.remove(record)
} }
rebuildOrder()
observable.notifyListeners() observable.notifyListeners()
} }
@ -206,7 +189,6 @@ class SQLiteHabitList @Inject constructor(private val modelFactory: ModelFactory
@Synchronized @Synchronized
override fun repair() { override fun repair() {
loadRecords() loadRecords()
rebuildOrder()
observable.notifyListeners() observable.notifyListeners()
} }

@ -79,6 +79,7 @@ class HabitGroupRecord {
habitGroup.isArchived = archived != 0 habitGroup.isArchived = archived != 0
habitGroup.position = position!! habitGroup.position = position!!
habitGroup.uuid = uuid habitGroup.uuid = uuid
habitGroup.habitList.groupID = id
if (reminderHour != null && reminderMin != null) { if (reminderHour != null && reminderMin != null) {
habitGroup.reminder = Reminder( habitGroup.reminder = Reminder(
reminderHour!!, reminderHour!!,

@ -146,7 +146,7 @@ class HabitCardListCache @Inject constructor(
@get:Synchronized @get:Synchronized
val subHabitCount: Int val subHabitCount: Int
get() = data.subHabits.sumOf { it.size() } get() = data.subHabits.sumOf { it.size }
@get:Synchronized @get:Synchronized
@set:Synchronized @set:Synchronized
@ -279,8 +279,9 @@ class HabitCardListCache @Inject constructor(
fun setFilter(matcher: HabitMatcher) { fun setFilter(matcher: HabitMatcher) {
filteredHabits = habits.getFiltered(matcher) filteredHabits = habits.getFiltered(matcher)
filteredHabitGroups = habitGroups.getFiltered(matcher) filteredHabitGroups = habitGroups.getFiltered(matcher)
for (idx in filteredSubHabits.indices) { filteredSubHabits.clear()
filteredSubHabits[idx] = filteredSubHabits[idx].getFiltered(matcher) for (hgr in filteredHabitGroups) {
filteredSubHabits.add(hgr.habitList.getFiltered(matcher))
} }
} }
@ -306,7 +307,7 @@ class HabitCardListCache @Inject constructor(
val uuidToHabitGroup: HashMap<String?, HabitGroup> = HashMap() val uuidToHabitGroup: HashMap<String?, HabitGroup> = HashMap()
val habits: MutableList<Habit> val habits: MutableList<Habit>
val habitGroups: MutableList<HabitGroup> val habitGroups: MutableList<HabitGroup>
val subHabits: MutableList<HabitList> val subHabits: MutableList<MutableList<Habit>>
val uuidToPosition: HashMap<String?, Int> val uuidToPosition: HashMap<String?, Int>
val positionTypes: MutableList<Int> val positionTypes: MutableList<Int>
val positionToHabit: HashMap<Int, Habit> val positionToHabit: HashMap<Int, Habit>
@ -368,15 +369,14 @@ class HabitCardListCache @Inject constructor(
habits.add(h) habits.add(h)
} }
for (hgr in filteredHabitGroups) { for ((index, hgr) in filteredHabitGroups.withIndex()) {
if (hgr.uuid == null) continue if (hgr.uuid == null) continue
habitGroups.add(hgr) habitGroups.add(hgr)
val habitList = hgr.habitList val habitList = LinkedList<Habit>()
subHabits.add(habitList) for (h in filteredSubHabits[index]) {
habitList.add(h)
for (h in habitList) {
if (h.uuid == null) continue
} }
subHabits.add(habitList)
} }
} }
@ -654,6 +654,10 @@ class HabitCardListCache @Inject constructor(
data.habits.add(position, habit) data.habits.add(position, habit)
data.positionTypes.add(position, STANDALONE_HABIT) data.positionTypes.add(position, STANDALONE_HABIT)
} else { } else {
val hgr = data.uuidToHabitGroup[habit.parentUUID]
val hgrIdx = data.habitGroups.indexOf(hgr)
val habitIndex = newData.subHabits[hgrIdx].indexOf(habit)
data.subHabits[hgrIdx].add(habitIndex, habit)
data.positionTypes.add(position, SUB_HABIT) data.positionTypes.add(position, SUB_HABIT)
} }
data.incrementPositions(position, data.positionTypes.size - 1) data.incrementPositions(position, data.positionTypes.size - 1)

@ -1,8 +1,3 @@
alter table Habits add column skip_days integer not null default 0;
alter table Habits add column skip_days_list integer not null default 0;
alter table Habits add column parent_id integer;
alter table Habits add column parent_uuid text;
create table HabitGroups ( create table HabitGroups (
id integer primary key autoincrement, id integer primary key autoincrement,
archived integer, archived integer,
@ -17,3 +12,6 @@ create table HabitGroups (
question text not null default "", question text not null default "",
uuid text uuid text
); );
alter table Habits add column parent_uuid text references habitgroups(uuid);
alter table Habits add column parent_id integer references habitgroups(id);
Loading…
Cancel
Save