Skip to content

Commit

Permalink
Remove cache that was causing refreshAllObservers to do nothing
Browse files Browse the repository at this point in the history
The ViewModel already keeps a copy of each CounterSummary, so this was
redundant.

Fixes #68
  • Loading branch information
albertvaka committed Jun 19, 2024
1 parent 5378e77 commit 4db4724
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions app/src/main/java/org/kde/bettercounter/persistence/Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class Repository(

private var tutorials: MutableSet<String>
private var counters: List<String>
private var counterCache = HashMap<String, CounterSummary>()

init {
val countersStr = sharedPref.getString(COUNTERS_PREFS_KEY, "[]")
Expand Down Expand Up @@ -90,7 +89,6 @@ class Repository(
.remove(intervalKey)
.remove(goalKey)
.apply()
counterCache.remove(name)
}

fun setCounterMetadata(counter: CounterMetadata) {
Expand All @@ -102,7 +100,6 @@ class Repository(
.putString(intervalKey, counter.interval.toString())
.putInt(goalKey, counter.goal)
.apply()
counterCache.remove(counter.name)
}

suspend fun getCounterSummary(name: String): CounterSummary {
Expand All @@ -115,42 +112,36 @@ class Repository(
}
val intervalEndDate = intervalStartDate.copy().apply { addInterval(interval, 1) }
val firstLastAndCount = entryDao.getFirstLastAndCount(name)
return counterCache.getOrPut(name) {
CounterSummary(
name = name,
color = color,
interval = interval,
goal = goal,
lastIntervalCount = entryDao.getCountInRange(name, intervalStartDate.time, intervalEndDate.time),
totalCount = firstLastAndCount.count, // entryDao.getCount(name),
leastRecent = firstLastAndCount.first, // entryDao.getLeastRecent(name)?.date,
mostRecent = firstLastAndCount.last, // entryDao.getMostRecent(name)?.date,
)
}
return CounterSummary(
name = name,
color = color,
interval = interval,
goal = goal,
lastIntervalCount = entryDao.getCountInRange(name, intervalStartDate.time, intervalEndDate.time),
totalCount = firstLastAndCount.count, // entryDao.getCount(name),
leastRecent = firstLastAndCount.first, // entryDao.getLeastRecent(name)?.date,
mostRecent = firstLastAndCount.last, // entryDao.getMostRecent(name)?.date,
)
}

suspend fun renameCounter(oldName: String, newName: String) {
entryDao.renameCounter(oldName, newName)
counterCache.remove(oldName)
}

suspend fun addEntry(name: String, date: Date = Calendar.getInstance().time) {
entryDao.insert(Entry(name = name, date = date))
counterCache.remove(name)
}

suspend fun removeEntry(name: String): Date? {
val entry = entryDao.getLastAdded(name)
if (entry != null) {
entryDao.delete(entry)
}
counterCache.remove(name)
return entry?.date
}

suspend fun removeAllEntries(name: String) {
entryDao.deleteAll(name)
counterCache.remove(name)
}

suspend fun getEntriesForRangeSortedByDate(name: String, since: Date, until: Date): List<Entry> {
Expand All @@ -162,6 +153,5 @@ class Repository(

suspend fun bulkAddEntries(entries: List<Entry>) {
entryDao.bulkInsert(entries)
counterCache.clear() // we don't know what changed
}
}

0 comments on commit 4db4724

Please sign in to comment.