@VisibleForTesting internal fun getCombinedHistory( historyHighlights: List<HistoryHighlightInternal>, historyGroups: List<HistoryGroupInternal>, ): List<RecentlyVisitedItem> { // Cleanup highlights now to avoid counting them below and then removing the ones found in groups. val distinctHighlights = historyHighlights .removeHighlightsAlreadyInGroups(historyGroups) val totalItemsCount = distinctHighlights.size + historyGroups.size return if (totalItemsCount <= MAX_RESULTS_TOTAL) { getSortedHistory( distinctHighlights.sortedByDescending { it.lastAccessedTime }, historyGroups.sortedByDescending { it.lastAccessedTime }, ) } else { var groupsCount = 0 var highlightCount = 0 while ((highlightCount + groupsCount) < MAX_RESULTS_TOTAL) { if ((highlightCount + groupsCount) < MAX_RESULTS_TOTAL && distinctHighlights.getOrNull(highlightCount) != null ) { highlightCount += 1 } if ((highlightCount + groupsCount) < MAX_RESULTS_TOTAL && historyGroups.getOrNull(groupsCount) != null ) { groupsCount += 1 } } getSortedHistory( distinctHighlights .sortedByDescending { it.lastAccessedTime } .take(highlightCount), historyGroups .sortedByDescending { it.lastAccessedTime } .take(groupsCount), ) } }
Get up to [MAX_RESULTS_TOTAL] items if available as an even split of history highlights and history groups. If more items then needed are available then highlights will be more by one.