Skip to content

Commit fee2036

Browse files
committed
refactor(components): optimize logic in ResetAssetsDialog
- implement popup functions in DialogUtils
1 parent 26849eb commit fee2036

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
package com.osfans.trime.settings.components
22

33
import android.content.Context
4-
import android.os.Build
5-
import android.view.WindowManager
64
import androidx.appcompat.app.AlertDialog
5+
import com.blankj.utilcode.util.ResourceUtils
76
import com.blankj.utilcode.util.ToastUtils
87
import com.osfans.trime.R
9-
import com.osfans.trime.data.Config
10-
import com.osfans.trime.ime.core.Trime
8+
import com.osfans.trime.data.DataManager
9+
import com.osfans.trime.util.appContext
10+
import com.osfans.trime.util.popup
11+
import kotlinx.coroutines.CoroutineScope
12+
import kotlinx.coroutines.Dispatchers
13+
import kotlinx.coroutines.MainScope
14+
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.withContext
1116

1217
/** 顯示輸入法內置數據列表,並回廠選中的數據 */
13-
class ResetAssetsDialog(context: Context) {
14-
private val config = Config.get(context)
15-
/** 內置數據列表 */
16-
private var assetItems: Array<String>? = context.assets.list("rime")
18+
class ResetAssetsDialog(private val context: Context) :
19+
CoroutineScope by MainScope() {
1720

18-
/** 列表勾選狀態 */
19-
private var checkedStatus: BooleanArray = BooleanArray(assetItems!!.size)
21+
/** Internal assets. 内置资源文件列表 **/
22+
private val assets: Array<String>? = appContext.assets.list("rime")
2023

21-
/** 回廠對話框 */
22-
val resetDialog: AlertDialog
24+
/** List to show if items are checked. 检查单 */
25+
private val checkedList: BooleanArray? = assets?.map { false }?.toBooleanArray()
2326

24-
init {
25-
resetDialog = AlertDialog.Builder(context).apply {
26-
setTitle(R.string.conf__reset_title)
27-
setCancelable(true)
28-
setNegativeButton(android.R.string.cancel, null)
29-
setPositiveButton(android.R.string.ok) { _, _ ->
30-
selectAssets()
31-
}
32-
setMultiChoiceItems(
33-
assetItems, checkedStatus
34-
) { _, id, isChecked -> checkedStatus[id] = isChecked }
35-
}.create()
36-
}
37-
38-
private fun selectAssets() {
27+
private suspend fun selectAssets() = withContext(Dispatchers.IO) {
28+
if (assets.isNullOrEmpty() || checkedList == null) {
29+
ToastUtils.showLong(R.string.reset__asset_is_null_or_empty)
30+
return@withContext
31+
}
3932
var res = true
40-
for (i in assetItems?.indices!!) {
41-
res = if (checkedStatus[i]) {
42-
config.copyFileOrDir(assetItems!![i], true)
43-
} else false
33+
for ((i, a) in assets.withIndex()) {
34+
if (checkedList[i]) {
35+
res = res and (
36+
runCatching {
37+
ResourceUtils.copyFileFromAssets(
38+
"rime/$a",
39+
"${DataManager.sharedDataDir.absolutePath}/$a"
40+
)
41+
}.getOrNull() ?: false
42+
)
43+
}
4444
}
4545
ToastUtils.showShort(
4646
if (res) R.string.reset_success else R.string.reset_failure
@@ -49,15 +49,17 @@ class ResetAssetsDialog(context: Context) {
4949

5050
/** 彈出對話框 */
5151
fun show() {
52-
resetDialog.window?.let { window ->
53-
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
54-
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
55-
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
56-
} else {
57-
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
52+
AlertDialog.Builder(context)
53+
.setTitle(R.string.conf__reset_title)
54+
.setNegativeButton(android.R.string.cancel, null)
55+
.setMultiChoiceItems(
56+
assets, checkedList
57+
) { _, id, isChecked -> checkedList?.set(id, isChecked) }
58+
.setPositiveButton(android.R.string.ok) { _, _ ->
59+
launch {
60+
runCatching { selectAssets() }
61+
}
5862
}
59-
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
60-
}
61-
resetDialog.show()
63+
.create().popup()
6264
}
6365
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
package com.osfans.trime.util
22

3+
import android.app.Dialog
34
import android.app.ProgressDialog
45
import android.content.Context
6+
import android.os.Build
7+
import android.view.WindowManager
8+
import com.osfans.trime.ime.core.Trime
59

10+
@Suppress("DEPRECATION")
611
fun createLoadingDialog(context: Context, textId: Int): ProgressDialog {
7-
@Suppress("DEPRECATION")
812
return ProgressDialog(context).apply {
913
setMessage(context.getText(textId))
1014
setCancelable(false)
1115
}
1216
}
17+
/**
18+
* Append layout params to a dialog and show it, which allow the dialog to show
19+
* outside of Activity of this application. This requires overlays permission.
20+
*
21+
* 追加布局参数到对话框并显示它,这可以让它在本应用的 Activity 之外显示。要求有悬浮窗权限。
22+
*/
23+
fun Dialog.popup() {
24+
this.window?.let { window ->
25+
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
26+
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
27+
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
28+
} else {
29+
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
30+
}
31+
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
32+
}
33+
this.show()
34+
}

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,5 @@
243243
<string name="export">Export</string>
244244
<string name="system">System</string>
245245
<string name="settings__system_toolkit">Toolkit</string>
246+
<string name="reset__asset_is_null_or_empty">This path points to a empty directory or null pointer.</string>
246247
</resources>

0 commit comments

Comments
 (0)