[fenix] Post: move group removal logic into PagedHistoryProvider

This moves the group removal logic to the place where the groups are
actually formed. This helps clean-up the fragment code a bit, and
removes the awkward 'allow mutate some random internal state' API from
the provider.
pull/600/head
Grisha Kruglov 3 years ago committed by Christian Sadilek
parent fc7e707538
commit 77f35fd757

@ -104,13 +104,31 @@ class DefaultPagedHistoryProvider(
}
}
/**
* Removes [group] and any corresponding history visits.
*/
suspend fun deleteHistoryGroup(group: History.Group) {
for (historyMetadata in group.items) {
getMatchingHistory(historyMetadata)?.let {
historyStorage.deleteVisit(
url = it.url,
timestamp = it.visitTime
)
}
}
historyStorage.deleteHistoryMetadata(
searchTerm = group.title
)
// Force a re-fetch of the groups next time we go through #getHistory.
historyGroups = null
}
/**
* Returns the [History.Regular] corresponding to the given [History.Metadata] item.
*
* @param historyMetadata The [History.Metadata] to match.
* @return the [History.Regular] corresponding to the given [History.Metadata] item or null.
*/
suspend fun getMatchingHistory(historyMetadata: History.Metadata): VisitInfo? {
private suspend fun getMatchingHistory(historyMetadata: History.Metadata): VisitInfo? {
val history = historyStorage.getDetailedVisits(
start = historyMetadata.visitedAt - BUFFER_TIME,
end = historyMetadata.visitedAt + BUFFER_TIME,
@ -121,13 +139,6 @@ class DefaultPagedHistoryProvider(
.minByOrNull { abs(historyMetadata.visitedAt - it.visitTime) }
}
/**
* Clears the history groups to refetch the most history metadata after any changes.
*/
fun clearHistoryGroups() {
historyGroups = null
}
@Suppress("MagicNumber")
private suspend fun getHistoryAndSearchGroups(
offset: Long,

@ -355,26 +355,11 @@ class HistoryFragment : LibraryPageFragment<History>(), UserInteractionHandler {
for (item in items) {
analytics.metrics.track(Event.HistoryItemRemoved)
if (item is History.Regular) {
core.historyStorage.deleteVisit(
url = item.url,
timestamp = item.visitedAt
)
} else if (item is History.Group) {
for (historyMetadata in item.items) {
historyProvider.getMatchingHistory(historyMetadata)?.let {
core.historyStorage.deleteVisit(
url = it.url,
timestamp = it.visitTime
)
}
}
core.historyStorage.deleteHistoryMetadata(
searchTerm = item.title
)
historyProvider.clearHistoryGroups()
when (item) {
is History.Regular -> core.historyStorage.deleteVisit(item.url, item.visitedAt)
is History.Group -> historyProvider.deleteHistoryGroup(item)
// We won't encounter individual metadata entries outside of groups.
is History.Metadata -> {}
}
}
}

Loading…
Cancel
Save