Bug fixed

This commit is contained in:
tom5079
2019-05-18 23:20:06 +09:00
parent 8de3eace8a
commit fd83f987dd
13 changed files with 237 additions and 45 deletions

View File

@@ -6,6 +6,7 @@ import android.os.Bundle
import android.preference.PreferenceManager
import android.text.*
import android.text.style.AlignmentSpan
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.TextView
@@ -21,6 +22,7 @@ import com.arlib.floatingsearchview.util.view.SearchInputView
import com.google.android.material.appbar.AppBarLayout
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.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration
@@ -30,10 +32,7 @@ import ru.noties.markwon.Markwon
import xyz.quaver.hitomi.*
import xyz.quaver.pupil.adapters.GalleryBlockAdapter
import xyz.quaver.pupil.types.TagSuggestion
import xyz.quaver.pupil.util.Histories
import xyz.quaver.pupil.util.ItemClickSupport
import xyz.quaver.pupil.util.SetLineOverlap
import xyz.quaver.pupil.util.checkUpdate
import xyz.quaver.pupil.util.*
import java.io.File
import java.io.FileOutputStream
import java.net.URL
@@ -47,6 +46,8 @@ class MainActivity : AppCompatActivity() {
private var query = ""
private val SETTINGS = 45162
private var galleryIDs: Deferred<List<Int>>? = null
private var loadingJob: Job? = null
@@ -164,6 +165,20 @@ class MainActivity : AppCompatActivity() {
super.onResume()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode) {
SETTINGS -> {
runOnUiThread {
cancelFetch()
clearGalleries()
fetchGalleries(query)
loadBlocks()
}
}
}
}
private fun checkUpdate() {
fun extractReleaseNote(update: JsonObject, locale: String) : String {
@@ -257,16 +272,63 @@ class MainActivity : AppCompatActivity() {
}
}
)
ItemClickSupport.addTo(this).setOnItemClickListener { _, position, _ ->
val intent = Intent(this@MainActivity, ReaderActivity::class.java)
val gallery = galleries[position].first
intent.putExtra("galleryblock", Json(JsonConfiguration.Stable).stringify(GalleryBlock.serializer(), gallery))
ItemClickSupport.addTo(this)
.setOnItemClickListener { _, position, _ ->
val intent = Intent(this@MainActivity, ReaderActivity::class.java)
val gallery = galleries[position].first
intent.putExtra("galleryblock", Json(JsonConfiguration.Stable).stringify(GalleryBlock.serializer(), gallery))
//TODO: Maybe sprinke some transitions will be nice :D
startActivity(intent)
//TODO: Maybe sprinke some transitions will be nice :D
startActivity(intent)
Histories.default.add(gallery.id)
}
Histories.default.add(gallery.id)
}.setOnItemLongClickListener { recyclerView, position, v ->
val galleryBlock = galleries[position].first
val view = LayoutInflater.from(this@MainActivity)
.inflate(R.layout.dialog_galleryblock, recyclerView, false)
val dialog = AlertDialog.Builder(this@MainActivity).apply {
setView(view)
}.create()
with(view.main_dialog_download) {
text = when(GalleryDownloader.get(galleryBlock.id)) {
null -> getString(R.string.reader_fab_download)
else -> getString(R.string.reader_fab_download_cancel)
}
isEnabled = !(adapter as GalleryBlockAdapter).completeFlag.get(galleryBlock.id, false)
setOnClickListener {
val downloader = GalleryDownloader.get(galleryBlock.id)
if (downloader == null) {
GalleryDownloader(context, galleryBlock, true).start()
Histories.default.add(galleryBlock.id)
} else {
downloader.cancel()
downloader.clearNotification()
}
dialog.dismiss()
}
}
view.main_dialog_delete.setOnClickListener {
CoroutineScope(Dispatchers.Default).launch {
with(GalleryDownloader[galleryBlock.id]) {
this?.cancelAndJoin()
this?.clearNotification()
}
val cache = File(cacheDir, "imageCache/${galleryBlock.id}/images/")
cache.deleteRecursively()
dialog.dismiss()
(adapter as GalleryBlockAdapter).completeFlag.put(galleryBlock.id, false)
}
}
dialog.show()
true
}
}
}
@@ -294,7 +356,7 @@ class MainActivity : AppCompatActivity() {
with(main_searchview as FloatingSearchView) {
setOnMenuItemClickListener {
when(it.itemId) {
R.id.main_menu_settings -> startActivity(Intent(this@MainActivity, SettingsActivity::class.java))
R.id.main_menu_settings -> startActivityForResult(Intent(this@MainActivity, SettingsActivity::class.java), SETTINGS)
R.id.main_menu_search -> setSearchFocused(true)
}
}
@@ -404,7 +466,12 @@ class MainActivity : AppCompatActivity() {
private fun clearGalleries() {
galleries.clear()
main_recyclerview.adapter?.notifyDataSetChanged()
with(main_recyclerview.adapter as GalleryBlockAdapter?) {
this ?: return@with
this.completeFlag.clear()
this.notifyDataSetChanged()
}
main_noresult.visibility = View.INVISIBLE
main_progressbar.show()

View File

@@ -2,6 +2,7 @@ package xyz.quaver.pupil
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

View File

@@ -1,7 +1,10 @@
package xyz.quaver.pupil.adapters
import android.graphics.BitmapFactory
import android.graphics.PorterDuff
import android.util.Log
import android.util.SparseArray
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -9,6 +12,7 @@ import android.widget.LinearLayout
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
import com.google.android.material.chip.Chip
import kotlinx.android.synthetic.main.item_galleryblock.view.*
import kotlinx.coroutines.CoroutineScope
@@ -45,6 +49,7 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
var noMore = false
private val refreshTasks = SparseArray<TimerTask>()
val completeFlag = SparseBooleanArray()
val onChipClickedHandler = ArrayList<((Tag) -> Unit)>()
@@ -121,16 +126,46 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
if (refreshTasks.get(gallery.id) == null) {
val refresh = Timer(false).schedule(0, 1000) {
this@with.post {
val size = imageCache.list()?.size ?: return@post
post {
with(galleryblock_progressbar) {
progress = size
if (visibility == View.GONE) {
val reader = Json(JsonConfiguration.Stable)
.parse(ReaderItem.serializer().list, readerCache.readText())
max = reader.size
visibility = View.VISIBLE
progress = imageCache.list()?.size ?: 0
if (!readerCache.exists()) {
visibility = View.GONE
max = 0
progress = 0
holder.view.galleryblock_progress_complete.visibility = View.INVISIBLE
} else {
if (visibility == View.GONE) {
val reader = Json(JsonConfiguration.Stable)
.parse(ReaderItem.serializer().list, readerCache.readText())
max = reader.size
visibility = View.VISIBLE
}
if (progress == max) {
if (completeFlag.get(gallery.id, false)) {
with(holder.view.galleryblock_progress_complete) {
setImageResource(R.drawable.ic_progressbar)
visibility = View.VISIBLE
}
} else {
val drawable = AnimatedVectorDrawableCompat.create(context, R.drawable.ic_progressbar_complete)
with(holder.view.galleryblock_progress_complete) {
setImageDrawable(drawable)
visibility = View.VISIBLE
}
drawable?.start()
completeFlag.put(gallery.id, true)
}
} else {
with(holder.view.galleryblock_progress_complete) {
visibility = View.INVISIBLE
}
}
null
}
}
}
@@ -176,12 +211,12 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
val icon = when(tag.area) {
"male" -> {
chip.setChipBackgroundColorResource(R.color.material_blue_100)
chip.setChipBackgroundColorResource(R.color.material_blue_700)
chip.setTextColor(ContextCompat.getColor(context, android.R.color.white))
ContextCompat.getDrawable(context, R.drawable.ic_gender_male_white)
}
"female" -> {
chip.setChipBackgroundColorResource(R.color.material_pink_100)
chip.setChipBackgroundColorResource(R.color.material_pink_600)
chip.setTextColor(ContextCompat.getColor(context, android.R.color.white))
ContextCompat.getDrawable(context, R.drawable.ic_gender_female_white)
}

View File

@@ -20,11 +20,15 @@ import xyz.quaver.pupil.ReaderActivity
import java.io.File
import java.io.FileOutputStream
import java.net.URL
import java.util.*
import javax.net.ssl.HttpsURLConnection
import kotlin.collections.ArrayList
import kotlin.concurrent.schedule
class GalleryDownloader(
base: Context,
private val galleryBlock: GalleryBlock
private val galleryBlock: GalleryBlock,
_notify: Boolean = false
) : ContextWrapper(base) {
var notify: Boolean = false
@@ -33,7 +37,7 @@ class GalleryDownloader(
field = true
notificationManager.notify(galleryBlock.id, notificationBuilder.build())
if (downloadJob?.isActive != true)
if (!reader.isActive && downloadJob?.isActive != true)
field = false
} else {
field = false
@@ -63,6 +67,7 @@ class GalleryDownloader(
initNotification()
reader = CoroutineScope(Dispatchers.IO).async {
notify = _notify
val json = Json(JsonConfiguration.Stable)
val serializer = ReaderItem.serializer().list
val preference = PreferenceManager.getDefaultSharedPreferences(this@GalleryDownloader)
@@ -74,8 +79,10 @@ class GalleryDownloader(
if (cache.exists()) {
val cached = json.parse(serializer, cache.readText())
if (cached.isNotEmpty())
if (cached.isNotEmpty()) {
onReaderLoadedHandler?.invoke(cached)
return@async cached
}
}
//Cache doesn't exist. Load from internet
@@ -175,15 +182,17 @@ class GalleryDownloader(
onCompleteHandler?.invoke()
notificationBuilder
.setContentTitle(galleryBlock.title)
.setContentText(getString(R.string.reader_notification_complete))
.setProgress(0, 0, false)
Timer(false).schedule(1000) {
notificationBuilder
.setContentTitle(galleryBlock.title)
.setContentText(getString(R.string.reader_notification_complete))
.setProgress(0, 0, false)
if (notify)
notificationManager.notify(galleryBlock.id, notificationBuilder.build())
if (notify)
notificationManager.notify(galleryBlock.id, notificationBuilder.build())
notify = false
notify = false
}
remove(galleryBlock.id)
}
@@ -195,6 +204,10 @@ class GalleryDownloader(
remove(galleryBlock.id)
}
suspend fun cancelAndJoin() {
downloadJob?.cancelAndJoin()
}
fun invokeOnReaderLoaded() {
CoroutineScope(Dispatchers.Default).launch {
onReaderLoadedHandler?.invoke(reader.await())