Fix Migration screen exiting instantly

This commit is contained in:
Jobobby04 2022-12-03 19:23:19 -05:00
parent 0a9f438894
commit 652bf68859
2 changed files with 19 additions and 19 deletions

View File

@ -36,7 +36,7 @@ class MigrationListScreen(private val config: MigrationProcedureConfig) : Screen
val navigator = LocalNavigator.currentOrThrow val navigator = LocalNavigator.currentOrThrow
val context = LocalContext.current val context = LocalContext.current
LaunchedEffect(items) { LaunchedEffect(items) {
if (items.isEmpty()) { if (items?.isEmpty() == true) {
val manualMigrations = screenModel.manualMigrations.value val manualMigrations = screenModel.manualMigrations.value
context.toast( context.toast(
context.resources.getQuantityString( context.resources.getQuantityString(
@ -61,10 +61,10 @@ class MigrationListScreen(private val config: MigrationProcedureConfig) : Screen
LaunchedEffect(screenModel) { LaunchedEffect(screenModel) {
screenModel.navigateOut.collect { screenModel.navigateOut.collect {
if (items.size == 1) { if (items.orEmpty().size == 1) {
val hasDetails = navigator.items.any { it is MangaScreen } val hasDetails = navigator.items.any { it is MangaScreen }
if (hasDetails) { if (hasDetails) {
val manga = (items.firstOrNull()?.searchResult?.value as? MigratingManga.SearchResult.Result)?.let { val manga = (items.orEmpty().firstOrNull()?.searchResult?.value as? MigratingManga.SearchResult.Result)?.let {
screenModel.getManga(it.id) screenModel.getManga(it.id)
} }
withUIContext { withUIContext {
@ -89,7 +89,7 @@ class MigrationListScreen(private val config: MigrationProcedureConfig) : Screen
} }
} }
MigrationListScreen( MigrationListScreen(
items = items, items = items.orEmpty(),
migrationDone = migrationDone, migrationDone = migrationDone,
unfinishedCount = unfinishedCount, unfinishedCount = unfinishedCount,
getManga = screenModel::getManga, getManga = screenModel::getManga,

View File

@ -79,7 +79,7 @@ class MigrationListScreenModel(
private val smartSearchEngine = SmartSearchEngine(config.extraSearchParams) private val smartSearchEngine = SmartSearchEngine(config.extraSearchParams)
private val throttleManager = EHentaiThrottleManager() private val throttleManager = EHentaiThrottleManager()
val migratingItems = MutableStateFlow<List<MigratingManga>>(emptyList()) val migratingItems = MutableStateFlow<List<MigratingManga>?>(null)
val migrationDone = MutableStateFlow(false) val migrationDone = MutableStateFlow(false)
val unfinishedCount = MutableStateFlow(0) val unfinishedCount = MutableStateFlow(0)
@ -275,21 +275,21 @@ class MigrationListScreenModel(
} }
private suspend fun sourceFinished() { private suspend fun sourceFinished() {
unfinishedCount.value = migratingItems.value.count { unfinishedCount.value = migratingItems.value.orEmpty().count {
it.searchResult.value != SearchResult.Searching it.searchResult.value != SearchResult.Searching
} }
if (allMangasDone()) { if (allMangasDone()) {
migrationDone.value = true migrationDone.value = true
} }
if (migratingItems.value.isEmpty()) { if (migratingItems.value?.isEmpty() == true) {
navigateOut() navigateOut()
} }
} }
fun allMangasDone() = migratingItems.value.all { it.searchResult.value != SearchResult.Searching } && fun allMangasDone() = migratingItems.value.orEmpty().all { it.searchResult.value != SearchResult.Searching } &&
migratingItems.value.any { it.searchResult.value is SearchResult.Result } migratingItems.value.orEmpty().any { it.searchResult.value is SearchResult.Result }
fun mangasSkipped() = migratingItems.value.count { it.searchResult.value == SearchResult.NotFound } fun mangasSkipped() = migratingItems.value.orEmpty().count { it.searchResult.value == SearchResult.NotFound }
private suspend fun migrateMangaInternal( private suspend fun migrateMangaInternal(
prevManga: Manga, prevManga: Manga,
@ -386,7 +386,7 @@ class MigrationListScreenModel(
} }
fun useMangaForMigration(context: Context, newMangaId: Long, selectedMangaId: Long) { fun useMangaForMigration(context: Context, newMangaId: Long, selectedMangaId: Long) {
val migratingManga = migratingItems.value.find { it.manga.id == selectedMangaId } val migratingManga = migratingItems.value.orEmpty().find { it.manga.id == selectedMangaId }
?: return ?: return
migratingManga.searchResult.value = SearchResult.Searching migratingManga.searchResult.value = SearchResult.Searching
coroutineScope.launchIO { coroutineScope.launchIO {
@ -425,7 +425,7 @@ class MigrationListScreenModel(
fun migrateMangas() { fun migrateMangas() {
coroutineScope.launchIO { coroutineScope.launchIO {
migratingItems.value.forEach { manga -> migratingItems.value.orEmpty().forEach { manga ->
val searchResult = manga.searchResult.value val searchResult = manga.searchResult.value
if (searchResult is SearchResult.Result) { if (searchResult is SearchResult.Result) {
val toMangaObj = getManga.await(searchResult.id) ?: return@forEach val toMangaObj = getManga.await(searchResult.id) ?: return@forEach
@ -443,7 +443,7 @@ class MigrationListScreenModel(
fun copyMangas() { fun copyMangas() {
coroutineScope.launchIO { coroutineScope.launchIO {
migratingItems.value.forEach { manga -> migratingItems.value.orEmpty().forEach { manga ->
val searchResult = manga.searchResult.value val searchResult = manga.searchResult.value
if (searchResult is SearchResult.Result) { if (searchResult is SearchResult.Result) {
val toMangaObj = getManga.await(searchResult.id) ?: return@forEach val toMangaObj = getManga.await(searchResult.id) ?: return@forEach
@ -465,7 +465,7 @@ class MigrationListScreenModel(
fun migrateManga(mangaId: Long, copy: Boolean) { fun migrateManga(mangaId: Long, copy: Boolean) {
manualMigrations.value++ manualMigrations.value++
coroutineScope.launchIO { coroutineScope.launchIO {
val manga = migratingItems.value.find { it.manga.id == mangaId } val manga = migratingItems.value.orEmpty().find { it.manga.id == mangaId }
?: return@launchIO ?: return@launchIO
val toMangaObj = getManga.await((manga.searchResult.value as? SearchResult.Result)?.id ?: return@launchIO) val toMangaObj = getManga.await((manga.searchResult.value as? SearchResult.Result)?.id ?: return@launchIO)
@ -482,7 +482,7 @@ class MigrationListScreenModel(
fun removeManga(mangaId: Long) { fun removeManga(mangaId: Long) {
coroutineScope.launchIO { coroutineScope.launchIO {
val item = migratingItems.value.find { it.manga.id == mangaId } val item = migratingItems.value.orEmpty().find { it.manga.id == mangaId }
?: return@launchIO ?: return@launchIO
removeManga(item) removeManga(item)
item.migrationScope.cancel() item.migrationScope.cancel()
@ -496,14 +496,14 @@ class MigrationListScreenModel(
if (index > -1) { if (index > -1) {
ids.removeAt(index) ids.removeAt(index)
config.mangaIds = ids config.mangaIds = ids
val index2 = migratingItems.value.indexOf(item) val index2 = migratingItems.value.orEmpty().indexOf(item)
if (index2 > -1) migratingItems.value = migratingItems.value - item if (index2 > -1) migratingItems.value = migratingItems.value.orEmpty() - item
} }
} }
override fun onDispose() { override fun onDispose() {
super.onDispose() super.onDispose()
migratingItems.value.forEach { migratingItems.value.orEmpty().forEach {
it.migrationScope.cancel() it.migrationScope.cancel()
} }
} }
@ -513,7 +513,7 @@ class MigrationListScreenModel(
) { ) {
dialog.value = Dialog.MigrateMangaDialog( dialog.value = Dialog.MigrateMangaDialog(
copy, copy,
migratingItems.value.size, migratingItems.value.orEmpty().size,
mangasSkipped(), mangasSkipped(),
) )
} }