Fixes and refactor

This commit is contained in:
Koitharu
2021-01-17 18:30:14 +02:00
parent a242aa6633
commit 8f8d85d172
41 changed files with 119 additions and 127 deletions

View File

@@ -9,7 +9,6 @@ import org.koitharu.kotatsu.core.model.MangaSource
import org.koitharu.kotatsu.core.model.MangaTag
import org.koitharu.kotatsu.utils.ext.getStringOrNull
import org.koitharu.kotatsu.utils.ext.mapToSet
import org.koitharu.kotatsu.utils.ext.safe
class MangaIndex(source: String?) {
@@ -40,7 +39,7 @@ class MangaIndex(source: String?) {
json.put("app_version", BuildConfig.VERSION_CODE)
}
fun getMangaInfo(): Manga? = if (json.length() == 0) null else safe {
fun getMangaInfo(): Manga? = if (json.length() == 0) null else runCatching {
val source = MangaSource.valueOf(json.getString("source"))
Manga(
id = json.getLong("id"),
@@ -60,7 +59,7 @@ class MangaIndex(source: String?) {
},
chapters = getChapters(json.getJSONObject("chapters"), source)
)
}
}.getOrNull()
fun getCoverEntry(): String? = json.optString("cover_entry")

View File

@@ -15,7 +15,6 @@ import org.koitharu.kotatsu.local.data.MangaZip
import org.koitharu.kotatsu.utils.AlphanumComparator
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.readText
import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.sub
import java.io.File
import java.util.*
@@ -37,7 +36,7 @@ class LocalMangaRepository(private val context: Context) : MangaRepository {
}
val files = getAvailableStorageDirs(context)
.flatMap { x -> x.listFiles(filenameFilter)?.toList().orEmpty() }
return files.mapNotNull { x -> safe { getFromFile(x) } }
return files.mapNotNull { x -> runCatching { getFromFile(x) }.getOrNull() }
}
override suspend fun getDetails(manga: Manga) = if (manga.chapters == null) {
@@ -128,9 +127,9 @@ class LocalMangaRepository(private val context: Context) : MangaRepository {
}
fun getRemoteManga(localManga: Manga): Manga? {
val file = safe {
val file = runCatching {
Uri.parse(localManga.url).toFile()
} ?: return null
}.getOrNull() ?: return null
val zip = ZipFile(file)
val entry = zip.getEntry(MangaZip.INDEX_ENTRY)
val index = entry?.let(zip::readText)?.let(::MangaIndex) ?: return null

View File

@@ -22,7 +22,6 @@ import org.koitharu.kotatsu.local.domain.LocalMangaRepository
import org.koitharu.kotatsu.utils.MediaStoreCompat
import org.koitharu.kotatsu.utils.SingleLiveEvent
import org.koitharu.kotatsu.utils.ext.asLiveDataDistinct
import org.koitharu.kotatsu.utils.ext.safe
import org.koitharu.kotatsu.utils.ext.sub
import java.io.IOException
@@ -49,7 +48,10 @@ class LocalListViewModel(
list.isEmpty() -> listOf(EmptyState(R.string.text_local_holder))
else -> list.toUi(mode)
}
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, listOf(LoadingState))
}.asLiveDataDistinct(
viewModelScope.coroutineContext + Dispatchers.Default,
listOf(LoadingState)
)
init {
onRefresh()
@@ -94,7 +96,7 @@ class LocalListViewModel(
withContext(Dispatchers.Default) {
val original = repository.getRemoteManga(manga)
repository.delete(manga) || throw IOException("Unable to delete file")
safe {
runCatching {
historyRepository.deleteOrSwap(manga, original)
}
}