Bug fix
Networking optimized
This commit is contained in:
@@ -23,11 +23,15 @@ import android.content.ContextWrapper
|
||||
import android.util.Base64
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.preference.PreferenceManager
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.serialization.ImplicitReflectionSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.parse
|
||||
import kotlinx.serialization.stringify
|
||||
import xyz.quaver.Code
|
||||
import xyz.quaver.hitomi.GalleryBlock
|
||||
import xyz.quaver.hitomi.Reader
|
||||
import xyz.quaver.pupil.util.*
|
||||
@@ -107,17 +111,21 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
suspend fun getGalleryBlock(galleryID: Int): GalleryBlock? {
|
||||
val metadata = Cache(this).getCachedMetadata(galleryID)
|
||||
|
||||
val source = mapOf(
|
||||
Code.HITOMI to { xyz.quaver.hitomi.getGalleryBlock(galleryID) },
|
||||
Code.HIYOBI to { xyz.quaver.hiyobi.getGalleryBlock(galleryID) }
|
||||
)
|
||||
|
||||
val galleryBlock = if (metadata?.galleryBlock == null)
|
||||
listOf(
|
||||
{ xyz.quaver.hitomi.getGalleryBlock(galleryID) },
|
||||
{ xyz.quaver.hiyobi.getGalleryBlock(galleryID) }
|
||||
).map {
|
||||
source.entries.map {
|
||||
CoroutineScope(Dispatchers.IO).async {
|
||||
kotlin.runCatching {
|
||||
it.invoke()
|
||||
it.value.invoke()
|
||||
}.getOrNull()
|
||||
}
|
||||
}.awaitAll().filterNotNull()
|
||||
}.firstOrNull {
|
||||
it.await() != null
|
||||
}?.await()
|
||||
else
|
||||
metadata.galleryBlock
|
||||
|
||||
@@ -126,52 +134,56 @@ class Cache(context: Context) : ContextWrapper(context) {
|
||||
Metadata(Cache(this).getCachedMetadata(galleryID), galleryBlock = galleryBlock)
|
||||
)
|
||||
|
||||
val mirrors = preference.getString("mirrors", "")!!.split('>')
|
||||
|
||||
return galleryBlock.firstOrNull {
|
||||
mirrors.contains(it.code.name)
|
||||
} ?: galleryBlock.firstOrNull()
|
||||
return galleryBlock
|
||||
}
|
||||
|
||||
fun getReaderOrNull(galleryID: Int): Reader? {
|
||||
val metadata = getCachedMetadata(galleryID)
|
||||
|
||||
val mirrors = preference.getString("mirrors", "")!!.split('>')
|
||||
|
||||
return metadata?.readers?.firstOrNull {
|
||||
mirrors.contains(it.code.name)
|
||||
} ?: metadata?.readers?.firstOrNull()
|
||||
return getCachedMetadata(galleryID)?.reader
|
||||
}
|
||||
|
||||
suspend fun getReader(galleryID: Int): Reader? {
|
||||
val metadata = getCachedMetadata(galleryID)
|
||||
val mirrors = preference.getString("mirrors", null)?.split('>') ?: listOf()
|
||||
|
||||
val readers = if (metadata?.readers == null) {
|
||||
listOf(
|
||||
{ xyz.quaver.hitomi.getReader(galleryID) },
|
||||
{ xyz.quaver.hiyobi.getReader(galleryID) }
|
||||
).map {
|
||||
CoroutineScope(Dispatchers.IO).async {
|
||||
kotlin.runCatching {
|
||||
it.invoke()
|
||||
}.getOrNull()
|
||||
}
|
||||
}.awaitAll().filterNotNull()
|
||||
} else {
|
||||
metadata.readers
|
||||
val sources = mapOf(
|
||||
Code.HITOMI to { xyz.quaver.hitomi.getReader(galleryID) },
|
||||
Code.HIYOBI to { xyz.quaver.hiyobi.getReader(galleryID) }
|
||||
).let {
|
||||
if (mirrors.isNotEmpty())
|
||||
it.toSortedMap(
|
||||
Comparator { o1, o2 ->
|
||||
mirrors.indexOf(o1.name) - mirrors.indexOf(o2.name)
|
||||
}
|
||||
)
|
||||
else
|
||||
it
|
||||
}
|
||||
|
||||
if (readers.isNotEmpty())
|
||||
val reader = if (metadata?.reader == null) {
|
||||
CoroutineScope(Dispatchers.IO).async {
|
||||
var retval: Reader? = null
|
||||
|
||||
for (source in sources) {
|
||||
retval = kotlin.runCatching {
|
||||
source.value.invoke()
|
||||
}.getOrNull()
|
||||
|
||||
if (retval != null)
|
||||
break
|
||||
}
|
||||
|
||||
retval
|
||||
}.await()
|
||||
} else
|
||||
metadata.reader
|
||||
|
||||
if (reader != null)
|
||||
setCachedMetadata(
|
||||
galleryID,
|
||||
Metadata(Cache(this).getCachedMetadata(galleryID), readers = readers)
|
||||
Metadata(Cache(this).getCachedMetadata(galleryID), readers = reader)
|
||||
)
|
||||
|
||||
val mirrors = preference.getString("mirrors", "")!!.split('>')
|
||||
|
||||
return readers.firstOrNull {
|
||||
mirrors.contains(it.code.name)
|
||||
} ?: readers.firstOrNull()
|
||||
return reader
|
||||
}
|
||||
|
||||
fun getImages(galleryID: Int): List<DocumentFile?>? {
|
||||
|
||||
@@ -279,6 +279,9 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
|
||||
progress.put(galleryID, reader.galleryInfo.map { 0F }.toMutableList())
|
||||
exception.put(galleryID, reader.galleryInfo.map { null }.toMutableList())
|
||||
|
||||
if (notification[galleryID] == null)
|
||||
initNotification(galleryID)
|
||||
|
||||
notification[galleryID].setContentTitle(reader.title)
|
||||
notify(galleryID)
|
||||
|
||||
|
||||
@@ -25,20 +25,20 @@ import xyz.quaver.hitomi.Reader
|
||||
@Serializable
|
||||
data class Metadata(
|
||||
val thumbnail: String? = null,
|
||||
val galleryBlock: List<GalleryBlock>? = null,
|
||||
val readers: List<Reader>? = null,
|
||||
val galleryBlock: GalleryBlock? = null,
|
||||
val reader: Reader? = null,
|
||||
val isDownloading: Boolean? = null
|
||||
) {
|
||||
constructor(
|
||||
metadata: Metadata?,
|
||||
thumbnail: String? = null,
|
||||
galleryBlock: List<GalleryBlock>? = null,
|
||||
readers: List<Reader>? = null,
|
||||
galleryBlock: GalleryBlock? = null,
|
||||
readers: Reader? = null,
|
||||
isDownloading: Boolean? = null
|
||||
) : this(
|
||||
thumbnail ?: metadata?.thumbnail,
|
||||
galleryBlock ?: metadata?.galleryBlock,
|
||||
readers ?: metadata?.readers,
|
||||
readers ?: metadata?.reader,
|
||||
isDownloading ?: metadata?.isDownloading
|
||||
)
|
||||
}
|
||||
@@ -20,6 +20,7 @@ package xyz.quaver.pupil.util
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import androidx.preference.PreferenceManager
|
||||
import java.io.File
|
||||
@@ -33,7 +34,10 @@ fun getCachedGallery(context: Context, galleryID: Int) =
|
||||
|
||||
fun getDownloadDirectory(context: Context) : DocumentFile? {
|
||||
val uri = PreferenceManager.getDefaultSharedPreferences(context).getString("dl_location", null).let {
|
||||
Uri.parse(it)
|
||||
if (it != null)
|
||||
Uri.parse(it)
|
||||
else
|
||||
return null
|
||||
}
|
||||
|
||||
return if (uri.toString().startsWith("file"))
|
||||
@@ -42,6 +46,12 @@ fun getDownloadDirectory(context: Context) : DocumentFile? {
|
||||
DocumentFile.fromTreeUri(context, uri)
|
||||
}
|
||||
|
||||
fun convertUpdateUri(context: Context, uri: Uri) : Uri =
|
||||
if (uri.toString().startsWith("file"))
|
||||
FileProvider.getUriForFile(context, context.applicationContext.packageName + ".provider", File(uri.path!!.substringAfter("file:///")))
|
||||
else
|
||||
uri
|
||||
|
||||
fun URL.download(context: Context, to: DocumentFile, onDownloadProgress: ((Long, Long) -> Unit)? = null) {
|
||||
context.contentResolver.openOutputStream(to.uri).use { out ->
|
||||
out!!
|
||||
|
||||
@@ -168,7 +168,7 @@ fun checkUpdate(context: AppCompatActivity, force: Boolean = false) {
|
||||
|
||||
val install = Intent(Intent.ACTION_VIEW).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
setDataAndType(target.uri, MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk"))
|
||||
setDataAndType(convertUpdateUri(context, target.uri), MimeTypeMap.getSingleton().getMimeTypeFromExtension("apk"))
|
||||
}
|
||||
|
||||
builder.apply {
|
||||
|
||||
Reference in New Issue
Block a user