what i got so far
This commit is contained in:
@@ -55,7 +55,6 @@ import kotlinx.coroutines.*
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import xyz.quaver.hitomi.GalleryBlock
|
||||
import xyz.quaver.hitomi.doSearch
|
||||
import xyz.quaver.hitomi.getGalleryIDsFromNozomi
|
||||
import xyz.quaver.hitomi.getSuggestionsForQuery
|
||||
@@ -68,8 +67,8 @@ import xyz.quaver.pupil.types.TagSuggestion
|
||||
import xyz.quaver.pupil.types.Tags
|
||||
import xyz.quaver.pupil.ui.dialog.GalleryDialog
|
||||
import xyz.quaver.pupil.util.*
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.download.DownloadWorker
|
||||
import xyz.quaver.pupil.util.downloader.Cache
|
||||
import xyz.quaver.pupil.util.downloader.DownloadFolderManager
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
@@ -92,7 +91,7 @@ class MainActivity : AppCompatActivity() {
|
||||
POPULAR
|
||||
}
|
||||
|
||||
private val galleries = ArrayList<GalleryBlock>()
|
||||
private val galleries = ArrayList<Int>()
|
||||
|
||||
private var query = ""
|
||||
set(value) {
|
||||
@@ -112,6 +111,8 @@ class MainActivity : AppCompatActivity() {
|
||||
private var loadingJob: Job? = null
|
||||
private var currentPage = 0
|
||||
|
||||
private lateinit var downloadFolderManager: DownloadFolderManager
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -146,15 +147,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
Intent(this, DownloadService::class.java).let {
|
||||
when {
|
||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ->
|
||||
startForegroundService(it)
|
||||
else ->
|
||||
startService(it)
|
||||
}
|
||||
}
|
||||
|
||||
downloadFolderManager = DownloadFolderManager.getInstance(this)
|
||||
checkUpdate(this)
|
||||
|
||||
initView()
|
||||
@@ -336,7 +329,7 @@ class MainActivity : AppCompatActivity() {
|
||||
with(main_fab_cancel) {
|
||||
setImageResource(R.drawable.cancel)
|
||||
setOnClickListener {
|
||||
DownloadWorker.getInstance(context).stop()
|
||||
DownloadService.cancel(this@MainActivity)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -447,19 +440,15 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
onDownloadClickedHandler = { position ->
|
||||
val galleryID = galleries[position].id
|
||||
val worker = DownloadWorker.getInstance(context)
|
||||
val galleryID = galleries[position]
|
||||
if (Preferences["cache_disable"])
|
||||
Toast.makeText(context, R.string.settings_download_when_cache_disable_warning, Toast.LENGTH_SHORT).show()
|
||||
else {
|
||||
if (worker.progress.indexOfKey(galleryID) >= 0 && Cache(context).isDownloading(galleryID)) { //download in progress
|
||||
Cache(context).setDownloading(galleryID, false)
|
||||
worker.cancel(galleryID)
|
||||
if (downloadFolderManager.isDownloading(galleryID)) { //download in progress
|
||||
DownloadService.cancel(this@MainActivity, galleryID)
|
||||
}
|
||||
else {
|
||||
Cache(context).setDownloading(galleryID, true)
|
||||
|
||||
worker.queue.add(galleryID)
|
||||
DownloadService.download(this@MainActivity, galleryID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,25 +456,20 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
onDeleteClickedHandler = { position ->
|
||||
val galleryID = galleries[position].id
|
||||
val galleryID = galleries[position]
|
||||
DownloadService.delete(this@MainActivity, galleryID)
|
||||
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
DownloadWorker.getInstance(context).cancel(galleryID)
|
||||
histories.remove(galleryID)
|
||||
|
||||
Cache(context).getCachedGallery(galleryID).deleteRecursively()
|
||||
if (this@MainActivity.mode != Mode.SEARCH)
|
||||
runOnUiThread {
|
||||
cancelFetch()
|
||||
clearGalleries()
|
||||
fetchGalleries(query, sortMode)
|
||||
loadBlocks()
|
||||
}
|
||||
|
||||
histories.remove(galleryID)
|
||||
|
||||
if (this@MainActivity.mode != Mode.SEARCH)
|
||||
runOnUiThread {
|
||||
cancelFetch()
|
||||
clearGalleries()
|
||||
fetchGalleries(query, sortMode)
|
||||
loadBlocks()
|
||||
}
|
||||
|
||||
completeFlag.put(galleryID, false)
|
||||
}
|
||||
completeFlag.put(galleryID, false)
|
||||
|
||||
closeAllItems()
|
||||
}
|
||||
@@ -496,8 +480,7 @@ class MainActivity : AppCompatActivity() {
|
||||
return@listener
|
||||
|
||||
val intent = Intent(this@MainActivity, ReaderActivity::class.java)
|
||||
val gallery = galleries[position]
|
||||
intent.putExtra("galleryID", gallery.id)
|
||||
intent.putExtra("galleryID", galleries[position])
|
||||
|
||||
//TODO: Maybe sprinkling some transitions will be nice :D
|
||||
startActivity(intent)
|
||||
@@ -507,7 +490,7 @@ class MainActivity : AppCompatActivity() {
|
||||
if (v !is CardView)
|
||||
return@listener false
|
||||
|
||||
val galleryID = galleries[position].id
|
||||
val galleryID = galleries[position]
|
||||
|
||||
GalleryDialog(
|
||||
this@MainActivity,
|
||||
@@ -762,7 +745,7 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
if (!favoritesFile.exists()) {
|
||||
favoritesFile.createNewFile()
|
||||
favoritesFile.writeText(Json.encodeToString(Tags()))
|
||||
favoritesFile.writeText("[]")
|
||||
}
|
||||
|
||||
setOnLeftMenuClickListener(object: FloatingSearchView.OnLeftMenuClickListener {
|
||||
@@ -788,7 +771,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
R.id.main_menu_sort_newest -> {
|
||||
sortMode = SortMode.NEWEST
|
||||
sortMode = MainActivity.SortMode.NEWEST
|
||||
it.isChecked = true
|
||||
|
||||
runOnUiThread {
|
||||
@@ -1023,7 +1006,7 @@ class MainActivity : AppCompatActivity() {
|
||||
totalItems = it.size
|
||||
}
|
||||
}
|
||||
else -> doSearch("$defaultQuery $query", sortMode == SortMode.POPULAR).also {
|
||||
else -> doSearch("$defaultQuery $query", sortMode == MainActivity.SortMode.POPULAR).also {
|
||||
totalItems = it.size
|
||||
}
|
||||
}
|
||||
@@ -1107,16 +1090,16 @@ class MainActivity : AppCompatActivity() {
|
||||
for (chunk in chunks)
|
||||
chunk.map { galleryID ->
|
||||
async {
|
||||
Cache(this@MainActivity).getGalleryBlock(galleryID)
|
||||
Cache.getInstance(this@MainActivity, galleryID).getGalleryBlock()?.let {
|
||||
galleryID
|
||||
}
|
||||
}
|
||||
}.forEach {
|
||||
val galleryBlock = it.await()
|
||||
it.await()?.also {
|
||||
withContext(Dispatchers.Main) {
|
||||
main_progressbar.hide()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
main_progressbar.hide()
|
||||
|
||||
if (galleryBlock != null) {
|
||||
galleries.add(galleryBlock)
|
||||
galleries.add(it)
|
||||
main_recyclerview.adapter!!.notifyItemInserted(galleries.size - 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,10 +18,14 @@
|
||||
|
||||
package xyz.quaver.pupil.ui
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.graphics.drawable.Animatable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import android.view.*
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
@@ -47,9 +51,10 @@ import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.adapters.ReaderAdapter
|
||||
import xyz.quaver.pupil.favorites
|
||||
import xyz.quaver.pupil.histories
|
||||
import xyz.quaver.pupil.services.DownloadService
|
||||
import xyz.quaver.pupil.util.Preferences
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.download.DownloadWorker
|
||||
import xyz.quaver.pupil.util.downloader.Cache
|
||||
import xyz.quaver.pupil.util.downloader.DownloadFolderManager
|
||||
import java.util.*
|
||||
import kotlin.concurrent.schedule
|
||||
import kotlin.concurrent.timer
|
||||
@@ -72,6 +77,22 @@ class ReaderActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private lateinit var cache: Cache
|
||||
var downloader: DownloadService? = null
|
||||
private val conn = object: ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
|
||||
downloader = (service as DownloadService.Binder).service
|
||||
Log.i("PUPILD", "CON")
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(name: ComponentName?) {
|
||||
downloader = null
|
||||
Log.i("PUPILD", "DIS")
|
||||
}
|
||||
}
|
||||
|
||||
private var deleteOnExit = true
|
||||
|
||||
private val timer = Timer()
|
||||
private var autoTimer: Timer? = null
|
||||
|
||||
@@ -81,6 +102,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_reader)
|
||||
|
||||
title = getString(R.string.reader_loading)
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(false)
|
||||
@@ -89,23 +111,18 @@ class ReaderActivity : AppCompatActivity() {
|
||||
WindowManager.LayoutParams.FLAG_SECURE,
|
||||
WindowManager.LayoutParams.FLAG_SECURE)
|
||||
|
||||
setContentView(R.layout.activity_reader)
|
||||
|
||||
handleIntent(intent)
|
||||
|
||||
histories.add(galleryID)
|
||||
cache = Cache.getInstance(this, galleryID)
|
||||
FirebaseCrashlytics.getInstance().setCustomKey("GalleryID", galleryID)
|
||||
|
||||
if (galleryID == 0) {
|
||||
onBackPressed()
|
||||
return
|
||||
}
|
||||
|
||||
initView()
|
||||
if (Preferences["cache_disable"]) {
|
||||
reader_download_progressbar.visibility = View.GONE
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val reader = Cache(this@ReaderActivity).getReader(galleryID)
|
||||
val reader = cache.getReader()
|
||||
|
||||
launch(Dispatchers.Main) initDownloader@{
|
||||
if (reader == null) {
|
||||
@@ -115,6 +132,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
return@initDownloader
|
||||
}
|
||||
|
||||
histories.add(galleryID)
|
||||
(reader_recyclerview.adapter as ReaderAdapter).apply {
|
||||
this.reader = reader
|
||||
notifyDataSetChanged()
|
||||
@@ -132,6 +150,8 @@ class ReaderActivity : AppCompatActivity() {
|
||||
}
|
||||
} else
|
||||
initDownloader()
|
||||
|
||||
initView()
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
@@ -187,9 +207,9 @@ class ReaderActivity : AppCompatActivity() {
|
||||
R.id.reader_menu_page_indicator -> {
|
||||
val view = LayoutInflater.from(this).inflate(R.layout.dialog_numberpicker, reader_layout, false)
|
||||
with(view.dialog_number_picker) {
|
||||
minValue=1
|
||||
maxValue=Cache(context).getReaderOrNull(galleryID)?.galleryInfo?.files?.size ?: 0
|
||||
value=currentPage
|
||||
minValue = 1
|
||||
maxValue = cache.metadata.reader?.galleryInfo?.files?.size ?: 0
|
||||
value = currentPage
|
||||
}
|
||||
val dialog = AlertDialog.Builder(this).apply {
|
||||
setView(view)
|
||||
@@ -224,8 +244,12 @@ class ReaderActivity : AppCompatActivity() {
|
||||
timer.cancel()
|
||||
(reader_recyclerview?.adapter as? ReaderAdapter)?.timer?.cancel()
|
||||
|
||||
if (!Cache(this).isDownloading(galleryID))
|
||||
DownloadWorker.getInstance(this@ReaderActivity).cancel(galleryID)
|
||||
if (deleteOnExit) {
|
||||
downloader?.cancel(galleryID)
|
||||
DownloadFolderManager.getInstance(this).deleteDownloadFolder(galleryID)
|
||||
}
|
||||
|
||||
unbindService(conn)
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
@@ -261,16 +285,16 @@ class ReaderActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun initDownloader() {
|
||||
val worker = DownloadWorker.getInstance(this).apply {
|
||||
cancel(galleryID)
|
||||
queue.add(galleryID)
|
||||
}
|
||||
DownloadService.download(this, galleryID)
|
||||
bindService(Intent(this, DownloadService::class.java), conn, BIND_AUTO_CREATE)
|
||||
|
||||
timer.schedule(1000, 1000) {
|
||||
if (worker.progress.indexOfKey(galleryID) < 0) //loading
|
||||
val downloader = downloader ?: return@schedule
|
||||
|
||||
if (downloader.progress.indexOfKey(galleryID) < 0) //loading
|
||||
return@schedule
|
||||
|
||||
if (worker.progress[galleryID] == null) { //Gallery not found
|
||||
if (downloader.progress[galleryID] == null) { //Gallery not found
|
||||
timer.cancel()
|
||||
Snackbar
|
||||
.make(reader_layout, R.string.reader_failed_to_find_gallery, Snackbar.LENGTH_INDEFINITE)
|
||||
@@ -279,14 +303,13 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
runOnUiThread {
|
||||
reader_download_progressbar.max = reader_recyclerview.adapter?.itemCount ?: 0
|
||||
reader_download_progressbar.progress = worker.progress[galleryID]?.count { it.isInfinite() } ?: 0
|
||||
reader_download_progressbar.progress = downloader.progress[galleryID]?.count { it.isInfinite() } ?: 0
|
||||
reader_progressbar.max = reader_recyclerview.adapter?.itemCount ?: 0
|
||||
|
||||
if (title == getString(R.string.reader_loading)) {
|
||||
val reader = Cache(this@ReaderActivity).getReaderOrNull(galleryID)
|
||||
val reader = cache.metadata.reader
|
||||
|
||||
if (reader != null) {
|
||||
|
||||
with (reader_recyclerview.adapter as ReaderAdapter) {
|
||||
this.reader = reader
|
||||
notifyDataSetChanged()
|
||||
@@ -304,7 +327,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
if (worker.progress[galleryID]?.all { it.isInfinite() } == true) { //Download finished
|
||||
if (downloader.isCompleted(galleryID)) { //Download finished
|
||||
reader_download_progressbar.visibility = View.GONE
|
||||
|
||||
animateDownloadFAB(false)
|
||||
@@ -315,7 +338,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
private fun initView() {
|
||||
with(reader_recyclerview) {
|
||||
adapter = ReaderAdapter(Glide.with(this@ReaderActivity), galleryID).apply {
|
||||
adapter = ReaderAdapter(this@ReaderActivity, galleryID).apply {
|
||||
onItemClickListener = {
|
||||
if (isScroll) {
|
||||
isScroll = false
|
||||
@@ -350,19 +373,18 @@ class ReaderActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
with(reader_fab_download) {
|
||||
animateDownloadFAB(Cache(context).isDownloading(galleryID)) //If download in progress, animate button
|
||||
animateDownloadFAB(DownloadFolderManager.getInstance(this@ReaderActivity).getDownloadFolder(galleryID) != null) //If download in progress, animate button
|
||||
|
||||
setOnClickListener {
|
||||
if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("cache_disable", false))
|
||||
Toast.makeText(context, R.string.settings_download_when_cache_disable_warning, Toast.LENGTH_SHORT).show()
|
||||
else {
|
||||
if (Cache(context).isDownloading(galleryID)) {
|
||||
Cache(context).setDownloading(galleryID, false)
|
||||
|
||||
animateDownloadFAB(false)
|
||||
} else {
|
||||
Cache(context).setDownloading(galleryID, true)
|
||||
if (deleteOnExit) {
|
||||
deleteOnExit = false
|
||||
cache.moveToDownload()
|
||||
animateDownloadFAB(true)
|
||||
} else {
|
||||
animateDownloadFAB(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -371,10 +393,8 @@ class ReaderActivity : AppCompatActivity() {
|
||||
with(reader_fab_retry) {
|
||||
setImageResource(R.drawable.refresh)
|
||||
setOnClickListener {
|
||||
DownloadWorker.getInstance(context).let {
|
||||
it.cancel(galleryID)
|
||||
it.queue.add(galleryID)
|
||||
}
|
||||
downloader?.cancel(galleryID)
|
||||
downloader?.download(galleryID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,8 +470,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
icon?.registerAnimationCallback(object : Animatable2Compat.AnimationCallback() {
|
||||
override fun onAnimationEnd(drawable: Drawable?) {
|
||||
val worker = DownloadWorker.getInstance(context)
|
||||
if (worker.progress[galleryID]?.all { it.isInfinite() } == true) // If download is finished, stop animating
|
||||
if (downloader?.isCompleted(galleryID) == true) // If download is finished, stop animating
|
||||
post {
|
||||
setImageResource(R.drawable.ic_download)
|
||||
labelText = getString(R.string.reader_fab_download_cancel)
|
||||
|
||||
@@ -27,15 +27,12 @@ import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import android.view.WindowManager
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.settings_activity.*
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import net.rdrei.android.dirchooser.DirectoryChooserActivity
|
||||
import xyz.quaver.io.util.toFile
|
||||
import xyz.quaver.pupil.Pupil
|
||||
import xyz.quaver.io.FileX
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.favorites
|
||||
import xyz.quaver.pupil.ui.fragment.LockSettingsFragment
|
||||
@@ -126,16 +123,14 @@ class SettingsActivity : AppCompatActivity() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
|
||||
contentResolver.takePersistableUriPermission(uri, takeFlags)
|
||||
|
||||
val file = uri.toFile(this)
|
||||
|
||||
if (file?.canWrite() != true)
|
||||
if (FileX(this, uri).canWrite())
|
||||
Preferences["download_folder"] = uri.toString()
|
||||
else
|
||||
Snackbar.make(
|
||||
settings,
|
||||
R.string.settings_dl_location_not_writable,
|
||||
R.string.settings_download_folder_not_writable,
|
||||
Snackbar.LENGTH_LONG
|
||||
).show()
|
||||
else
|
||||
Preferences["dl_location"] = file.canonicalPath
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,11 +141,11 @@ class SettingsActivity : AppCompatActivity() {
|
||||
if (!File(directory).canWrite())
|
||||
Snackbar.make(
|
||||
settings,
|
||||
R.string.settings_dl_location_not_writable,
|
||||
R.string.settings_download_folder_not_writable,
|
||||
Snackbar.LENGTH_LONG
|
||||
).show()
|
||||
else
|
||||
Preferences["dl_location"] = File(directory).canonicalPath
|
||||
Preferences["download_folder"] = File(directory).canonicalPath
|
||||
}
|
||||
}
|
||||
else -> super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
@@ -18,21 +18,19 @@
|
||||
|
||||
package xyz.quaver.pupil.ui.dialog
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.Dialog
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.RadioButton
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import kotlinx.android.synthetic.main.item_dl_location.view.*
|
||||
import androidx.core.net.toUri
|
||||
import kotlinx.android.synthetic.main.item_download_folder.view.*
|
||||
import net.rdrei.android.dirchooser.DirectoryChooserActivity
|
||||
import net.rdrei.android.dirchooser.DirectoryChooserConfig
|
||||
import xyz.quaver.pupil.R
|
||||
@@ -44,7 +42,7 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) {
|
||||
private val buttons = mutableListOf<Pair<RadioButton, File?>>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
setTitle(R.string.settings_dl_location)
|
||||
setTitle(R.string.settings_download_folder)
|
||||
|
||||
setView(build())
|
||||
|
||||
@@ -54,7 +52,7 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) {
|
||||
}
|
||||
|
||||
private fun build() : View {
|
||||
val view = layoutInflater.inflate(R.layout.dialog_dl_location, null) as LinearLayout
|
||||
val view = layoutInflater.inflate(R.layout.dialog_download_folder, null) as LinearLayout
|
||||
|
||||
val externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null)
|
||||
|
||||
@@ -62,13 +60,13 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) {
|
||||
|
||||
dir ?: return@forEachIndexed
|
||||
|
||||
view.addView(layoutInflater.inflate(R.layout.item_dl_location, view, false).apply {
|
||||
view.addView(layoutInflater.inflate(R.layout.item_download_folder, view, false).apply {
|
||||
location_type.text = context.getString(when (index) {
|
||||
0 -> R.string.settings_dl_location_internal
|
||||
else -> R.string.settings_dl_location_removable
|
||||
0 -> R.string.settings_download_folder_internal
|
||||
else -> R.string.settings_download_folder_removable
|
||||
})
|
||||
location_available.text = context.getString(
|
||||
R.string.settings_dl_location_available,
|
||||
R.string.settings_download_folder_available,
|
||||
byteToString(dir.freeSpace)
|
||||
)
|
||||
setOnClickListener {
|
||||
@@ -76,14 +74,14 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) {
|
||||
pair.first.isChecked = false
|
||||
}
|
||||
button.performClick()
|
||||
Preferences["dl_location"] = dir.canonicalPath
|
||||
Preferences["download_folder"] = dir.toUri().toString()
|
||||
}
|
||||
buttons.add(button to dir)
|
||||
})
|
||||
}
|
||||
|
||||
view.addView(layoutInflater.inflate(R.layout.item_dl_location, view, false).apply {
|
||||
location_type.text = context.getString(R.string.settings_dl_location_custom)
|
||||
view.addView(layoutInflater.inflate(R.layout.item_download_folder, view, false).apply {
|
||||
location_type.text = context.getString(R.string.settings_download_folder_custom)
|
||||
setOnClickListener {
|
||||
buttons.forEach { pair ->
|
||||
pair.first.isChecked = false
|
||||
@@ -91,17 +89,12 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) {
|
||||
button.performClick()
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
|
||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), R.id.request_write_permission_and_saf.normalizeID())
|
||||
else {
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
|
||||
putExtra("android.content.extra.SHOW_ADVANCED", true)
|
||||
}
|
||||
|
||||
activity.startActivityForResult(intent, R.id.request_download_folder.normalizeID())
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
|
||||
putExtra("android.content.extra.SHOW_ADVANCED", true)
|
||||
}
|
||||
|
||||
activity.startActivityForResult(intent, R.id.request_download_folder.normalizeID())
|
||||
|
||||
dismiss()
|
||||
} else { // Can't use SAF on old Androids!
|
||||
val config = DirectoryChooserConfig.builder()
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.LinearLayout.LayoutParams
|
||||
@@ -36,15 +37,10 @@ import kotlinx.android.synthetic.main.dialog_gallery.*
|
||||
import kotlinx.android.synthetic.main.dialog_gallery_details.view.*
|
||||
import kotlinx.android.synthetic.main.dialog_gallery_dotindicator.view.*
|
||||
import kotlinx.android.synthetic.main.item_gallery_details.view.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.*
|
||||
import xyz.quaver.hitomi.Gallery
|
||||
import xyz.quaver.hitomi.GalleryBlock
|
||||
import xyz.quaver.hitomi.getGallery
|
||||
import xyz.quaver.pupil.BuildConfig
|
||||
import xyz.quaver.pupil.Pupil
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.adapters.GalleryBlockAdapter
|
||||
import xyz.quaver.pupil.adapters.ThumbnailPageAdapter
|
||||
@@ -52,7 +48,7 @@ import xyz.quaver.pupil.histories
|
||||
import xyz.quaver.pupil.types.Tag
|
||||
import xyz.quaver.pupil.ui.ReaderActivity
|
||||
import xyz.quaver.pupil.util.ItemClickSupport
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.downloader.Cache
|
||||
import xyz.quaver.pupil.util.wordCapitalize
|
||||
|
||||
class GalleryDialog(context: Context, private val glide: RequestManager, private val galleryID: Int) : Dialog(context) {
|
||||
@@ -131,7 +127,7 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
|
||||
|
||||
private fun addDetails(gallery: Gallery) {
|
||||
val inflater = LayoutInflater.from(context)
|
||||
|
||||
|
||||
inflater.inflate(R.layout.dialog_gallery_details, gallery_contents, false).apply {
|
||||
gallery_details.setText(R.string.gallery_details)
|
||||
|
||||
@@ -230,7 +226,7 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
|
||||
|
||||
private fun addRelated(gallery: Gallery) {
|
||||
val inflater = LayoutInflater.from(context)
|
||||
val galleries = ArrayList<GalleryBlock>()
|
||||
val galleries = ArrayList<Int>()
|
||||
|
||||
val adapter = GalleryBlockAdapter(glide, galleries).apply {
|
||||
onChipClickedHandler.add { tag ->
|
||||
@@ -240,19 +236,6 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
|
||||
}
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
gallery.related.forEachIndexed { i, galleryID ->
|
||||
async(Dispatchers.IO) {
|
||||
Cache(context).getGalleryBlock(galleryID)
|
||||
}.let {
|
||||
val galleryBlock = it.await() ?: return@let
|
||||
|
||||
galleries.add(galleryBlock)
|
||||
adapter.notifyItemInserted(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inflater.inflate(R.layout.dialog_gallery_details, gallery_contents, false).apply {
|
||||
gallery_details.setText(R.string.gallery_related)
|
||||
|
||||
@@ -263,15 +246,15 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
|
||||
ItemClickSupport.addTo(this).apply {
|
||||
onItemClickListener = { _, position, _ ->
|
||||
context.startActivity(Intent(context, ReaderActivity::class.java).apply {
|
||||
putExtra("galleryID", galleries[position].id)
|
||||
putExtra("galleryID", galleries[position])
|
||||
})
|
||||
histories.add(galleries[position].id)
|
||||
histories.add(galleries[position])
|
||||
}
|
||||
onItemLongClickListener = { _, position, _ ->
|
||||
GalleryDialog(
|
||||
context,
|
||||
glide,
|
||||
galleries[position].id
|
||||
galleries[position]
|
||||
).apply {
|
||||
onChipClickedHandler.add { tag ->
|
||||
this@GalleryDialog.onChipClickedHandler.forEach { it.invoke(tag) }
|
||||
@@ -287,6 +270,18 @@ class GalleryDialog(context: Context, private val glide: RequestManager, private
|
||||
}.let {
|
||||
gallery_contents.addView(it)
|
||||
}
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
gallery.related.forEach { galleryID ->
|
||||
Cache.getInstance(context, galleryID).getGalleryBlock()?.let {
|
||||
galleries.add(galleryID)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
adapter.notifyItemInserted(galleries.size-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import androidx.preference.PreferenceFragmentCompat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import xyz.quaver.io.FileX
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.histories
|
||||
import xyz.quaver.pupil.ui.LockActivity
|
||||
@@ -141,7 +142,7 @@ class SettingsFragment :
|
||||
setNegativeButton(android.R.string.no) { _, _ -> }
|
||||
}.show()
|
||||
}
|
||||
"dl_location" -> {
|
||||
"download_folder" -> {
|
||||
DownloadLocationDialog(requireActivity()).show()
|
||||
}
|
||||
"default_query" -> {
|
||||
@@ -208,9 +209,6 @@ class SettingsFragment :
|
||||
"proxy" -> {
|
||||
summary = context?.let { getProxyInfo().type.name }
|
||||
}
|
||||
"dl_location" -> {
|
||||
summary = context?.let { getDownloadDirectory(it).canonicalPath }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -275,8 +273,14 @@ class SettingsFragment :
|
||||
|
||||
onPreferenceClickListener = this@SettingsFragment
|
||||
}
|
||||
"dl_location" -> {
|
||||
summary = getDownloadDirectory(requireContext()).canonicalPath
|
||||
"download_folder" -> {
|
||||
setSummaryProvider {
|
||||
val uri: String = Preferences[it.key]
|
||||
|
||||
kotlin.runCatching {
|
||||
FileX(context, uri).canonicalPath
|
||||
}.getOrElse { "" }
|
||||
}
|
||||
|
||||
onPreferenceClickListener = this@SettingsFragment
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user