Skip to content

Commit

Permalink
Improve performance of the manga reader
Browse files Browse the repository at this point in the history
  • Loading branch information
rubengees committed Mar 30, 2018
1 parent 155f3eb commit ec2bc00
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/main/kotlin/me/proxer/app/manga/MangaAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.widget.ImageView
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.davemorrissey.labs.subscaleview.decoder.SkiaImageDecoder
import com.davemorrissey.labs.subscaleview.decoder.SkiaPooledImageRegionDecoder
import com.mikepenz.community_material_typeface_library.CommunityMaterial
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.reactivex.subjects.PublishSubject
import kotterknife.bindView
import me.proxer.app.GlideRequests
Expand All @@ -23,6 +27,7 @@ import me.proxer.app.manga.MangaAdapter.ViewHolder
import me.proxer.app.util.DeviceUtils
import me.proxer.app.util.extension.decodedName
import me.proxer.app.util.extension.setIconicsImage
import me.proxer.app.util.extension.subscribeAndLogErrors
import me.proxer.library.entity.manga.Page
import me.proxer.library.util.ProxerUrls
import java.io.File
Expand Down Expand Up @@ -69,8 +74,6 @@ class MangaAdapter(private val isVertical: Boolean) : BaseAdapter<Page, ViewHold

init {
image.setDoubleTapZoomDuration(shortAnimationTime)
image.setBitmapDecoderClass(RapidImageDecoder::class.java)
image.setRegionDecoderClass(RapidImageRegionDecoder::class.java)

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

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

if (item.name.endsWith("png")) {
image.setBitmapDecoderClass(RapidImageDecoder::class.java)
image.setRegionDecoderClass(RapidImageRegionDecoder::class.java)
} else {
image.setBitmapDecoderClass(SkiaImageDecoder::class.java)
image.setRegionDecoderClass(SkiaPooledImageRegionDecoder::class.java)
}

errorIndicator.visibility = View.GONE
image.visibility = View.VISIBLE

glide?.clear(glideTarget)
glideTarget = GlideFileTarget()

glideTarget?.let { target ->
glide?.download(ProxerUrls.mangaPageImage(server, entryId, id, item.decodedName).toString())
?.format(when (DeviceUtils.shouldShowHighQualityImages(image.context)) {
true -> DecodeFormat.PREFER_ARGB_8888
false -> DecodeFormat.PREFER_RGB_565
})
glide
?.download(ProxerUrls.mangaPageImage(server, entryId, id, item.decodedName).toString())
?.into(target)
}
}

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

override fun onResourceReady(resource: File, transition: Transition<in File>?) {
image.setImage(ImageSource.uri(resource.path))
image.setScaleAndCenter(0.2f, PointF(0f, 0f))

// Fade animations do not look good with the horizontal reader.
if (isVertical) {
image.apply { alpha = 0.2f }
.animate()
.alpha(1.0f)
.setDuration(mediumAnimationTime.toLong())
.start()
}
Single.fromCallable { ImageSource.uri(resource.path) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeAndLogErrors { source ->
image.setImage(source)
image.setScaleAndCenter(0.2f, PointF(0f, 0f))

// Fade animations do not look good with the horizontal reader.
if (isVertical) {
image.apply { alpha = 0.2f }
.animate()
.alpha(1.0f)
.setDuration(mediumAnimationTime.toLong())
.start()
}
}
}

override fun onLoadFailed(errorDrawable: Drawable?) {
Expand Down

0 comments on commit ec2bc00

Please sign in to comment.