improve threading at the file editor to better handle large files

This commit is contained in:
tibbi 2019-08-28 22:40:35 +02:00
parent 9b00f60543
commit 87c2eef8bc
1 changed files with 29 additions and 22 deletions

View File

@ -16,6 +16,7 @@ import androidx.core.view.MenuItemCompat
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.dialogs.SaveAsDialog import com.simplemobiletools.filemanager.pro.dialogs.SaveAsDialog
@ -41,7 +42,9 @@ class ReadTextActivity : SimpleActivity() {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) { if (it) {
checkIntent() read_text_view.onGlobalLayout {
checkIntent()
}
} else { } else {
toast(R.string.no_storage_permissions) toast(R.string.no_storage_permissions)
finish() finish()
@ -160,30 +163,34 @@ class ReadTextActivity : SimpleActivity() {
return return
} }
originalText = if (uri.scheme == "file") { ensureBackgroundThread {
filePath = uri.path originalText = if (uri.scheme == "file") {
val file = File(filePath) filePath = uri.path
if (file.exists()) { val file = File(filePath)
file.readText() if (file.exists()) {
file.readText()
} else {
toast(R.string.unknown_error_occurred)
""
}
} else { } else {
toast(R.string.unknown_error_occurred) try {
"" contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
} catch (e: Exception) {
showErrorToast(e)
finish()
return@ensureBackgroundThread
}
} }
} else {
try {
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
} catch (e: Exception) {
showErrorToast(e)
finish()
return
}
}
read_text_view.setText(originalText) runOnUiThread {
if (originalText.isNotEmpty()) { read_text_view.setText(originalText)
hideKeyboard() if (originalText.isNotEmpty()) {
} else { hideKeyboard()
showKeyboard(read_text_view) } else {
showKeyboard(read_text_view)
}
}
} }
} }
} }