Replace history with remote manga when delete local
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -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>()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user