Skip to content

Commit 089f7c0

Browse files
fix(backup): Improve error handling for word list import
This commit enhances the error handling for the word list import feature. Previously, importing a word list from a file did not handle potential exceptions, such as file read errors or the inability to open the selected file. Now, the file import logic is wrapped in a `try-catch` block to gracefully handle any exceptions during the process. Additionally, it uses a `use` block to ensure the `InputStream` is automatically closed. If an error occurs, a "Failed to restore words" message is displayed to the user. A check is also added to show an "Unable to open file" message if the `ContentResolver` fails to provide an `InputStream`.
1 parent 796b735 commit 089f7c0

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,16 @@ class MainActivity : AppCompatActivity() {
311311
if (result.resultCode == RESULT_OK) {
312312
result.data?.data?.let { uri ->
313313
// Handle the imported file
314-
val inputStream = contentResolver.openInputStream(uri)
315-
val importedWords = readWordsFromFile(inputStream)
316-
saveCustomWordList(importedWords)
317-
AppReloader.restartApp(applicationContext)
314+
try {
315+
contentResolver.openInputStream(uri)?.use { inputStream ->
316+
val importedWords = readWordsFromFile(inputStream)
317+
saveCustomWordList(importedWords)
318+
AppReloader.restartApp(applicationContext)
319+
} ?: showLongToast("Unable to open file")
320+
} catch (e: Exception) {
321+
e.printStackTrace()
322+
showLongToast("Failed to restore words")
323+
}
318324
}
319325
}
320326
}

0 commit comments

Comments
 (0)