MassMigrationPreviewViewModel.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/migration/MassMigrationPreviewViewModel.kt
1package eu.friedcutlet.yokan.ui.screens.migration23import kotlinx.coroutines.CoroutineDispatcher4import androidx.lifecycle.ViewModel5import androidx.lifecycle.viewModelScope6import eu.friedcutlet.yokan.data.LibraryIndexStore7import eu.friedcutlet.yokan.data.SettingsStore8import eu.friedcutlet.yokan.data.YokanRepository9import eu.friedcutlet.yokan.data.observe10import eu.friedcutlet.yokan.domain.Manga11import eu.friedcutlet.yokan.domain.Source12import eu.friedcutlet.yokan.services.library.sortedChapters13import kotlinx.coroutines.Dispatchers14import kotlinx.coroutines.flow.MutableStateFlow15import kotlinx.coroutines.flow.StateFlow16import kotlinx.coroutines.flow.asStateFlow17import kotlinx.coroutines.flow.combine18import kotlinx.coroutines.flow.flowOn19import kotlinx.coroutines.flow.update20import kotlinx.coroutines.launch2122/** A library title of the migrated source with the chapters shown in its expandable section. */23internal data class MassMigrationPreviewItem(24 val manga: Manga,25 val chapterCount: Int,26 /** The first [PREVIEW_CHAPTER_COUNT] chapter names in the title's chapter sort order. */27 val previewChapterNames: List<String>,28)2930internal data class MassMigrationPreviewState(31 val source: Source?,32 /** Null until the library has been read. */33 val items: List<MassMigrationPreviewItem>? = null,34 val selectedIds: Set<Long> = emptySet(),35 /** The last toggled row; a long press selects the range from it. */36 val anchorIndex: Int? = null,37) {38 val selectedCount: Int get() = items.orEmpty().count { it.manga.id in selectedIds }39 val allSelected: Boolean get() = !items.isNullOrEmpty() && selectedCount == items.size4041 /** The selected titles in list order. */42 val selectedMangaIds: List<Long> get() = items.orEmpty().map { it.manga.id }.filter { it in selectedIds }43}4445private const val PREVIEW_CHAPTER_COUNT = 124647/** Favorite titles of [sourceId] to migrate; every title starts selected. */48internal class MassMigrationPreviewViewModel(49 sourceId: Long,50 repository: YokanRepository,51 settings: SettingsStore,52 libraryIndex: LibraryIndexStore,53 private val workDispatcher: CoroutineDispatcher = Dispatchers.Default,54) : ViewModel() {55 private val _state = MutableStateFlow(MassMigrationPreviewState(source = repository.source(sourceId)))56 val state: StateFlow<MassMigrationPreviewState> = _state.asStateFlow()5758 init {59 _state.value.source?.let { source ->60 val titles = repository.observe({ it.mangas }, workDispatcher) {61 allTitles(source.itemType).filter { it.favorite && it.source == source.name }62 }63 viewModelScope.launch {64 combine(titles, libraryIndex.index, settings.db) { mangas, index, db ->65 mangas.map { manga ->66 val chapters = index.chapters(manga.id)67 MassMigrationPreviewItem(68 manga = manga,69 chapterCount = chapters.size,70 previewChapterNames = sortedChapters(manga, chapters, db).take(PREVIEW_CHAPTER_COUNT).map { it.name },71 )72 }73 }74 .flowOn(workDispatcher)75 .collect { items ->76 _state.update { state ->77 state.copy(78 items = items,79 selectedIds = if (state.items == null) items.mapTo(HashSet()) { it.manga.id } else state.selectedIds,80 )81 }82 }83 }84 }85 }8687 fun toggle(index: Int) {88 _state.update { state ->89 val id = state.items?.getOrNull(index)?.manga?.id ?: return@update state90 state.copy(selectedIds = state.selectedIds.toggled(id), anchorIndex = index)91 }92 }9394 /** Selects the range from the anchor to [index], or toggles [index] when nothing is anchored. */95 fun selectRangeTo(index: Int) {96 _state.update { state ->97 val ids = state.items.orEmpty().map { it.manga.id }98 val id = ids.getOrNull(index) ?: return@update state99 val anchor = state.anchorIndex100 val selectedIds = if (anchor == null) state.selectedIds.toggled(id) else state.selectedIds.plusRange(anchor, index, ids)101 state.copy(selectedIds = selectedIds, anchorIndex = index)102 }103 }104105 fun toggleAll() {106 _state.update { state ->107 val selectedIds = if (state.allSelected) emptySet() else state.items.orEmpty().mapTo(HashSet()) { it.manga.id }108 state.copy(selectedIds = selectedIds, anchorIndex = null)109 }110 }111}112113internal fun Set<Long>.toggled(id: Long): Set<Long> = if (id in this) this - id else this + id114115/** Adds the ids between [from] and [to] (inclusive, either order, clamped to [ids]). */116internal fun Set<Long>.plusRange(from: Int, to: Int, ids: List<Long>): Set<Long> {117 if (ids.isEmpty()) return this118 val start = minOf(from, to).coerceIn(0, ids.lastIndex)119 val end = maxOf(from, to).coerceIn(0, ids.lastIndex)120 return this + ids.subList(start, end + 1)121}122