From bcd515b2dbad851f86f6c5a985ee078f032f92d8 Mon Sep 17 00:00:00 2001 From: tom5079 Date: Wed, 5 Jun 2019 23:03:37 +0900 Subject: [PATCH] Download gallery to the data folder not cache folder --- app/src/main/AndroidManifest.xml | 11 +---- .../java/xyz/quaver/pupil/MainActivity.kt | 23 +++++++-- .../pupil/adapters/GalleryBlockAdapter.kt | 30 ++++++++---- .../quaver/pupil/util/GalleryDownloader.kt | 47 +++++++++++++++++-- .../main/res/layout/dialog_galleryblock.xml | 8 ++++ app/src/main/res/values-ja/strings.xml | 1 + app/src/main/res/values-ko/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 8 files changed, 96 insertions(+), 26 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 766a3ca7..c0514b8f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="xyz.quaver.pupil"> + - - - - diff --git a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt index d35b14b4..e4432784 100644 --- a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt @@ -15,6 +15,7 @@ import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.cardview.widget.CardView +import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.view.GravityCompat import com.arlib.floatingsearchview.FloatingSearchView @@ -359,10 +360,9 @@ class MainActivity : AppCompatActivity() { isEnabled = !(adapter as GalleryBlockAdapter).completeFlag.get(galleryBlock.id, false) setOnClickListener { val downloader = GalleryDownloader.get(galleryBlock.id) - if (downloader == null) { + if (downloader == null) GalleryDownloader(context, galleryBlock, true).start() - downloads.add(galleryBlock.id) - } else { + else { downloader.cancel() downloader.clearNotification() } @@ -377,12 +377,25 @@ class MainActivity : AppCompatActivity() { this?.cancelAndJoin() this?.clearNotification() } - val cache = File(cacheDir, "imageCache/${galleryBlock.id}/images/") + val cache = File(cacheDir, "imageCache/${galleryBlock.id}") + val data = File(ContextCompat.getDataDir(this@MainActivity), "images/${galleryBlock.id}") cache.deleteRecursively() + data.deleteRecursively() + + downloads.remove(galleryBlock.id) + + if (mode == Mode.DOWNLOAD) { + runOnUiThread { + cancelFetch() + clearGalleries() + fetchGalleries(query) + loadBlocks() + } + } - dialog.dismiss() (adapter as GalleryBlockAdapter).completeFlag.put(galleryBlock.id, false) } + dialog.dismiss() } dialog.show() diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt index c75529b7..ed8aa83d 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt @@ -68,16 +68,30 @@ class GalleryBlockAdapter(private val galleries: List it + else -> File(context.cacheDir, "imageCache/${gallery.id}/reader.json") + } + } + } + val imageCache = { + File(ContextCompat.getDataDir(context), "images/${gallery.id}/images").let { + when { + it.exists() -> it + else -> File(context.cacheDir, "imageCache/${gallery.id}/images") + } + } + } - if (readerCache.exists()) { + if (readerCache.invoke().exists()) { val reader = Json(JsonConfiguration.Stable) - .parse(ReaderItem.serializer().list, readerCache.readText()) + .parse(ReaderItem.serializer().list, readerCache.invoke().readText()) with(galleryblock_progressbar) { max = reader.size - progress = imageCache.list()?.size ?: 0 + progress = imageCache.invoke().list()?.size ?: 0 visibility = View.VISIBLE } @@ -89,9 +103,9 @@ class GalleryBlockAdapter(private val galleries: List it + else -> File(cacheDir, "imageCache/${galleryBlock.id}/reader.json") + } + } if (cache.exists()) { val cached = json.parse(serializer, cache.readText()) @@ -148,7 +169,12 @@ class GalleryDownloader( val name = "$index".padStart(4, '0') val ext = url.split('.').last() - val cache = File(cacheDir, "/imageCache/${galleryBlock.id}/images/$name.$ext") + val cache = File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}/images/$name.$ext").let { + when { + it.exists() -> it + else -> File(cacheDir, "/imageCache/${galleryBlock.id}/images/$name.$ext") + } + } if (!cache.exists()) try { @@ -163,6 +189,8 @@ class GalleryDownloader( } catch (e: Exception) { cache.delete() + downloads.remove(galleryBlock.id) + onErrorHandler?.invoke(e) notificationBuilder @@ -189,8 +217,19 @@ class GalleryDownloader( .setContentText(getString(R.string.reader_notification_complete)) .setProgress(0, 0, false) - if (download) + if (download) { + File(cacheDir, "imageCache/${galleryBlock.id}").let { + if (it.exists()) { + it.copyRecursively( + File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}"), + true + ) + it.deleteRecursively() + } + } + notificationManager.notify(galleryBlock.id, notificationBuilder.build()) + } download = false } @@ -207,6 +246,8 @@ class GalleryDownloader( suspend fun cancelAndJoin() { downloadJob?.cancelAndJoin() + + remove(galleryBlock.id) } fun invokeOnReaderLoaded() { diff --git a/app/src/main/res/layout/dialog_galleryblock.xml b/app/src/main/res/layout/dialog_galleryblock.xml index 3c9bc82a..391e4525 100644 --- a/app/src/main/res/layout/dialog_galleryblock.xml +++ b/app/src/main/res/layout/dialog_galleryblock.xml @@ -20,4 +20,12 @@ android:text="@string/main_dialog_delete" app:layout_constraintTop_toBottomOf="@id/main_dialog_download"/> +