Skip to content

Commit

Permalink
Download Bug fix
Browse files Browse the repository at this point in the history
Added favorite to TagChip
Improved eyeblink recognition
  • Loading branch information
tom5079 committed Sep 26, 2020
1 parent fca42c7 commit a973cdf
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 55 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ dependencies {
exclude group: 'org.jetbrains.kotlinx', module: 'kotlinx-serialization-core-jvm'
}
implementation "xyz.quaver:documentfilex:0.2.15"
implementation "xyz.quaver:floatingsearchview:1.0.3"
implementation "xyz.quaver:floatingsearchview:1.0.4"
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test:rules:1.3.0'
Expand Down
Binary file modified app/libs/recyclerviewfastscroller-release.aar
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"type": "SINGLE",
"filters": [],
"properties": [],
"versionCode": 59,
"versionName": "5.0.3-hotfix2",
"versionCode": 60,
"versionName": "5.1",
"enabled": true,
"outputFile": "app-release.apk"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.cardview.widget.CardView
import androidx.core.view.children
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.CircularProgressDrawable
import androidx.vectordrawable.graphics.drawable.Animatable2Compat
Expand All @@ -48,6 +49,7 @@ import xyz.quaver.hitomi.getReader
import xyz.quaver.io.util.getChild
import xyz.quaver.pupil.BuildConfig
import xyz.quaver.pupil.R
import xyz.quaver.pupil.favoriteTags
import xyz.quaver.pupil.favorites
import xyz.quaver.pupil.types.Tag
import xyz.quaver.pupil.ui.view.TagChip
Expand Down Expand Up @@ -222,7 +224,18 @@ class GalleryBlockAdapter(private val glide: RequestManager, private val galleri

galleryblock_tag_group.removeAllViews()
CoroutineScope(Dispatchers.Default).launch {
galleryBlock.relatedTags.map {
galleryBlock.relatedTags.sortedBy {
val tag = Tag.parse(it)

if (favoriteTags.contains(tag))
-1
else
when(Tag.parse(it).area) {
"female" -> 0
"male" -> 1
else -> 2
}
}.map {
TagChip(context, Tag.parse(it)).apply {
setOnClickListener { view ->
for (callback in onChipClickedHandler)
Expand Down
24 changes: 15 additions & 9 deletions app/src/main/java/xyz/quaver/pupil/services/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ import xyz.quaver.pupil.R
import xyz.quaver.pupil.client
import xyz.quaver.pupil.interceptors
import xyz.quaver.pupil.ui.ReaderActivity
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
import xyz.quaver.pupil.util.ellipsize
import xyz.quaver.pupil.util.normalizeID
import xyz.quaver.pupil.util.requestBuilders
import java.io.IOException
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.roundToInt

private typealias ProgressListener = (DownloadService.Tag, Long, Long, Boolean) -> Unit
class DownloadService : Service() {
Expand Down Expand Up @@ -218,10 +221,11 @@ class DownloadService : Service() {

kotlin.runCatching {
val image = response.also { if (it.code() != 200) throw IOException() }.body()?.use { it.bytes() } ?: throw Exception()
val padding = ceil(progress[galleryID]?.size?.let { log10(it.toFloat()) } ?: 0F).toInt()

CoroutineScope(Dispatchers.IO).launch {
kotlin.runCatching {
Cache.getInstance(this@DownloadService, galleryID).putImage(index, "$index.$ext", image)
Cache.getInstance(this@DownloadService, galleryID).putImage(index, "${index.toString().padStart(padding, '0')}.$ext", image)
}.onSuccess {
progress[galleryID]?.set(index, Float.POSITIVE_INFINITY)
notify(galleryID)
Expand Down Expand Up @@ -309,24 +313,26 @@ class DownloadService : Service() {
return@launch
}

progress.put(galleryID, MutableList(reader.galleryInfo.files.size) { 0F })
val list = MutableList(reader.galleryInfo.files.size) { 0F }

cache.metadata.imageList?.let {
if (progress[galleryID]?.size != it.size) {
if (list.size != it.size) {
FirebaseCrashlytics.getInstance().log(
"""
GALLERYID: $galleryID
${it.size} - ${progress[galleryID]?.size}
${it.size} - ${list.size}
""".trimIndent()
)
error("ImageList Size does not match")
}

it.forEachIndexed { index, image ->
progress[galleryID]?.set(index, if (image != null) Float.POSITIVE_INFINITY else 0F)
list[index] = if (image != null) Float.POSITIVE_INFINITY else 0F
}
}

progress.put(galleryID, list)

if (isCompleted(galleryID)) {
if (DownloadManager.getInstance(this@DownloadService)
.getDownloadFolder(galleryID) != null )
Expand All @@ -352,17 +358,17 @@ class DownloadService : Service() {
}

reader.requestBuilders.also {
if (it.size != progress[galleryID]?.size) {
if (it.size != list.size) {
FirebaseCrashlytics.getInstance().log(
"""
GALLERYID: $galleryID
${it.size} - ${progress[galleryID]?.size}
${it.size} - ${list.size}
""".trimIndent()
)
error("Requests Size does not match")
}
}.forEachIndexed { index, it ->
if (progress[galleryID]?.get(index)?.isInfinite() != true) {
if (!list[index].isInfinite()) {
val request = it.tag(Tag(galleryID, index, startId)).build()
client.newCall(request).enqueue(callback)
}
Expand Down
21 changes: 7 additions & 14 deletions app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class ReaderActivity : BaseActivity() {

private var cameraEnabled = false
private var eyeType: Eye? = null
private var eyeCount: Int = 0
private var eyeTime: Long = 0L

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -248,13 +247,18 @@ class ReaderActivity : BaseActivity() {
override fun onResume() {
super.onResume()

bindService(Intent(this, DownloadService::class.java), conn, BIND_AUTO_CREATE)

if (cameraEnabled)
startCamera(this, cameraCallback)
}

override fun onPause() {
super.onPause()
closeCamera()

if (downloader != null)
unbindService(conn)
}

override fun onDestroy() {
Expand All @@ -265,9 +269,6 @@ class ReaderActivity : BaseActivity() {

if (!DownloadManager.getInstance(this).isDownloading(galleryID))
DownloadService.cancel(this, galleryID)

if (downloader != null)
unbindService(conn)
}

override fun onBackPressed() {
Expand Down Expand Up @@ -304,7 +305,6 @@ class ReaderActivity : BaseActivity() {

private fun initDownloader() {
DownloadService.download(this, galleryID, true)
bindService(Intent(this, DownloadService::class.java), conn, BIND_AUTO_CREATE)

timer.schedule(1000, 1000) {
val downloader = downloader ?: return@schedule
Expand Down Expand Up @@ -564,28 +564,23 @@ class ReaderActivity : BaseActivity() {
// Both closed / opened
!left.xor(right) -> {
eyeType = null
eyeCount = 0
eyeTime = 0L
}
!left -> {
if (eyeType != Eye.LEFT) {
eyeType = Eye.LEFT
eyeCount = 0
eyeTime = System.currentTimeMillis()
}
eyeCount++
}
!right -> {
if (eyeType != Eye.RIGHT) {
eyeType = Eye.RIGHT
eyeCount = 0
eyeTime = System.currentTimeMillis()
}
eyeCount++
}
}

if (eyeCount > 3 && System.currentTimeMillis() - eyeTime > 500) {
if (eyeType != null && System.currentTimeMillis() - eyeTime > 100) {
(this@ReaderActivity.reader_recyclerview.layoutManager as LinearLayoutManager).let {
it.scrollToPositionWithOffset(when(eyeType!!) {
Eye.RIGHT -> {
Expand All @@ -597,9 +592,7 @@ class ReaderActivity : BaseActivity() {
}, 0)
}

eyeType = null
eyeCount = 0
eyeTime = 0L
eyeTime = System.currentTimeMillis() + 500
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import kotlinx.android.synthetic.main.item_download_folder.view.*
import net.rdrei.android.dirchooser.DirectoryChooserActivity
import net.rdrei.android.dirchooser.DirectoryChooserConfig
import xyz.quaver.io.FileX
import xyz.quaver.io.util.toFile
import xyz.quaver.pupil.R
import xyz.quaver.pupil.util.Preferences
import xyz.quaver.pupil.util.byteToString
Expand All @@ -62,7 +63,7 @@ class DownloadLocationDialogFragment : DialogFragment() {
context.contentResolver.takePersistableUriPermission(uri, takeFlags)

if (kotlin.runCatching { FileX(context, uri).canWrite() }.getOrDefault(false)) {
entries[null]?.message?.text = uri.toString()
entries[null]?.location_available?.text = uri.toFile(context)?.canonicalPath
Preferences["download_folder"] = uri.toString()
} else {
Snackbar.make(
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/xyz/quaver/pupil/ui/dialog/GalleryDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import xyz.quaver.pupil.BuildConfig
import xyz.quaver.pupil.R
import xyz.quaver.pupil.adapters.GalleryBlockAdapter
import xyz.quaver.pupil.adapters.ThumbnailPageAdapter
import xyz.quaver.pupil.favoriteTags
import xyz.quaver.pupil.histories
import xyz.quaver.pupil.types.Tag
import xyz.quaver.pupil.ui.ReaderActivity
Expand Down Expand Up @@ -141,7 +142,18 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
listOf(gallery.language).map { Tag("language", it) },
gallery.series.map { Tag("series", it) },
gallery.characters.map { Tag("character", it) },
gallery.tags.map {
gallery.tags.sortedBy {
val tag = Tag.parse(it)

if (favoriteTags.contains(tag))
-1
else
when(Tag.parse(it).area) {
"female" -> 0
"male" -> 1
else -> 2
}
}.map {
Tag.parse(it).let { tag ->
when {
tag.area != null -> tag
Expand Down
43 changes: 20 additions & 23 deletions app/src/main/java/xyz/quaver/pupil/util/downloader/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,18 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
fun moveToDownload() = CoroutineScope(Dispatchers.IO).launch {
val downloadFolder = downloadFolder ?: return@launch

if (downloadFolder.getChild(".metadata").exists())
return@launch

metadata.imageList?.forEach { imageName ->
imageName ?: return@forEach
val target = downloadFolder.getChild(imageName)
val source = cacheFolder.getChild(imageName)
val cacheMetadata = cacheFolder.getChild(".metadata")
val downloadMetadata = downloadFolder.getChild(".metadata")

if (!source.exists() || target.exists())
return@forEach
if (downloadMetadata.exists() || !cacheMetadata.exists())
return@launch

if (cacheMetadata.exists()) {
kotlin.runCatching {
if (!target.exists())
target.createNewFile()
downloadMetadata.createNewFile()
downloadMetadata.writeText(Json.encodeToString(metadata))

target.outputStream()?.use { target -> source.inputStream()?.use { source ->
source.copyTo(target)
} }
cacheMetadata.delete()
}
}

Expand All @@ -236,19 +230,22 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
}
}

val cacheMetadata = cacheFolder.getChild(".metadata")
val downloadMetadata = downloadFolder.getChild(".metadata")
metadata.imageList?.forEach { imageName ->
imageName ?: return@forEach
val target = downloadFolder.getChild(imageName)
val source = cacheFolder.getChild(imageName)

if (!source.exists() || target.exists())
return@forEach

if (cacheMetadata.exists() && !downloadMetadata.exists()) {
kotlin.runCatching {
if (!downloadMetadata.exists())
downloadMetadata.createNewFile()
if (!target.exists())
target.createNewFile()

downloadMetadata.writeText(Json.encodeToString(metadata))
cacheMetadata.delete()
target.outputStream()?.use { target -> source.inputStream()?.use { source ->
source.copyTo(target)
} }
}
}

cacheFolder.delete()
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
app:leftActionMode="showHamburger"
app:menu="@menu/main"
app:dismissOnOutsideTouch="true"
app:close_search_on_keyboard_dismiss="true"
app:close_search_on_keyboard_dismiss="false"
tools:ignore="NewApi" />

</RelativeLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">

<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

<dimen name="galleryblock_thumbnail_thin">100dp</dimen>

<dimen name="reader_max_height">2000dp</dimen>
<dimen name="reader_max_height" tools:ignore="PxUsage">2000px</dimen>

<dimen name="thumb_width">24dp</dimen>
<dimen name="thumb_height">72dp</dimen>
Expand Down

0 comments on commit a973cdf

Please sign in to comment.