Skip to content

Commit

Permalink
refactor: standardize deploy process with a result dialog
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goofyz committed Dec 30, 2023
1 parent ab36ded commit dd52f31
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,7 +73,7 @@ class ProfileFragment :
}
get<Preference>("profile_sync_user_data")?.setOnPreferenceClickListener {
lifecycleScope.launch {
withContext(Dispatchers.IO) {
this@ProfileFragment.context?.rimeActionWithResultDialog("rime.trime", "W", 1) {
Rime.syncRimeUserData()
RimeWrapper.deploy()
}
Expand Down
24 changes: 5 additions & 19 deletions app/src/main/java/com/osfans/trime/ui/main/PrefMainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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()
}
}
}
Expand Down
31 changes: 26 additions & 5 deletions app/src/main/java/com/osfans/trime/util/DialogUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -30,7 +29,7 @@ fun Context.ProgressBarDialogIndeterminate(
orientation = LinearLayout.VERTICAL
addView(
ProgressBar(
this@ProgressBarDialogIndeterminate,
this@progressBarDialogIndeterminate,
null,
android.R.attr.progressBarStyleHorizontal,
).apply {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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")
}
}
}

0 comments on commit dd52f31

Please sign in to comment.