HistoryViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/activity/HistoryViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.activity23import androidx.lifecycle.ViewModel4import androidx.lifecycle.viewModelScope5import eu.friedcutlet.yokan.data.LibraryIndex6import eu.friedcutlet.yokan.data.LibraryIndexStore7import eu.friedcutlet.yokan.data.YokanRepository8import eu.friedcutlet.yokan.data.observe9import eu.friedcutlet.yokan.domain.Chapter10import eu.friedcutlet.yokan.domain.History11import eu.friedcutlet.yokan.domain.ItemType12import eu.friedcutlet.yokan.domain.Manga13import kotlinx.coroutines.CoroutineDispatcher14import kotlinx.coroutines.Dispatchers15import kotlinx.coroutines.flow.MutableStateFlow16import kotlinx.coroutines.flow.StateFlow17import kotlinx.coroutines.flow.asStateFlow18import kotlinx.coroutines.flow.combine19import kotlinx.coroutines.flow.flowOn20import kotlinx.coroutines.flow.update21import kotlinx.coroutines.launch2223/** One History row with its title and chapter resolved once per data change. */24data class HistoryRowModel(25 val history: History,26 val manga: Manga?,27 val chapter: Chapter?,28) {29 val title: String = manga?.name?.takeIf { it.isNotBlank() } ?: history.title30 val chapterName: String = chapter?.name?.takeIf { it.isNotBlank() } ?: history.chapterName31}3233data class HistoryState(34 /** The latest entry of each title per item type, newest first. */35 val rowsByType: Map<ItemType, List<HistoryRowModel>> = emptyMap(),36)3738private class HistorySource(val history: List<History>, val titles: Map<Long, Manga>)3940class HistoryViewModel(41 private val repository: YokanRepository,42 libraryIndex: LibraryIndexStore,43 workDispatcher: CoroutineDispatcher = Dispatchers.Default,44) : ViewModel() {45 private val _state = MutableStateFlow(HistoryState())46 val state: StateFlow<HistoryState> = _state.asStateFlow()4748 init {49 // Titles depend on the settings snapshot too (toDomain), so key the read on all three parts.50 val source = repository.observe({ Triple(it.histories, it.mangas, it.settings) }, workDispatcher) {51 val history = history()52 val titles = HashMap<Long, Manga>()53 for (entry in history) {54 if (entry.mangaId !in titles) manga(entry.mangaId)?.let { titles[entry.mangaId] = it }55 }56 HistorySource(history, titles)57 }58 viewModelScope.launch {59 combine(source, libraryIndex.index) { data, index -> buildHistoryRows(data.history, data.titles, index) }60 .flowOn(workDispatcher)61 .collect { rows -> _state.update { it.copy(rowsByType = rows) } }62 }63 }6465 fun removeHistory(historyId: Long) {66 repository.removeHistory(historyId)67 }6869 fun clearHistory(itemType: ItemType) {70 repository.clearHistory(itemType)71 }72}7374/**75 * Keeps the newest entry of each title, grouped by item type and sorted newest first, with the76 * title and chapter resolved. The chapter is looked up only when the title still exists.77 */78internal fun buildHistoryRows(79 history: List<History>,80 titles: Map<Long, Manga>,81 index: LibraryIndex,82): Map<ItemType, List<HistoryRowModel>> =83 history.groupBy { it.itemType }84 .mapValues { (_, entries) ->85 entries.groupBy { it.mangaId }86 .values87 .mapNotNull { rows -> rows.maxByOrNull { it.updatedAt } }88 .sortedByDescending { it.updatedAt }89 .map { entry ->90 val manga = titles[entry.mangaId]91 HistoryRowModel(92 history = entry,93 manga = manga,94 chapter = manga?.let { title -> index.chapters(title.id).firstOrNull { it.id == entry.chapterId } },95 )96 }97 }98