package eu.friedcutlet.yokan.ui.screens.migration import kotlinx.coroutines.CoroutineDispatcher import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import eu.friedcutlet.yokan.data.LibraryIndexStore import eu.friedcutlet.yokan.data.SettingsStore import eu.friedcutlet.yokan.data.YokanRepository import eu.friedcutlet.yokan.data.observe import eu.friedcutlet.yokan.domain.Manga import eu.friedcutlet.yokan.domain.Source import eu.friedcutlet.yokan.services.library.sortedChapters import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch /** A library title of the migrated source with the chapters shown in its expandable section. */ internal data class MassMigrationPreviewItem( val manga: Manga, val chapterCount: Int, /** The first [PREVIEW_CHAPTER_COUNT] chapter names in the title's chapter sort order. */ val previewChapterNames: List, ) internal data class MassMigrationPreviewState( val source: Source?, /** Null until the library has been read. */ val items: List? = null, val selectedIds: Set = emptySet(), /** The last toggled row; a long press selects the range from it. */ val anchorIndex: Int? = null, ) { val selectedCount: Int get() = items.orEmpty().count { it.manga.id in selectedIds } val allSelected: Boolean get() = !items.isNullOrEmpty() && selectedCount == items.size /** The selected titles in list order. */ val selectedMangaIds: List get() = items.orEmpty().map { it.manga.id }.filter { it in selectedIds } } private const val PREVIEW_CHAPTER_COUNT = 12 /** Favorite titles of [sourceId] to migrate; every title starts selected. */ internal class MassMigrationPreviewViewModel( sourceId: Long, repository: YokanRepository, settings: SettingsStore, libraryIndex: LibraryIndexStore, private val workDispatcher: CoroutineDispatcher = Dispatchers.Default, ) : ViewModel() { private val _state = MutableStateFlow(MassMigrationPreviewState(source = repository.source(sourceId))) val state: StateFlow = _state.asStateFlow() init { _state.value.source?.let { source -> val titles = repository.observe({ it.mangas }, workDispatcher) { allTitles(source.itemType).filter { it.favorite && it.source == source.name } } viewModelScope.launch { combine(titles, libraryIndex.index, settings.db) { mangas, index, db -> mangas.map { manga -> val chapters = index.chapters(manga.id) MassMigrationPreviewItem( manga = manga, chapterCount = chapters.size, previewChapterNames = sortedChapters(manga, chapters, db).take(PREVIEW_CHAPTER_COUNT).map { it.name }, ) } } .flowOn(workDispatcher) .collect { items -> _state.update { state -> state.copy( items = items, selectedIds = if (state.items == null) items.mapTo(HashSet()) { it.manga.id } else state.selectedIds, ) } } } } } fun toggle(index: Int) { _state.update { state -> val id = state.items?.getOrNull(index)?.manga?.id ?: return@update state state.copy(selectedIds = state.selectedIds.toggled(id), anchorIndex = index) } } /** Selects the range from the anchor to [index], or toggles [index] when nothing is anchored. */ fun selectRangeTo(index: Int) { _state.update { state -> val ids = state.items.orEmpty().map { it.manga.id } val id = ids.getOrNull(index) ?: return@update state val anchor = state.anchorIndex val selectedIds = if (anchor == null) state.selectedIds.toggled(id) else state.selectedIds.plusRange(anchor, index, ids) state.copy(selectedIds = selectedIds, anchorIndex = index) } } fun toggleAll() { _state.update { state -> val selectedIds = if (state.allSelected) emptySet() else state.items.orEmpty().mapTo(HashSet()) { it.manga.id } state.copy(selectedIds = selectedIds, anchorIndex = null) } } } internal fun Set.toggled(id: Long): Set = if (id in this) this - id else this + id /** Adds the ids between [from] and [to] (inclusive, either order, clamped to [ids]). */ internal fun Set.plusRange(from: Int, to: Int, ids: List): Set { if (ids.isEmpty()) return this val start = minOf(from, to).coerceIn(0, ids.lastIndex) val end = maxOf(from, to).coerceIn(0, ids.lastIndex) return this + ids.subList(start, end + 1) }