From 332e8c9487b2e5e39a3a90705b2def80183bcc04 Mon Sep 17 00:00:00 2001 From: Jay Date: Sat, 4 Jan 2020 22:19:47 -0800 Subject: [PATCH] More refactoring + more bug fixes Such as when a manga picked has 0 chapters --- .../migration/manga/process/MigratingManga.kt | 10 ++++ .../manga/process/MigrationListController.kt | 46 +++++++++++++------ .../manga/process/MigrationProcessAdapter.kt | 8 ++-- .../manga/process/MigrationProcessHolder.kt | 10 ++-- .../manga/process/MigrationProcessItem.kt | 7 --- app/src/main/res/values/strings.xml | 3 ++ 6 files changed, 51 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigratingManga.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigratingManga.kt index 1294afdb56..97787b801f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigratingManga.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigratingManga.kt @@ -21,6 +21,8 @@ class MigratingManga(private val db: DatabaseHelper, val migrationJob = parentContext + SupervisorJob() + Dispatchers.Default + var migrationStatus:Int = MigrationStatus.RUNNUNG + @Volatile private var manga: Manga? = null suspend fun manga(): Manga? { @@ -36,4 +38,12 @@ class MigratingManga(private val db: DatabaseHelper, // Create the model object. return MigrationProcessItem(this) } +} + +class MigrationStatus { + companion object { + val RUNNUNG = 0 + val MANGA_FOUND = 1 + val MANGA_NOT_FOUND = 2 + } } \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationListController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationListController.kt index 30642cf234..912fbecbd2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationListController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationListController.kt @@ -9,6 +9,7 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.core.graphics.ColorUtils import androidx.recyclerview.widget.LinearLayoutManager import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat @@ -233,6 +234,9 @@ class MigrationListController(bundle: Bundle? = null) : BaseController(bundle), } } + manga.migrationStatus = if (result == null) MigrationStatus.MANGA_NOT_FOUND else + MigrationStatus.MANGA_FOUND + adapter?.sourceFinished() manga.searchResult.initialize(result?.id) } } @@ -281,34 +285,46 @@ class MigrationListController(bundle: Bundle? = null) : BaseController(bundle), fun useMangaForMigration(manga: Manga, source: Source) { val firstIndex = selectedPosition ?: return val migratingManga = adapter?.getItem(firstIndex) ?: return - migratingManga.showSpinner() + migratingManga.manga.migrationStatus = MigrationStatus.RUNNUNG + adapter?.notifyItemChanged(firstIndex) launchUI { val result = CoroutineScope(migratingManga.manga.migrationJob).async { val localManga = smartSearchEngine.networkToLocalManga(manga, source.id) val chapters = source.fetchChapterList(localManga).toSingle().await( Schedulers.io() ) - withContext(Dispatchers.IO) { + try { syncChaptersWithSource(db, chapters, localManga, source) } + catch (e: Exception) { + return@async null + } localManga }.await() - try { - val newManga = - sourceManager.getOrStub(result.source).fetchMangaDetails(result).toSingle() - .await() - result.copyFrom(newManga) + if (result != null) { + try { + val newManga = + sourceManager.getOrStub(result.source).fetchMangaDetails(result).toSingle() + .await() + result.copyFrom(newManga) - db.insertManga(result).executeAsBlocking() - } catch (e: CancellationException) { - // Ignore cancellations - throw e - } catch (e: Exception) { + db.insertManga(result).executeAsBlocking() + } catch (e: CancellationException) { + // Ignore cancellations + throw e + } catch (e: Exception) { + } + + migratingManga.manga.migrationStatus = MigrationStatus.MANGA_FOUND + migratingManga.manga.searchResult.set(result.id) + adapter?.notifyDataSetChanged() + } + else { + migratingManga.manga.migrationStatus = MigrationStatus.MANGA_NOT_FOUND + activity?.toast(R.string.error_fetching_migration, Toast.LENGTH_LONG) + adapter?.notifyDataSetChanged() } - - migratingManga.manga.searchResult.set(result.id) - adapter?.notifyDataSetChanged() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessAdapter.kt index d362cec4ac..c4b4c4e14c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessAdapter.kt @@ -45,12 +45,10 @@ class MigrationProcessAdapter( if (allMangasDone()) menuItemListener.enableButtons() } - fun allMangasDone() = (items.all { it.manga.searchResult.initialized || !it.manga.migrationJob - .isActive } && items.any { it.manga - .searchResult.content != null }) + fun allMangasDone() = (items.all { it.manga.migrationStatus != MigrationStatus + .RUNNUNG } && items.any { it.manga.migrationStatus == MigrationStatus.MANGA_FOUND }) - fun mangasSkipped() = (items.count { (!it.manga.searchResult.initialized || it.manga - .searchResult.content == null) }) + fun mangasSkipped() = (items.count { it.manga.migrationStatus == MigrationStatus.MANGA_NOT_FOUND }) suspend fun performMigrations(copy: Boolean) { withContext(Dispatchers.IO) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessHolder.kt index d0254ae32f..4f98e0d635 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessHolder.kt @@ -87,7 +87,8 @@ class MigrationProcessHolder( sourceManager.get(it) } withContext(Dispatchers.Main) { - if (item.manga.mangaId != this@MigrationProcessHolder.item?.manga?.mangaId) { + if (item.manga.mangaId != this@MigrationProcessHolder.item?.manga?.mangaId || + item.manga.migrationStatus == MigrationStatus.RUNNUNG) { return@withContext } if (searchResult != null && resultSource != null) { @@ -101,7 +102,8 @@ class MigrationProcessHolder( } } else { migration_manga_card_to.loading_group.gone() - migration_manga_card_to.title.text = "No Alternatives Found" + migration_manga_card_to.title.text = view.context.applicationContext + .getString(R.string.no_alternatives_found) } migration_menu.visible() skip_manga.gone() @@ -111,10 +113,6 @@ class MigrationProcessHolder( } } - fun showSpinner() { - migration_manga_card_to.loading_group.visible() - } - private fun View.resetManga() { loading_group.visible() thumbnail.setImageDrawable(null) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessItem.kt index 28c171872d..70b0ebdcf7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/migration/manga/process/MigrationProcessItem.kt @@ -10,7 +10,6 @@ import eu.kanade.tachiyomi.R class MigrationProcessItem(val manga: MigratingManga) : AbstractFlexibleItem() { - var holder:MigrationProcessHolder? = null override fun getLayoutRes(): Int { return R.layout.migration_process_item } @@ -23,8 +22,6 @@ class MigrationProcessItem(val manga: MigratingManga) : holder: MigrationProcessHolder, position: Int, payloads: MutableList?) { - - this.holder = holder holder.bind(this) } @@ -36,10 +33,6 @@ class MigrationProcessItem(val manga: MigratingManga) : return false } - fun showSpinner() { - holder?.showSpinner() - } - override fun hashCode(): Int { return manga.mangaId.toInt() } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e7cb12d225..c90628cffd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -426,6 +426,9 @@ Copy %1$d%2$s mangas? (skipping %1$d) No manga migrated + No chapters found, this manga cannot be used for + migration + No Alternatives Found Tracking