From eebeece8565f985d1a4039d7430f650aa91d87e1 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 16 Sep 2020 16:16:54 +0200 Subject: LogViewerActivity: simplify scoping Signed-off-by: Jason A. Donenfeld --- .../android/activity/LogViewerActivity.kt | 42 ++++++++++------------ 1 file changed, 19 insertions(+), 23 deletions(-) (limited to 'ui/src/main/java/com') diff --git a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt index da1618f3..456ad6ce 100644 --- a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt @@ -147,7 +147,8 @@ class LogViewerActivity : AppCompatActivity() { true } R.id.save_log -> { - lifecycleScope.launch(Dispatchers.IO) { saveLog() } + saveButton?.isEnabled = false + lifecycleScope.launch { saveLog() } true } else -> super.onOptionsItemSelected(item) @@ -155,31 +156,26 @@ class LogViewerActivity : AppCompatActivity() { } private suspend fun saveLog() { - withContext(Dispatchers.Main.immediate) { - saveButton?.isEnabled = false - withContext(Dispatchers.IO) { - var exception: Throwable? = null - var outputFile: DownloadsFileSaver.DownloadsFile? = null - try { - outputFile = DownloadsFileSaver.save(this@LogViewerActivity, "wireguard-log.txt", "text/plain", true) - outputFile.outputStream.use { - it.write(rawLogLines.toString().toByteArray(Charsets.UTF_8)) - } - } catch (e: Throwable) { - outputFile?.delete() - exception = e - } - withContext(Dispatchers.Main.immediate) { - saveButton?.isEnabled = true - Snackbar.make(findViewById(android.R.id.content), - if (exception == null) getString(R.string.log_export_success, outputFile?.fileName) - else getString(R.string.log_export_error, ErrorMessages[exception]), - if (exception == null) Snackbar.LENGTH_SHORT else Snackbar.LENGTH_LONG) - .setAnchorView(binding.shareFab) - .show() + var exception: Throwable? = null + var outputFile: DownloadsFileSaver.DownloadsFile? = null + withContext(Dispatchers.IO) { + try { + outputFile = DownloadsFileSaver.save(this@LogViewerActivity, "wireguard-log.txt", "text/plain", true) + outputFile?.outputStream.use { + it?.write(rawLogLines.toString().toByteArray(Charsets.UTF_8)) } + } catch (e: Throwable) { + outputFile?.delete() + exception = e } } + saveButton?.isEnabled = true + Snackbar.make(findViewById(android.R.id.content), + if (exception == null) getString(R.string.log_export_success, outputFile?.fileName) + else getString(R.string.log_export_error, ErrorMessages[exception]), + if (exception == null) Snackbar.LENGTH_SHORT else Snackbar.LENGTH_LONG) + .setAnchorView(binding.shareFab) + .show() } private suspend fun streamingLog() = withContext(Dispatchers.IO) { -- cgit v1.2.3