Async file loading in file viewers

This commit is contained in:
Matéo Duparc 2022-10-04 13:30:51 +02:00
parent cdc269f2f7
commit 34aad2596d
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
5 changed files with 35 additions and 26 deletions

View File

@ -1,10 +1,7 @@
package sushi.hardcore.droidfs package sushi.hardcore.droidfs
import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Parcelable
import android.view.WindowManager import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
@ -17,8 +14,8 @@ open class BaseActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this) sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
if (shouldCheckTheme && applyCustomTheme) {
themeValue = sharedPrefs.getString(ConstValues.THEME_VALUE_KEY, ConstValues.DEFAULT_THEME_VALUE)!! themeValue = sharedPrefs.getString(ConstValues.THEME_VALUE_KEY, ConstValues.DEFAULT_THEME_VALUE)!!
if (shouldCheckTheme && applyCustomTheme) {
when (themeValue) { when (themeValue) {
"black_green" -> setTheme(R.style.BlackGreen) "black_green" -> setTheme(R.style.BlackGreen)
"dark_red" -> setTheme(R.style.DarkRed) "dark_red" -> setTheme(R.style.DarkRed)

View File

@ -6,6 +6,11 @@ import androidx.activity.addCallback
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import sushi.hardcore.droidfs.BaseActivity import sushi.hardcore.droidfs.BaseActivity
import sushi.hardcore.droidfs.ConstValues import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.R import sushi.hardcore.droidfs.R
@ -81,10 +86,15 @@ abstract class FileViewerActivity: BaseActivity() {
} }
} }
protected fun loadWholeFile(path: String, fileSize: Long? = null): ByteArray? { protected fun loadWholeFile(path: String, fileSize: Long? = null, callback: (ByteArray) -> Unit) {
lifecycleScope.launch(Dispatchers.IO) {
val result = encryptedVolume.loadWholeFile(path, size = fileSize) val result = encryptedVolume.loadWholeFile(path, size = fileSize)
if (result.second != 0) { if (isActive) {
val dialog = CustomAlertDialogBuilder(this, themeValue) withContext(Dispatchers.Main) {
if (result.second == 0) {
callback(result.first!!)
} else {
val dialog = CustomAlertDialogBuilder(this@FileViewerActivity, themeValue)
.setTitle(R.string.error) .setTitle(R.string.error)
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() } .setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
@ -96,7 +106,9 @@ abstract class FileViewerActivity: BaseActivity() {
} }
dialog.show() dialog.show()
} }
return result.first }
}
}
} }
protected fun createPlaylist() { protected fun createPlaylist() {

View File

@ -163,13 +163,13 @@ class ImageViewer: FileViewerActivity() {
} }
private fun loadImage(){ private fun loadImage(){
fileName = File(filePath).name
binding.textFilename.text = fileName
requestBuilder = null requestBuilder = null
loadWholeFile(filePath)?.let { loadWholeFile(filePath) {
originalOrientation = 0f originalOrientation = 0f
requestBuilder = Glide.with(this).load(it) requestBuilder = Glide.with(this).load(it)
requestBuilder?.into(binding.imageViewer) requestBuilder?.into(binding.imageViewer)
fileName = File(filePath).name
binding.textFilename.text = fileName
rotationAngle = originalOrientation rotationAngle = originalOrientation
} }
} }

View File

@ -22,7 +22,7 @@ class PdfViewer: FileViewerActivity() {
val fileName = File(filePath).name val fileName = File(filePath).name
title = fileName title = fileName
val fileSize = encryptedVolume.getAttr(filePath)?.size val fileSize = encryptedVolume.getAttr(filePath)?.size
loadWholeFile(filePath, fileSize)?.let { loadWholeFile(filePath, fileSize) {
pdfViewer.loadPdf(ByteArrayInputStream(it), fileName, fileSize) pdfViewer.loadPdf(ByteArrayInputStream(it), fileName, fileSize)
} }
} }

View File

@ -23,8 +23,10 @@ class TextEditor: FileViewerActivity() {
} }
override fun viewFile() { override fun viewFile() {
loadWholeFile(filePath)?.let {
fileName = File(filePath).name fileName = File(filePath).name
title = fileName
supportActionBar?.setDisplayHomeAsUpEnabled(true)
loadWholeFile(filePath) {
try { try {
loadLayout(String(it)) loadLayout(String(it))
} catch (e: OutOfMemoryError){ } catch (e: OutOfMemoryError){
@ -43,8 +45,6 @@ class TextEditor: FileViewerActivity() {
} else { } else {
setContentView(R.layout.activity_text_editor) setContentView(R.layout.activity_text_editor)
} }
title = fileName
supportActionBar?.setDisplayHomeAsUpEnabled(true)
editor = findViewById(R.id.text_editor) editor = findViewById(R.id.text_editor)
editor.setText(fileContent) editor.setText(fileContent)
editor.addTextChangedListener(object: TextWatcher { editor.addTextChangedListener(object: TextWatcher {