Skip to content

Commit

Permalink
Cache history items
Browse files Browse the repository at this point in the history
  • Loading branch information
CrisBarreiro committed Apr 5, 2024
1 parent 0f99c32 commit fcb236d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions app/src/main/java/com/duckduckgo/app/history/HistoryRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ class RealHistoryRepository(
private val currentTimeProvider: CurrentTimeProvider,
) : HistoryRepository {

override fun getHistoryObservable(): Single<List<HistoryEntry>> =
historyDao.getHistoryEntriesWithVisits().map { it.map { historyEntryWithVisits -> historyEntryWithVisits.toHistoryEntry() } }
private var cachedHistoryEntries: List<HistoryEntry>? = null

override fun getHistoryObservable(): Single<List<HistoryEntry>> {
return if (cachedHistoryEntries != null) {
Single.just(cachedHistoryEntries)
} else {
fetchAndCacheHistoryEntries()
}
}

override fun saveToHistory(
url: String,
Expand All @@ -45,5 +52,14 @@ class RealHistoryRepository(
isSerp: Boolean,
) {
historyDao.updateOrInsertVisit(url, title ?: "", query, isSerp, currentTimeProvider.currentTimeMillis())
fetchAndCacheHistoryEntries()
}
private fun fetchAndCacheHistoryEntries(): Single<List<HistoryEntry>> {
return historyDao.getHistoryEntriesWithVisits()
.map { entries ->
entries.map { it.toHistoryEntry() }.also {
cachedHistoryEntries = it
}
}
}
}

0 comments on commit fcb236d

Please sign in to comment.