Skip to content

Commit 87c2eef

Browse files
committed
improve threading at the file editor to better handle large files
1 parent 9b00f60 commit 87c2eef

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.core.view.MenuItemCompat
1616
import com.simplemobiletools.commons.extensions.*
1717
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
1818
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
19+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1920
import com.simplemobiletools.commons.models.FileDirItem
2021
import com.simplemobiletools.filemanager.pro.R
2122
import com.simplemobiletools.filemanager.pro.dialogs.SaveAsDialog
@@ -41,7 +42,9 @@ class ReadTextActivity : SimpleActivity() {
4142

4243
handlePermission(PERMISSION_WRITE_STORAGE) {
4344
if (it) {
44-
checkIntent()
45+
read_text_view.onGlobalLayout {
46+
checkIntent()
47+
}
4548
} else {
4649
toast(R.string.no_storage_permissions)
4750
finish()
@@ -160,30 +163,34 @@ class ReadTextActivity : SimpleActivity() {
160163
return
161164
}
162165

163-
originalText = if (uri.scheme == "file") {
164-
filePath = uri.path
165-
val file = File(filePath)
166-
if (file.exists()) {
167-
file.readText()
166+
ensureBackgroundThread {
167+
originalText = if (uri.scheme == "file") {
168+
filePath = uri.path
169+
val file = File(filePath)
170+
if (file.exists()) {
171+
file.readText()
172+
} else {
173+
toast(R.string.unknown_error_occurred)
174+
""
175+
}
168176
} else {
169-
toast(R.string.unknown_error_occurred)
170-
""
171-
}
172-
} else {
173-
try {
174-
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
175-
} catch (e: Exception) {
176-
showErrorToast(e)
177-
finish()
178-
return
177+
try {
178+
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
179+
} catch (e: Exception) {
180+
showErrorToast(e)
181+
finish()
182+
return@ensureBackgroundThread
183+
}
179184
}
180-
}
181185

182-
read_text_view.setText(originalText)
183-
if (originalText.isNotEmpty()) {
184-
hideKeyboard()
185-
} else {
186-
showKeyboard(read_text_view)
186+
runOnUiThread {
187+
read_text_view.setText(originalText)
188+
if (originalText.isNotEmpty()) {
189+
hideKeyboard()
190+
} else {
191+
showKeyboard(read_text_view)
192+
}
193+
}
187194
}
188195
}
189196
}

0 commit comments

Comments
 (0)