Replace history with remote manga when delete local

This commit is contained in:
Koitharu
2020-03-28 12:46:57 +02:00
parent 2c66edda68
commit e7a150bd9a
4 changed files with 25 additions and 2 deletions

View File

@@ -105,6 +105,16 @@ class LocalMangaRepository : MangaRepository, KoinComponent {
}
}
fun getRemoteManga(localManga: Manga): Manga? {
val file = safe {
Uri.parse(localManga.url).toFile()
} ?: return null
val zip = ZipFile(file)
val entry = zip.getEntry(MangaZip.INDEX_ENTRY)
val index = entry?.let(zip::readText)?.let(::MangaIndex) ?: return null
return index.getMangaInfo()
}
private fun zipUri(file: File, entryName: String) =
Uri.fromParts("cbz", file.path, entryName).toString()

View File

@@ -58,6 +58,17 @@ class HistoryRepository : KoinComponent {
notifyHistoryChanged()
}
/**
* Try to replace one manga with another one
* Useful for replacing saved manga on deleting it with remove source
*/
suspend fun deleteOrSwap(manga: Manga, alternative: Manga?) {
if (alternative == null || db.mangaDao().update(MangaEntity.from(alternative)) <= 0) {
db.historyDao().delete(manga.id)
notifyHistoryChanged()
}
}
companion object {
private val listeners = HashSet<OnHistoryChangeListener>()

View File

@@ -94,9 +94,10 @@ class MangaDetailsPresenter private constructor() : BasePresenter<MangaDetailsVi
withContext(Dispatchers.IO) {
val repository =
MangaProviderFactory.create(MangaSource.LOCAL) as LocalMangaRepository
val original = repository.getRemoteManga(manga)
repository.delete(manga) || throw IOException("Unable to delete file")
safe {
HistoryRepository().delete(manga)
HistoryRepository().deleteOrSwap(manga, original)
}
}
viewState.onMangaRemoved(manga)

View File

@@ -91,9 +91,10 @@ class LocalListPresenter : BasePresenter<MangaListView<File>>() {
presenterScope.launch {
try {
withContext(Dispatchers.IO) {
val original = repository.getRemoteManga(manga)
repository.delete(manga) || throw IOException("Unable to delete file")
safe {
HistoryRepository().delete(manga)
HistoryRepository().deleteOrSwap(manga, original)
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {