Skip to content

Commit ec2bc00

Browse files
committed
Improve performance of the manga reader
1 parent 155f3eb commit ec2bc00

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/main/kotlin/me/proxer/app/manga/MangaAdapter.kt

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import android.view.View
88
import android.view.ViewGroup
99
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
1010
import android.widget.ImageView
11-
import com.bumptech.glide.load.DecodeFormat
1211
import com.bumptech.glide.request.target.SimpleTarget
1312
import com.bumptech.glide.request.transition.Transition
1413
import com.davemorrissey.labs.subscaleview.ImageSource
1514
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
15+
import com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder
16+
import com.davemorrissey.labs.subscaleview.decoder.SkiaPooledImageRegionDecoder
1617
import com.mikepenz.community_material_typeface_library.CommunityMaterial
18+
import io.reactivex.Single
19+
import io.reactivex.android.schedulers.AndroidSchedulers
20+
import io.reactivex.schedulers.Schedulers
1721
import io.reactivex.subjects.PublishSubject
1822
import kotterknife.bindView
1923
import me.proxer.app.GlideRequests
@@ -23,6 +27,7 @@ import me.proxer.app.manga.MangaAdapter.ViewHolder
2327
import me.proxer.app.util.DeviceUtils
2428
import me.proxer.app.util.extension.decodedName
2529
import me.proxer.app.util.extension.setIconicsImage
30+
import me.proxer.app.util.extension.subscribeAndLogErrors
2631
import me.proxer.library.entity.manga.Page
2732
import me.proxer.library.util.ProxerUrls
2833
import java.io.File
@@ -69,8 +74,6 @@ class MangaAdapter(private val isVertical: Boolean) : BaseAdapter<Page, ViewHold
6974

7075
init {
7176
image.setDoubleTapZoomDuration(shortAnimationTime)
72-
image.setBitmapDecoderClass(RapidImageDecoder::class.java)
73-
image.setRegionDecoderClass(RapidImageRegionDecoder::class.java)
7477

7578
errorIndicator.setIconicsImage(CommunityMaterial.Icon.cmd_refresh, 64)
7679

@@ -103,36 +106,46 @@ class MangaAdapter(private val isVertical: Boolean) : BaseAdapter<Page, ViewHold
103106
image.maxScale = scale
104107
}
105108

109+
if (item.name.endsWith("png")) {
110+
image.setBitmapDecoderClass(RapidImageDecoder::class.java)
111+
image.setRegionDecoderClass(RapidImageRegionDecoder::class.java)
112+
} else {
113+
image.setBitmapDecoderClass(SkiaImageDecoder::class.java)
114+
image.setRegionDecoderClass(SkiaPooledImageRegionDecoder::class.java)
115+
}
116+
106117
errorIndicator.visibility = View.GONE
107118
image.visibility = View.VISIBLE
108119

109120
glide?.clear(glideTarget)
110121
glideTarget = GlideFileTarget()
111122

112123
glideTarget?.let { target ->
113-
glide?.download(ProxerUrls.mangaPageImage(server, entryId, id, item.decodedName).toString())
114-
?.format(when (DeviceUtils.shouldShowHighQualityImages(image.context)) {
115-
true -> DecodeFormat.PREFER_ARGB_8888
116-
false -> DecodeFormat.PREFER_RGB_565
117-
})
124+
glide
125+
?.download(ProxerUrls.mangaPageImage(server, entryId, id, item.decodedName).toString())
118126
?.into(target)
119127
}
120128
}
121129

122130
internal inner class GlideFileTarget : SimpleTarget<File>() {
123131

124132
override fun onResourceReady(resource: File, transition: Transition<in File>?) {
125-
image.setImage(ImageSource.uri(resource.path))
126-
image.setScaleAndCenter(0.2f, PointF(0f, 0f))
127-
128-
// Fade animations do not look good with the horizontal reader.
129-
if (isVertical) {
130-
image.apply { alpha = 0.2f }
131-
.animate()
132-
.alpha(1.0f)
133-
.setDuration(mediumAnimationTime.toLong())
134-
.start()
135-
}
133+
Single.fromCallable { ImageSource.uri(resource.path) }
134+
.subscribeOn(Schedulers.io())
135+
.observeOn(AndroidSchedulers.mainThread())
136+
.subscribeAndLogErrors { source ->
137+
image.setImage(source)
138+
image.setScaleAndCenter(0.2f, PointF(0f, 0f))
139+
140+
// Fade animations do not look good with the horizontal reader.
141+
if (isVertical) {
142+
image.apply { alpha = 0.2f }
143+
.animate()
144+
.alpha(1.0f)
145+
.setDuration(mediumAnimationTime.toLong())
146+
.start()
147+
}
148+
}
136149
}
137150

138151
override fun onLoadFailed(errorDrawable: Drawable?) {

0 commit comments

Comments
 (0)