Minor cleanup in ImageUtil and SourceFilterPresenter (#7106)

* Minor cleanup in `ImageUtil` and `SourceFilterPresenter`

* Fix missed stuff
This commit is contained in:
FourTOne5 2022-05-14 21:34:15 +06:00 committed by GitHub
parent 6d011ebe32
commit 71bb8ed975
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 18 deletions

View file

@ -392,7 +392,10 @@ class Downloader(
return pageObservable return pageObservable
// When the page is ready, set page path, progress (just in case) and status // When the page is ready, set page path, progress (just in case) and status
.doOnNext { file -> .doOnNext { file ->
splitTallImageIfNeeded(page, tmpDir) val success = splitTallImageIfNeeded(page, tmpDir)
if (success.not()) {
notifier.onError(context.getString(R.string.download_notifier_split_failed), download.chapter.name, download.manga.title)
}
page.uri = file.uri page.uri = file.uri
page.progress = 100 page.progress = 100
download.downloadedImages++ download.downloadedImages++
@ -477,8 +480,8 @@ class Downloader(
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg" return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg"
} }
private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile) { private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean {
if (!preferences.splitTallImages().get()) return if (!preferences.splitTallImages().get()) return true
val filename = String.format("%03d", page.number) val filename = String.format("%03d", page.number)
val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) } val imageFile = tmpDir.listFiles()?.find { it.name!!.startsWith(filename) }
@ -487,8 +490,9 @@ class Downloader(
?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number)) ?: throw Error(context.getString(R.string.download_notifier_split_page_path_not_found, page.number))
// check if the original page was previously splitted before then skip. // check if the original page was previously splitted before then skip.
if (imageFile.name!!.contains("__")) return if (imageFile.name!!.contains("__")) return true
ImageUtil.splitTallImage(imageFile, imageFilePath)
return ImageUtil.splitTallImage(imageFile, imageFilePath)
} }
/** /**

View file

@ -33,15 +33,12 @@ class SourceFilterPresenter(
.catch { exception -> .catch { exception ->
_state.value = SourceFilterState.Error(exception) _state.value = SourceFilterState.Error(exception)
} }
.collectLatest { sourceLangMap -> .collectLatest(::collectLatestSourceLangMap)
val uiModels = sourceLangMap.toFilterUiModels()
_state.value = SourceFilterState.Success(uiModels)
}
} }
} }
private fun Map<String, List<Source>>.toFilterUiModels(): List<FilterUiModel> { private fun collectLatestSourceLangMap(sourceLangMap: Map<String, List<Source>>) {
return this.flatMap { val uiModels = sourceLangMap.flatMap {
val isLangEnabled = it.key in preferences.enabledLanguages().get() val isLangEnabled = it.key in preferences.enabledLanguages().get()
val header = listOf(FilterUiModel.Header(it.key, isLangEnabled)) val header = listOf(FilterUiModel.Header(it.key, isLangEnabled))
@ -53,6 +50,7 @@ class SourceFilterPresenter(
) )
} }
} }
_state.value = SourceFilterState.Success(uiModels)
} }
fun toggleSource(source: Source) { fun toggleSource(source: Source) {

View file

@ -19,6 +19,7 @@ import androidx.core.graphics.createBitmap
import androidx.core.graphics.green import androidx.core.graphics.green
import androidx.core.graphics.red import androidx.core.graphics.red
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import logcat.LogPriority
import tachiyomi.decoder.Format import tachiyomi.decoder.Format
import tachiyomi.decoder.ImageDecoder import tachiyomi.decoder.ImageDecoder
import java.io.BufferedInputStream import java.io.BufferedInputStream
@ -182,7 +183,7 @@ object ImageUtil {
* *
* @return true if the height:width ratio is greater than 3. * @return true if the height:width ratio is greater than 3.
*/ */
fun isTallImage(imageStream: InputStream): Boolean { private fun isTallImage(imageStream: InputStream): Boolean {
val options = extractImageOptions(imageStream, false) val options = extractImageOptions(imageStream, false)
return (options.outHeight / options.outWidth) > 3 return (options.outHeight / options.outWidth) > 3
} }
@ -190,9 +191,9 @@ object ImageUtil {
/** /**
* Splits tall images to improve performance of reader * Splits tall images to improve performance of reader
*/ */
fun splitTallImage(imageFile: UniFile, imageFilePath: String) { fun splitTallImage(imageFile: UniFile, imageFilePath: String): Boolean {
if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) { if (isAnimatedAndSupported(imageFile.openInputStream()) || !isTallImage(imageFile.openInputStream())) {
return return true
} }
val options = extractImageOptions(imageFile.openInputStream(), false).apply { inJustDecodeBounds = false } val options = extractImageOptions(imageFile.openInputStream(), false).apply { inJustDecodeBounds = false }
@ -213,6 +214,11 @@ object ImageUtil {
BitmapRegionDecoder.newInstance(imageFile.openInputStream(), false) BitmapRegionDecoder.newInstance(imageFile.openInputStream(), false)
} }
if (bitmapRegionDecoder == null) {
logcat { "Failed to create new instance of BitmapRegionDecoder" }
return false
}
try { try {
(0 until partCount).forEach { splitIndex -> (0 until partCount).forEach { splitIndex ->
val splitPath = imageFilePath.substringBeforeLast(".") + "__${"%03d".format(splitIndex + 1)}.jpg" val splitPath = imageFilePath.substringBeforeLast(".") + "__${"%03d".format(splitIndex + 1)}.jpg"
@ -225,19 +231,21 @@ object ImageUtil {
val region = Rect(0, topOffset, imageWidth, bottomOffset) val region = Rect(0, topOffset, imageWidth, bottomOffset)
FileOutputStream(splitPath).use { outputStream -> FileOutputStream(splitPath).use { outputStream ->
val splitBitmap = bitmapRegionDecoder!!.decodeRegion(region, options) val splitBitmap = bitmapRegionDecoder.decodeRegion(region, options)
splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) splitBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
} }
} }
imageFile.delete() imageFile.delete()
return true
} catch (e: Exception) { } catch (e: Exception) {
// Image splits were not successfully saved so delete them and keep the original image // Image splits were not successfully saved so delete them and keep the original image
(0 until partCount) (0 until partCount)
.map { imageFile.filePath!!.substringBeforeLast(".") + "__${"%03d".format(it + 1)}.jpg" } .map { imageFilePath.substringBeforeLast(".") + "__${"%03d".format(it + 1)}.jpg" }
.forEach { File(it).delete() } .forEach { File(it).delete() }
throw e logcat(LogPriority.ERROR, e)
return false
} finally { } finally {
bitmapRegionDecoder?.recycle() bitmapRegionDecoder.recycle()
} }
} }

View file

@ -820,6 +820,7 @@
<string name="download_notifier_download_finish">Download completed</string> <string name="download_notifier_download_finish">Download completed</string>
<string name="download_notifier_split_page_not_found">Page %d not found while splitting</string> <string name="download_notifier_split_page_not_found">Page %d not found while splitting</string>
<string name="download_notifier_split_page_path_not_found">Couldn\'t find file path of page %d</string> <string name="download_notifier_split_page_path_not_found">Couldn\'t find file path of page %d</string>
<string name="download_notifier_split_failed">Couldn\'t split downloaded image</string>
<!-- Notification channels --> <!-- Notification channels -->
<string name="channel_common">Common</string> <string name="channel_common">Common</string>