From 3fc71b195a7e0fe88853f9a19246aa8d5d1eaf40 Mon Sep 17 00:00:00 2001 From: tom5079 Date: Sun, 9 Jun 2019 13:42:40 +0900 Subject: [PATCH 1/4] Show gallery ID Fixed paging problem when tag searched made gallery info clickable --- app/build.gradle | 4 +- .../java/xyz/quaver/pupil/MainActivity.kt | 10 ++- .../pupil/adapters/GalleryBlockAdapter.kt | 62 ++++++++++++++++++- .../quaver/pupil/adapters/ReaderAdapter.kt | 6 +- app/src/main/res/layout/item_galleryblock.xml | 27 ++++++-- app/src/main/res/values/strings.xml | 3 +- .../java/xyz/quaver/hitomi/galleryblock.kt | 2 +- .../test/java/xyz/quaver/hitomi/UnitTest.kt | 2 +- 8 files changed, 100 insertions(+), 16 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3935d108..0c68795c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "xyz.quaver.pupil" minSdkVersion 16 targetSdkVersion 28 - versionCode 14 - versionName "2.7" + versionCode 15 + versionName "3.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt index feaa0bab..8add8651 100644 --- a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt @@ -39,7 +39,6 @@ import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.content import kotlinx.serialization.list -import kotlinx.serialization.parseList import kotlinx.serialization.stringify import ru.noties.markwon.Markwon import xyz.quaver.hitomi.* @@ -116,6 +115,13 @@ class MainActivity : AppCompatActivity() { initView() } + override fun onDestroy() { + super.onDestroy() + + if (cacheDir.exists()) + cacheDir.deleteRecursively() + } + override fun onBackPressed() { when { main_drawer_layout.isDrawerOpen(GravityCompat.START) -> main_drawer_layout.closeDrawer(GravityCompat.START) @@ -249,7 +255,7 @@ class MainActivity : AppCompatActivity() { val msg = extractReleaseNote(update, Locale.getDefault().language) setMessage(Markwon.create(context).toMarkdown(msg)) setPositiveButton(android.R.string.yes) { _, _ -> - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.home_page)))) + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.update)))) } setNegativeButton(android.R.string.no) { _, _ ->} } 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 96f5b609..52d25afe 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt @@ -1,5 +1,7 @@ package xyz.quaver.pupil.adapters +import android.app.AlertDialog +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.drawable.Animatable import android.util.Log @@ -7,6 +9,7 @@ import android.util.SparseBooleanArray import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ArrayAdapter import android.widget.LinearLayout import androidx.cardview.widget.CardView import androidx.core.content.ContextCompat @@ -23,6 +26,7 @@ import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.list import xyz.quaver.hitomi.GalleryBlock import xyz.quaver.hitomi.ReaderItem +import xyz.quaver.pupil.BuildConfig import xyz.quaver.pupil.Pupil import xyz.quaver.pupil.R import xyz.quaver.pupil.types.Tag @@ -64,7 +68,10 @@ class GalleryBlockAdapter(private val galleries: List Bitmap.createScaledBitmap(BitmapFactory.decodeFile(thumbnail.await()), 5, 8, true) + else -> BitmapFactory.decodeFile(thumbnail.await()) + } post { galleryblock_thumbnail.setImageBitmap(bitmap) @@ -157,6 +164,19 @@ class GalleryBlockAdapter(private val galleries: List View.VISIBLE else -> View.GONE } + setOnClickListener { + if (artists.size > 1) { + AlertDialog.Builder(context).apply { + setAdapter(ArrayAdapter(context, android.R.layout.select_dialog_item, artists)) { _, index -> + for (callback in onChipClickedHandler) + callback.invoke(Tag("artist", artists[index])) + } + }.show() + } else { + for(callback in onChipClickedHandler) + callback.invoke(Tag("artist", artists.first())) + } + } } with(galleryblock_series) { text = @@ -167,8 +187,31 @@ class GalleryBlockAdapter(private val galleries: List View.VISIBLE else -> View.GONE } + setOnClickListener { + setOnClickListener { + if (series.size > 1) { + AlertDialog.Builder(context).apply { + setAdapter(ArrayAdapter(context, android.R.layout.select_dialog_item, series)) { _, index -> + for (callback in onChipClickedHandler) + callback.invoke(Tag("series", series[index])) + } + }.show() + } else { + for(callback in onChipClickedHandler) + callback.invoke(Tag("series", series.first())) + } + } + } + } + with(galleryblock_type) { + text = resources.getString(R.string.galleryblock_type, gallery.type).wordCapitalize() + setOnClickListener { + setOnClickListener { + for(callback in onChipClickedHandler) + callback.invoke(Tag("type", gallery.type)) + } + } } - galleryblock_type.text = resources.getString(R.string.galleryblock_type, gallery.type).wordCapitalize() with(galleryblock_language) { text = resources.getString(R.string.galleryblock_language, languages[gallery.language]) @@ -176,11 +219,22 @@ class GalleryBlockAdapter(private val galleries: List View.VISIBLE else -> View.GONE } + setOnClickListener { + setOnClickListener { + for(callback in onChipClickedHandler) + callback.invoke(Tag("language", gallery.language)) + } + } } galleryblock_tag_group.removeAllViews() gallery.relatedTags.forEach { - val tag = Tag.parse(it) + val tag = Tag.parse(it).let { tag -> + when { + tag.area != null -> tag + else -> Tag("tag", it) + } + } val chip = LayoutInflater.from(context) .inflate(R.layout.tag_chip, this, false) as Chip @@ -209,6 +263,8 @@ class GalleryBlockAdapter(private val galleries: List) : RecyclerView.Adapter() { @@ -37,7 +38,10 @@ class ReaderAdapter(private val images: List) : RecyclerView.Adapter Bitmap.createScaledBitmap(BitmapFactory.decodeFile(images[position], options), 5, 8, true) + else -> BitmapFactory.decodeFile(images[position], options) + } post { setImageBitmap(image) } } diff --git a/app/src/main/res/layout/item_galleryblock.xml b/app/src/main/res/layout/item_galleryblock.xml index 8186c6a5..7e05f4c2 100644 --- a/app/src/main/res/layout/item_galleryblock.xml +++ b/app/src/main/res/layout/item_galleryblock.xml @@ -69,6 +69,8 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" + android:clickable="true" + android:focusable="true" app:layout_constraintLeft_toRightOf="@id/galleryblock_thumbnail" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/galleryblock_title"/> @@ -79,6 +81,8 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" + android:clickable="true" + android:focusable="true" app:layout_constraintTop_toBottomOf="@id/galleryblock_artist" app:layout_constraintStart_toEndOf="@id/galleryblock_thumbnail" app:layout_constraintEnd_toEndOf="parent"/> @@ -89,6 +93,8 @@ android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" + android:clickable="true" + android:focusable="true" app:layout_constraintTop_toBottomOf="@id/galleryblock_series" app:layout_constraintStart_toEndOf="@id/galleryblock_thumbnail" /> @@ -99,6 +105,8 @@ android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginBottom="8dp" + android:clickable="true" + android:focusable="true" app:layout_constraintTop_toBottomOf="@id/galleryblock_type" app:layout_constraintBottom_toTopOf="@id/galleryblock_padding" app:layout_constraintStart_toEndOf="@id/galleryblock_thumbnail" /> @@ -135,16 +143,25 @@ + android:paddingLeft="8dp" + android:paddingRight="8dp" + android:paddingBottom="8dp" + android:orientation="horizontal"> + + + + https://api.github.com/repos/tom5079/Pupil-issue/releases Pupil-v(\\d+\\.)+\\d+\\.apk - http://bit.ly/2ZlOjXJ + http://bit.ly/2EZDClw + http://bit.ly/2ZlOjXJ http://bit.ly/2Z7lNZE https://github.com/tom5079/Pupil-issue/issues/new/choose mailto:pupil.hentai@gmail.com diff --git a/libpupil/src/main/java/xyz/quaver/hitomi/galleryblock.kt b/libpupil/src/main/java/xyz/quaver/hitomi/galleryblock.kt index 42aece05..9ecd6a9e 100644 --- a/libpupil/src/main/java/xyz/quaver/hitomi/galleryblock.kt +++ b/libpupil/src/main/java/xyz/quaver/hitomi/galleryblock.kt @@ -80,7 +80,7 @@ fun getGalleryBlock(galleryID: Int) : GalleryBlock? { val relatedTags = doc.select(".relatedtags a").map { val href = URLDecoder.decode(it.attr("href"), "UTF-8") - href.slice(5 until href.indexOf('-')) + href.slice(5 until href.indexOf("-all")) } return GalleryBlock(galleryID, thumbnails, title, artists, series, type, language, relatedTags) diff --git a/libpupil/src/test/java/xyz/quaver/hitomi/UnitTest.kt b/libpupil/src/test/java/xyz/quaver/hitomi/UnitTest.kt index fe648e59..bc7c0d78 100644 --- a/libpupil/src/test/java/xyz/quaver/hitomi/UnitTest.kt +++ b/libpupil/src/test/java/xyz/quaver/hitomi/UnitTest.kt @@ -42,7 +42,7 @@ class UnitTest { @Test fun test_getBlock() { - val galleryBlock = getGalleryBlock(1405716) + val galleryBlock = getGalleryBlock(1428250) print(galleryBlock) } From 257ad928700f53c792266087dfcf70014cfd2fbc Mon Sep 17 00:00:00 2001 From: tom5079 Date: Sun, 9 Jun 2019 15:01:52 +0900 Subject: [PATCH 2/4] Made download simple Bug fixed --- .../java/xyz/quaver/pupil/MainActivity.kt | 94 ++------------ .../java/xyz/quaver/pupil/ReaderActivity.kt | 8 -- .../java/xyz/quaver/pupil/SettingsActivity.kt | 29 +---- .../pupil/adapters/GalleryBlockAdapter.kt | 57 ++++----- .../quaver/pupil/adapters/ReaderAdapter.kt | 5 +- .../quaver/pupil/util/GalleryDownloader.kt | 118 +++--------------- .../main/java/xyz/quaver/pupil/util/file.kt | 14 +++ app/src/main/res/layout/activity_reader.xml | 8 -- .../main/res/layout/dialog_galleryblock.xml | 8 -- app/src/main/res/values-ja/strings.xml | 9 +- app/src/main/res/values-ko/strings.xml | 9 +- app/src/main/res/values/strings.xml | 9 +- app/src/main/res/xml/root_preferences.xml | 16 +-- 13 files changed, 71 insertions(+), 313 deletions(-) create mode 100644 app/src/main/java/xyz/quaver/pupil/util/file.kt diff --git a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt index 8add8651..024641a3 100644 --- a/app/src/main/java/xyz/quaver/pupil/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/MainActivity.kt @@ -6,7 +6,6 @@ import android.content.pm.PackageManager import android.graphics.drawable.Animatable import android.net.Uri import android.os.Bundle -import android.os.Environment import android.preference.PreferenceManager import android.text.* import android.text.style.AlignmentSpan @@ -32,7 +31,6 @@ import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main_content.* import kotlinx.android.synthetic.main.dialog_galleryblock.view.* import kotlinx.coroutines.* -import kotlinx.io.IOException import kotlinx.serialization.ImplicitReflectionSerializer import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonConfiguration @@ -48,12 +46,9 @@ import xyz.quaver.pupil.types.TagSuggestion import xyz.quaver.pupil.types.Tags import xyz.quaver.pupil.util.* import java.io.File -import java.io.FileInputStream import java.io.FileOutputStream import java.net.URL import java.util.* -import java.util.zip.ZipEntry -import java.util.zip.ZipOutputStream import javax.net.ssl.HttpsURLConnection import kotlin.collections.ArrayList import kotlin.math.roundToInt @@ -417,7 +412,7 @@ class MainActivity : AppCompatActivity() { this?.clearNotification() } val cache = File(cacheDir, "imageCache/${galleryBlock.id}") - val data = File(ContextCompat.getDataDir(this@MainActivity), "images/${galleryBlock.id}") + val data = getCachedGallery(context, galleryBlock.id) cache.deleteRecursively() data.deleteRecursively() @@ -437,73 +432,6 @@ class MainActivity : AppCompatActivity() { dialog.dismiss() } - with(view.main_dialog_export) { - val images = File(ContextCompat.getDataDir(this@MainActivity), "images/${galleryBlock.id}/images").let { - when { - it.exists() -> it - else -> File(cacheDir, "imageCache/${galleryBlock.id}/images") - } - } - isEnabled = images.exists() - - setOnClickListener { - CoroutineScope(Dispatchers.Default).launch { - val preference = PreferenceManager.getDefaultSharedPreferences(context) - val zip = preference.getBoolean("export_zip", false) - - if (zip) { - var target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id} ${galleryBlock.title}.zip") - - try { - target.createNewFile() - } catch (e: IOException) { - target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}.zip") - - try { - target.createNewFile() - } catch (e: IOException) { - Snackbar.make(main_layout, getString(R.string.main_export_error), Snackbar.LENGTH_LONG).show() - return@launch - } - } - - FileOutputStream(target).use { targetStream -> - ZipOutputStream(targetStream).use {zipStream -> - images.listFiles().forEach { - zipStream.putNextEntry(ZipEntry(it.name)) - - FileInputStream(it).use { fileStream -> - fileStream.copyTo(zipStream) - } - } - } - } - } else { - var target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id} ${galleryBlock.title}") - - try { - target.canonicalPath - } catch (e: IOException) { - target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}") - - try { - target.canonicalPath - } catch (e: IOException) { - Snackbar.make(main_layout, getString(R.string.main_export_error), Snackbar.LENGTH_LONG).show() - return@launch - } - } - - images.copyRecursively(target, true) - } - - Snackbar.make(main_layout, getString(R.string.main_export_complete), Snackbar.LENGTH_LONG).show() - } - - dialog.dismiss() - } - } - dialog.show() true @@ -1064,18 +992,18 @@ class MainActivity : AppCompatActivity() { galleryIDs.slice(currentPage*perPage until Math.min(currentPage*perPage+perPage, galleryIDs.size)) }.chunked(5).let { chunks -> for (chunk in chunks) - chunk.map { + chunk.map { galleryID -> async { try { val json = Json(JsonConfiguration.Stable) val serializer = GalleryBlock.serializer() val galleryBlock = - File(cacheDir, "imageCache/$it/galleryBlock.json").let { cache -> + File(getCachedGallery(this@MainActivity, galleryID), "galleryBlock.json").let { cache -> when { cache.exists() -> json.parse(serializer, cache.readText()) else -> { - getGalleryBlock(it).apply { + getGalleryBlock(galleryID).apply { this ?: return@apply if (!cache.parentFile.exists()) @@ -1089,19 +1017,17 @@ class MainActivity : AppCompatActivity() { val thumbnail = async { val ext = galleryBlock.thumbnails[0].split('.').last() - File(cacheDir, "imageCache/$it/thumbnail.$ext").apply { - val cache = this - - if (!cache.exists()) + File(getCachedGallery(this@MainActivity, galleryBlock.id), "thumbnail.$ext").apply { + if (!exists()) try { with(URL(galleryBlock.thumbnails[0]).openConnection() as HttpsURLConnection) { - if (!cache.parentFile.exists()) - cache.parentFile.mkdirs() + if (!this@apply.parentFile.exists()) + this@apply.parentFile.mkdirs() - inputStream.copyTo(FileOutputStream(cache)) + inputStream.copyTo(FileOutputStream(this@apply)) } } catch (e: Exception) { - cache.delete() + delete() } }.absolutePath } diff --git a/app/src/main/java/xyz/quaver/pupil/ReaderActivity.kt b/app/src/main/java/xyz/quaver/pupil/ReaderActivity.kt index 263eebd1..f4ddbd97 100644 --- a/app/src/main/java/xyz/quaver/pupil/ReaderActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/ReaderActivity.kt @@ -309,14 +309,6 @@ class ReaderActivity : AppCompatActivity() { if (!downloader.download) downloader.clearNotification() } - - reader_fab_export.setOnClickListener { - downloader.export( { - Snackbar.make(reader_layout, getString(R.string.main_export_complete), Snackbar.LENGTH_LONG).show() - }, { - Snackbar.make(reader_layout, getString(R.string.main_export_error), Snackbar.LENGTH_LONG).show() - }) - } } private fun fullscreen(isFullscreen: Boolean) { diff --git a/app/src/main/java/xyz/quaver/pupil/SettingsActivity.kt b/app/src/main/java/xyz/quaver/pupil/SettingsActivity.kt index 9f7b30be..e93b0166 100644 --- a/app/src/main/java/xyz/quaver/pupil/SettingsActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/SettingsActivity.kt @@ -1,6 +1,7 @@ package xyz.quaver.pupil import android.os.Bundle +import android.os.Environment import android.preference.PreferenceManager import android.text.Editable import android.text.TextWatcher @@ -12,7 +13,6 @@ import android.widget.LinearLayout import android.widget.TextView import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import kotlinx.android.synthetic.main.dialog_default_query.view.* @@ -67,39 +67,16 @@ class SettingsActivity : AppCompatActivity() { suffixIndex++ } - return getString(R.string.settings_clear_cache_summary, size, suffix[suffixIndex]) + return getString(R.string.settings_clear_downloads_summary, size, suffix[suffixIndex]) } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.root_preferences, rootKey) - with(findPreference("delete_image_cache")) { - this ?: return@with - - val dir = File(context.cacheDir, "imageCache") - - summary = getCacheSize(dir) - - setOnPreferenceClickListener { - AlertDialog.Builder(context).apply { - setTitle(R.string.warning) - setMessage(R.string.settings_clear_cache_alert_message) - setPositiveButton(android.R.string.yes) { _, _ -> - if (dir.exists()) - dir.deleteRecursively() - - summary = getCacheSize(dir) - } - setNegativeButton(android.R.string.no) { _, _ -> } - }.show() - - true - } - } with(findPreference("delete_downloads")) { this ?: return@with - val dir = File(ContextCompat.getDataDir(context), "images") + val dir = File(Environment.getExternalStorageDirectory(), "Pupil") summary = getCacheSize(dir) 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 52d25afe..7307be71 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt @@ -4,6 +4,7 @@ import android.app.AlertDialog import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.drawable.Animatable +import android.os.Environment import android.util.Log import android.util.SparseBooleanArray import android.view.LayoutInflater @@ -31,6 +32,7 @@ import xyz.quaver.pupil.Pupil import xyz.quaver.pupil.R import xyz.quaver.pupil.types.Tag import xyz.quaver.pupil.util.Histories +import xyz.quaver.pupil.util.getCachedGallery import java.io.File import java.util.* import kotlin.collections.ArrayList @@ -57,10 +59,10 @@ class GalleryBlockAdapter(private val galleries: List) = item + val (galleryBlock: GalleryBlock, thumbnail: Deferred) = item - val artists = gallery.artists - val series = gallery.series + val artists = galleryBlock.artists + val series = galleryBlock.series CoroutineScope(Dispatchers.Default).launch { val cache = thumbnail.await() @@ -68,10 +70,7 @@ class GalleryBlockAdapter(private val galleries: List Bitmap.createScaledBitmap(BitmapFactory.decodeFile(thumbnail.await()), 5, 8, true) - else -> BitmapFactory.decodeFile(thumbnail.await()) - } + val bitmap = BitmapFactory.decodeFile(thumbnail.await()) post { galleryblock_thumbnail.setImageBitmap(bitmap) @@ -79,22 +78,8 @@ 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") - } - } - } + val readerCache = { File(getCachedGallery(context, galleryBlock.id), "reader.json") } + val imageCache = { File(getCachedGallery(context, galleryBlock.id), "images") } if (readerCache.invoke().exists()) { val reader = Json(JsonConfiguration.Stable) @@ -131,7 +116,7 @@ class GalleryBlockAdapter(private val galleries: List View.VISIBLE + galleryBlock.language.isNotEmpty() -> View.VISIBLE else -> View.GONE } setOnClickListener { setOnClickListener { for(callback in onChipClickedHandler) - callback.invoke(Tag("language", gallery.language)) + callback.invoke(Tag("language", galleryBlock.language)) } } } galleryblock_tag_group.removeAllViews() - gallery.relatedTags.forEach { + galleryBlock.relatedTags.forEach { val tag = Tag.parse(it).let { tag -> when { tag.area != null -> tag @@ -263,19 +248,19 @@ class GalleryBlockAdapter(private val galleries: List favorites.add(gallery.id) - else -> favorites.remove(gallery.id) + isChecked -> favorites.add(galleryBlock.id) + else -> favorites.remove(galleryBlock.id) } } setOnCheckedChangeListener { _, isChecked -> diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt index 2cdcd634..21a0830e 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt @@ -38,10 +38,7 @@ class ReaderAdapter(private val images: List) : RecyclerView.Adapter Bitmap.createScaledBitmap(BitmapFactory.decodeFile(images[position], options), 5, 8, true) - else -> BitmapFactory.decodeFile(images[position], options) - } + val image = BitmapFactory.decodeFile(images[position], options) post { setImageBitmap(image) } } diff --git a/app/src/main/java/xyz/quaver/pupil/util/GalleryDownloader.kt b/app/src/main/java/xyz/quaver/pupil/util/GalleryDownloader.kt index 3b5a892e..5a5835c1 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/GalleryDownloader.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/GalleryDownloader.kt @@ -9,7 +9,6 @@ import android.util.SparseArray import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.TaskStackBuilder -import androidx.core.content.ContextCompat import androidx.preference.PreferenceManager import kotlinx.coroutines.* import kotlinx.io.IOException @@ -23,12 +22,9 @@ import xyz.quaver.pupil.Pupil import xyz.quaver.pupil.R import xyz.quaver.pupil.ReaderActivity import java.io.File -import java.io.FileInputStream import java.io.FileOutputStream import java.net.URL import java.util.* -import java.util.zip.ZipEntry -import java.util.zip.ZipOutputStream import javax.net.ssl.HttpsURLConnection import kotlin.collections.ArrayList import kotlin.concurrent.schedule @@ -48,7 +44,12 @@ class GalleryDownloader( field = true notificationManager.notify(galleryBlock.id, notificationBuilder.build()) - val data = File(ContextCompat.getDataDir(this), "images/${galleryBlock.id}") + val data = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id} ${galleryBlock.title}").let { + when { + it.exists() -> it + else -> File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}") + } + } val cache = File(cacheDir, "imageCache/${galleryBlock.id}") if (cache.exists() && !data.exists()) { @@ -93,12 +94,7 @@ class GalleryDownloader( val serializer = ReaderItem.serializer().list //Check cache - val cache = File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}/reader.json").let { - when { - it.exists() -> it - else -> File(cacheDir, "imageCache/${galleryBlock.id}/reader.json") - } - } + val cache = File(getCachedGallery(this@GalleryDownloader, galleryBlock.id), "reader.json") if (cache.exists()) { val cached = json.parse(serializer, cache.readText()) @@ -181,12 +177,7 @@ class GalleryDownloader( val name = "$index".padStart(4, '0') val ext = url.split('.').last() - 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") - } - } + val cache = File(getCachedGallery(this@GalleryDownloader, galleryBlock.id), "images/$name.$ext") if (!cache.exists()) try { @@ -234,31 +225,17 @@ class GalleryDownloader( if (download) { File(cacheDir, "imageCache/${galleryBlock.id}").let { if (it.exists()) { - it.copyRecursively( - File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}"), - true - ) + val target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}") + + if (!target.exists()) + target.mkdirs() + + it.copyRecursively(target, true) it.deleteRecursively() } } - val preference = PreferenceManager.getDefaultSharedPreferences(this@GalleryDownloader) - val autoExport = preference.getBoolean("auto_export", false) - - if (autoExport) { - export({ - notificationManager.notify(galleryBlock.id, notificationBuilder.build()) - }, { - notificationBuilder - .setContentTitle(galleryBlock.title) - .setContentText(getString(R.string.main_export_error)) - .setProgress(0, 0, false) - - notificationManager.notify(galleryBlock.id, notificationBuilder.build()) - }) - } else { - notificationManager.notify(galleryBlock.id, notificationBuilder.build()) - } + notificationManager.notify(galleryBlock.id, notificationBuilder.build()) } download = false @@ -314,69 +291,4 @@ class GalleryDownloader( notificationManager = NotificationManagerCompat.from(this) } - fun export(onSuccess: (() -> Unit)? = null, onError: (() -> Unit)? = null) { - val images = File(ContextCompat.getDataDir(this), "images/${galleryBlock.id}/images").let { - when { - it.exists() -> it - else -> File(cacheDir, "imageCache/${galleryBlock.id}/images") - } - } - - if (!images.exists()) - return - - CoroutineScope(Dispatchers.Default).launch { - val preference = PreferenceManager.getDefaultSharedPreferences(this@GalleryDownloader) - val zip = preference.getBoolean("export_zip", false) - - if (zip) { - var target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id} ${galleryBlock.title}.zip") - - try { - target.createNewFile() - } catch (e: IOException) { - target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}.zip") - - try { - target.createNewFile() - } catch (e: IOException) { - onError?.invoke() - return@launch - } - } - - FileOutputStream(target).use { targetStream -> - ZipOutputStream(targetStream).use { zipStream -> - images.listFiles().forEach { - zipStream.putNextEntry(ZipEntry(it.name)) - - FileInputStream(it).use { fileStream -> - fileStream.copyTo(zipStream) - } - } - } - } - } else { - var target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id} ${galleryBlock.title}") - - try { - target.canonicalPath - } catch (e: IOException) { - target = File(Environment.getExternalStorageDirectory(), "Pupil/${galleryBlock.id}") - - try { - target.canonicalPath - } catch (e: IOException) { - onError?.invoke() - return@launch - } - } - - images.copyRecursively(target, true) - } - - onSuccess?.invoke() - } - } - } \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/util/file.kt b/app/src/main/java/xyz/quaver/pupil/util/file.kt new file mode 100644 index 00000000..7e085650 --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/util/file.kt @@ -0,0 +1,14 @@ +package xyz.quaver.pupil.util + +import android.content.Context +import android.os.Environment +import java.io.File + +fun getCachedGallery(context: Context, galleryID: Int): File { + return File(Environment.getExternalStorageDirectory(), "Pupil/$galleryID").let { + when { + it.exists() -> it + else -> File(context.cacheDir, "imageCache/$galleryID") + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_reader.xml b/app/src/main/res/layout/activity_reader.xml index e9827e8d..9e49df68 100644 --- a/app/src/main/res/layout/activity_reader.xml +++ b/app/src/main/res/layout/activity_reader.xml @@ -51,14 +51,6 @@ app:fab_label="@string/reader_fab_download" app:fab_size="mini"/> - - -