Improved loading speed of the cached gallery

This commit is contained in:
tom5079
2019-05-16 16:25:28 +09:00
parent 5e61912f75
commit 23bf93e960
15 changed files with 162 additions and 76 deletions

2
.idea/misc.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>

View File

@@ -41,6 +41,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "ru.noties.markwon:core:${markwonVersion}"
implementation 'com.shawnlin:number-picker:2.4.8'
implementation 'com.github.clans:fab:1.6.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test:runner:1.1.1'

View File

@@ -1,19 +1,23 @@
package xyz.quaver.pupil
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_reader.*
import kotlinx.android.synthetic.main.activity_reader.view.*
import kotlinx.android.synthetic.main.dialog_numberpicker.view.*
import kotlinx.coroutines.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.list
import xyz.quaver.hitomi.Reader
import xyz.quaver.hitomi.ReaderItem
import xyz.quaver.hitomi.getReader
import xyz.quaver.hitomi.getReferer
import xyz.quaver.pupil.adapters.ReaderAdapter
@@ -32,7 +36,10 @@ class ReaderActivity : AppCompatActivity() {
private lateinit var reader: Deferred<Reader>
private var loadJob: Job? = null
private lateinit var snapHelper: PagerSnapHelper
private var isScroll = true
private var isFullscreen = false
private val snapHelper = PagerSnapHelper()
private var menu: Menu? = null
@@ -48,44 +55,39 @@ class ReaderActivity : AppCompatActivity() {
supportActionBar?.title = intent.getStringExtra("GALLERY_TITLE")
galleryID = intent.getIntExtra("GALLERY_ID", 0)
CoroutineScope(Dispatchers.Unconfined).launch {
reader = async(Dispatchers.IO) {
val preference = PreferenceManager.getDefaultSharedPreferences(this@ReaderActivity)
if (preference.getBoolean("use_hiyobi", false)) {
try {
xyz.quaver.hiyobi.getReader(galleryID)
} catch (e: Exception) {
reader = CoroutineScope(Dispatchers.IO).async {
val json = Json(JsonConfiguration.Stable)
val serializer = ReaderItem.serializer().list
val preference = PreferenceManager.getDefaultSharedPreferences(this@ReaderActivity)
val isHiyobi = preference.getBoolean("use_hiyobi", false)
val cache = when {
isHiyobi -> File(cacheDir, "imageCache/$galleryID/reader-hiyobi.json")
else -> File(cacheDir, "imageCache/$galleryID/reader.json")
}
if (cache.exists())
json.parse(serializer, cache.readText())
else {
val reader = when {
isHiyobi -> {
try {
xyz.quaver.hiyobi.getReader(galleryID)
} catch (e: Exception) {
getReader(galleryID)
}
}
else -> {
getReader(galleryID)
}
}
getReader(galleryID)
cache.writeText(json.stringify(serializer, reader))
reader
}
}
snapHelper = PagerSnapHelper()
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
val attrs = window.attributes
if (preferences.getBoolean("reader_fullscreen", false)) {
attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
supportActionBar?.hide()
} else {
attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
supportActionBar?.show()
}
window.attributes = attrs
if (preferences.getBoolean("reader_one_by_one", false)) {
snapHelper.attachToRecyclerView(reader_recyclerview)
reader_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
} else {
snapHelper.attachToRecyclerView(null)
reader_recyclerview.layoutManager = LinearLayoutManager(this)
}
initView()
loadImages()
}
@@ -138,6 +140,21 @@ class ReaderActivity : AppCompatActivity() {
loadJob?.cancel()
}
override fun onBackPressed() {
if (isScroll and !isFullscreen)
super.onBackPressed()
if (isFullscreen) {
isFullscreen = false
fullscreen(false)
}
if (!isScroll) {
isScroll = true
scrollMode(true)
}
}
private fun initView() {
with(reader_recyclerview) {
adapter = ReaderAdapter(images)
@@ -155,45 +172,61 @@ class ReaderActivity : AppCompatActivity() {
}
})
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
ItemClickSupport.addTo(this)
.setOnItemClickListener { _, _, _ ->
val attrs = window.attributes
val fullscreen = preferences.getBoolean("reader_fullscreen", false)
if (isScroll) {
isScroll = false
isFullscreen = true
if (fullscreen) {
attrs.flags = attrs.flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
supportActionBar?.show()
scrollMode(false)
fullscreen(true)
} else {
attrs.flags = attrs.flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
supportActionBar?.hide()
val smoothScroller = object : LinearSmoothScroller(context) {
override fun getVerticalSnapPreference() = SNAP_TO_START
}.apply {
targetPosition = currentPage
}
(reader_recyclerview.layoutManager as LinearLayoutManager?)?.startSmoothScroll(smoothScroller)
}
window.attributes = attrs
preferences.edit().putBoolean("reader_fullscreen", !fullscreen).apply()
}.setOnItemLongClickListener { _, _, _ ->
val oneByOne = preferences.getBoolean("reader_one_by_one", false)
if (oneByOne) {
snapHelper.attachToRecyclerView(null)
reader_recyclerview.layoutManager = LinearLayoutManager(context)
}
else {
snapHelper.attachToRecyclerView(reader_recyclerview)
reader_recyclerview.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
}
(reader_recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(currentPage-1, 0)
preferences.edit().putBoolean("reader_one_by_one", !oneByOne).apply()
true
}
}
reader_fab_fullscreen.setOnClickListener {
isFullscreen = true
fullscreen(isFullscreen)
}
}
private fun fullscreen(isFullscreen: Boolean) {
with(window.attributes) {
if (isFullscreen) {
flags = flags or WindowManager.LayoutParams.FLAG_FULLSCREEN
supportActionBar?.hide()
this@ReaderActivity.reader_fab.visibility = View.INVISIBLE
} else {
flags = flags and WindowManager.LayoutParams.FLAG_FULLSCREEN.inv()
supportActionBar?.show()
this@ReaderActivity.reader_fab.visibility = View.VISIBLE
}
window.attributes = this
}
}
private fun scrollMode(isScroll: Boolean) {
if (isScroll) {
snapHelper.attachToRecyclerView(null)
reader_recyclerview.layoutManager = LinearLayoutManager(this)
} else {
snapHelper.attachToRecyclerView(reader_recyclerview)
reader_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
}
(reader_recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(currentPage-1, 0)
}
private fun loadImages() {
fun webpUrlFromUrl(url: URL) = URL(url.toString().replace("/galleries/", "/webp/") + ".webp")
fun webpUrlFromUrl(url: String) = url.replace("/galleries/", "/webp/") + ".webp"
loadJob = CoroutineScope(Dispatchers.Default).launch {
val reader = reader.await()
@@ -213,18 +246,18 @@ class ReaderActivity : AppCompatActivity() {
reader.chunked(8).forEach { chunked ->
chunked.map {
async(Dispatchers.IO) {
val url = if (it.second?.haswebp == 1) webpUrlFromUrl(it.first) else it.first
val url = if (it.galleryInfo?.haswebp == 1) webpUrlFromUrl(it.url) else it.url
val fileName: String
with(url.path) {
with(url) {
fileName = substring(lastIndexOf('/')+1)
}
val cache = File(cacheDir, "/imageCache/$galleryID/$fileName")
if (!cache.exists())
with(url.openConnection() as HttpsURLConnection) {
with(URL(url).openConnection() as HttpsURLConnection) {
setRequestProperty("Referer", getReferer(galleryID))
if (!cache.parentFile.exists())

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#FFFFFF">
<path
android:fillColor="#FF000000"
android:pathData="M7,14L5,14v5h5v-2L7,17v-3zM5,10h2L7,7h3L10,5L5,5v5zM17,17h-3v2h5v-5h-2v3zM14,5v2h3v3h2L19,5h-5z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 99 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 125 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 149 B

View File

@@ -0,0 +1,8 @@
<!-- drawable/github-circle.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M12,2A10,10 0 0,0 2,12C2,16.42 4.87,20.17 8.84,21.5C9.34,21.58 9.5,21.27 9.5,21C9.5,20.77 9.5,20.14 9.5,19.31C6.73,19.91 6.14,17.97 6.14,17.97C5.68,16.81 5.03,16.5 5.03,16.5C4.12,15.88 5.1,15.9 5.1,15.9C6.1,15.97 6.63,16.93 6.63,16.93C7.5,18.45 8.97,18 9.54,17.76C9.63,17.11 9.89,16.67 10.17,16.42C7.95,16.17 5.62,15.31 5.62,11.5C5.62,10.39 6,9.5 6.65,8.79C6.55,8.54 6.2,7.5 6.75,6.15C6.75,6.15 7.59,5.88 9.5,7.17C10.29,6.95 11.15,6.84 12,6.84C12.85,6.84 13.71,6.95 14.5,7.17C16.41,5.88 17.25,6.15 17.25,6.15C17.8,7.5 17.45,8.54 17.35,8.79C18,9.5 18.38,10.39 18.38,11.5C18.38,15.32 16.04,16.16 13.81,16.41C14.17,16.72 14.5,17.33 14.5,18.26C14.5,19.6 14.5,20.68 14.5,21C14.5,21.27 14.66,21.59 15.17,21.5C19.14,20.16 22,16.42 22,12A10,10 0 0,0 12,2Z" />
</vector>

View File

@@ -6,12 +6,18 @@
android:layout_height="match_parent"
tools:context=".ReaderActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/reader_recyclerview"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/reader_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
<FrameLayout
android:id="@+id/reader_framelayout"
@@ -26,5 +32,22 @@
android:layout_gravity="center"/>
</FrameLayout>
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/reader_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:menu_colorNormal="@color/colorAccent">
<com.github.clans.fab.FloatingActionButton
android:id="@+id/reader_fab_fullscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fullscreen"
app:fab_label="@string/reader_fab_fullscreen"
app:fab_size="mini"/>
</com.github.clans.fab.FloatingActionMenu>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@@ -43,4 +43,5 @@
<string name="main_drawer_group_contact_email">メールを送る</string>
<string name="help_dialog_title">準備中</string>
<string name="help_dialog_message">準備中です。</string>
<string name="reader_fab_fullscreen">フルスクリーン</string>
</resources>

View File

@@ -43,4 +43,5 @@
<string name="main_drawer_group_contact_title">문의</string>
<string name="help_dialog_title">준비 중</string>
<string name="help_dialog_message">준비중입니다.\n만화 화면에서 사진을 길게 누르면 스크롤 방식이 바뀝니다. 알고 계셨나요? :)</string>
<string name="reader_fab_fullscreen">전체 화면</string>
</resources>

View File

@@ -48,6 +48,7 @@
<string name="galleryblock_language">Language: %1$s</string>
<string name="reader_go_to_page">Go to page</string>
<string name="reader_fab_fullscreen">Fullscreen</string>
<string name="settings_title">Settings</string>
<string name="settings_search_title">Search Settings</string>

View File

@@ -16,7 +16,12 @@ data class GalleryInfo(
val name: String,
val height: Int
)
typealias Reader = List<Pair<URL, GalleryInfo?>>
@Serializable
data class ReaderItem(
val url: String,
val galleryInfo: GalleryInfo?
)
typealias Reader = List<ReaderItem>
//Set header `Referer` to reader url to avoid 403 error
fun getReader(galleryID: Int) : Reader {
val readerUrl = "https://hitomi.la/reader/$galleryID.html"
@@ -25,7 +30,7 @@ fun getReader(galleryID: Int) : Reader {
val doc = Jsoup.connect(readerUrl).get()
val images = doc.select(".img-url").map {
URL(protocol + urlFromURL(it.text()))
protocol + urlFromURL(it.text())
}
val galleryInfo = ArrayList<GalleryInfo?>()
@@ -42,5 +47,7 @@ fun getReader(galleryID: Int) : Reader {
if (images.size > galleryInfo.size)
galleryInfo.addAll(arrayOfNulls(images.size - galleryInfo.size))
return images zip galleryInfo
return (images zip galleryInfo).map {
ReaderItem(it.first, it.second)
}
}

View File

@@ -4,6 +4,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.json.content
import xyz.quaver.hitomi.Reader
import xyz.quaver.hitomi.ReaderItem
import java.net.URL
import javax.net.ssl.HttpsURLConnection
@@ -42,6 +43,6 @@ fun getReader(galleryId: Int) : Reader {
return json.jsonArray.map {
val name = it.jsonObject["name"]!!.content
Pair(URL("https://$hiyobi/data/$galleryId/$name"), null)
ReaderItem("https://$hiyobi/data/$galleryId/$name", null)
}
}