From dd52f31e643a13d259e2ecbabb33630bec1073b1 Mon Sep 17 00:00:00 2001 From: Goofyz Leung Date: Wed, 20 Dec 2023 14:08:34 +0800 Subject: [PATCH] refactor: standardize deploy process with a result dialog Add `rimeActionWIthResultDialog()` to run deploy and display a result dialog. Cannot add to `schemaPickers` because the token of windows maybe null if it is started by a keyboard view. --- .../trime/ui/fragments/ProfileFragment.kt | 3 +- .../osfans/trime/ui/main/PrefMainActivity.kt | 24 +++----------- .../java/com/osfans/trime/util/DialogUtils.kt | 31 ++++++++++++++++--- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/osfans/trime/ui/fragments/ProfileFragment.kt b/app/src/main/java/com/osfans/trime/ui/fragments/ProfileFragment.kt index e1db7d1696..d5ad762e52 100644 --- a/app/src/main/java/com/osfans/trime/ui/fragments/ProfileFragment.kt +++ b/app/src/main/java/com/osfans/trime/ui/fragments/ProfileFragment.kt @@ -30,6 +30,7 @@ import com.osfans.trime.ui.components.PaddingPreferenceFragment import com.osfans.trime.ui.main.MainViewModel import com.osfans.trime.util.appContext import com.osfans.trime.util.formatDateTime +import com.osfans.trime.util.rimeActionWithResultDialog import com.osfans.trime.util.withLoadingDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -72,7 +73,7 @@ class ProfileFragment : } get("profile_sync_user_data")?.setOnPreferenceClickListener { lifecycleScope.launch { - withContext(Dispatchers.IO) { + this@ProfileFragment.context?.rimeActionWithResultDialog("rime.trime", "W", 1) { Rime.syncRimeUserData() RimeWrapper.deploy() } diff --git a/app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt b/app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt index 57d0945558..bfa90e7cc9 100644 --- a/app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt +++ b/app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt @@ -30,12 +30,10 @@ import com.osfans.trime.databinding.ActivityPrefBinding import com.osfans.trime.ime.core.RimeWrapper import com.osfans.trime.ime.core.Status import com.osfans.trime.ui.setup.SetupActivity -import com.osfans.trime.util.ProgressBarDialogIndeterminate import com.osfans.trime.util.applyTranslucentSystemBars -import com.osfans.trime.util.briefResultLogDialog -import kotlinx.coroutines.Dispatchers +import com.osfans.trime.util.progressBarDialogIndeterminate +import com.osfans.trime.util.rimeActionWithResultDialog import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext class PrefMainActivity : AppCompatActivity() { private val viewModel: MainViewModel by viewModels() @@ -117,7 +115,7 @@ class PrefMainActivity : AppCompatActivity() { Status.IN_PROGRESS -> { loadingDialog?.dismiss() loadingDialog = - ProgressBarDialogIndeterminate(R.string.deploy_progress).create().apply { + progressBarDialogIndeterminate(R.string.deploy_progress).create().apply { show() } } @@ -155,20 +153,8 @@ class PrefMainActivity : AppCompatActivity() { private fun deploy() { lifecycleScope.launch { - withContext(Dispatchers.IO) { - Runtime.getRuntime().exec(arrayOf("logcat", "-c")) - } - val deployResult = - withContext(Dispatchers.Default) { - // All functions that implement the DirectoryChangeListener.Listener - // interface are called here. - // To refresh directory settings. - return@withContext RimeWrapper.deploy() - } - if (deployResult) { - briefResultLogDialog("rime.trime", "W", 1) - } else { - ToastUtils.showLong("Rime is already deploying/starting") + rimeActionWithResultDialog("rime.trime", "W", 1) { + RimeWrapper.deploy() } } } diff --git a/app/src/main/java/com/osfans/trime/util/DialogUtils.kt b/app/src/main/java/com/osfans/trime/util/DialogUtils.kt index 94bdb9ca0d..0bf810eecf 100644 --- a/app/src/main/java/com/osfans/trime/util/DialogUtils.kt +++ b/app/src/main/java/com/osfans/trime/util/DialogUtils.kt @@ -19,8 +19,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext // Adapted from https://github.com/fcitx5-android/fcitx5-android/blob/e37f5513239bab279a9e58cf0c9b163e0dbf5efb/app/src/main/java/org/fcitx/fcitx5/android/ui/common/Preset.kt#L60 -@Suppress("FunctionName") -fun Context.ProgressBarDialogIndeterminate( +fun Context.progressBarDialogIndeterminate( @StringRes titleId: Int, ): AlertDialog.Builder { return AlertDialog.Builder(this, Theme_AppCompat_DayNight_Dialog_Alert) @@ -30,7 +29,7 @@ fun Context.ProgressBarDialogIndeterminate( orientation = LinearLayout.VERTICAL addView( ProgressBar( - this@ProgressBarDialogIndeterminate, + this@progressBarDialogIndeterminate, null, android.R.attr.progressBarStyleHorizontal, ).apply { @@ -60,7 +59,7 @@ fun LifecycleCoroutineScope.withLoadingDialog( @StringRes titleId: Int = R.string.loading, action: suspend () -> Unit, ) { - val loading = context.ProgressBarDialogIndeterminate(titleId).create() + val loading = context.progressBarDialogIndeterminate(titleId).create() val job = launch { delay(thresholds) @@ -83,7 +82,7 @@ suspend fun Context.briefResultLogDialog( val log = withContext(Dispatchers.IO) { Runtime.getRuntime() - .exec(arrayOf("logcat", "-d", "-v", "brief", "-s", "$tag:$priority")) + .exec(arrayOf("logcat", "-d", "-v", "time", "-s", "$tag:$priority")) .inputStream .bufferedReader() .readLines() @@ -107,3 +106,25 @@ suspend fun Context.briefResultLogDialog( ToastUtils.showShort(R.string.setup__done) } } + +suspend fun Context.rimeActionWithResultDialog( + tag: String, + priority: String, + thresholds: Int, + action: suspend () -> Boolean, +) { + withContext(Dispatchers.Main.immediate) { + withContext(Dispatchers.IO) { + Runtime.getRuntime().exec(arrayOf("logcat", "-c")) + } + val result = + withContext(Dispatchers.IO) { + action() + } + if (result) { + briefResultLogDialog(tag, priority, thresholds) + } else { + ToastUtils.showLong("Failed") + } + } +}