1
1
package com.osfans.trime.settings.components
2
2
3
3
import android.content.Context
4
- import android.os.Build
5
- import android.view.WindowManager
6
4
import androidx.appcompat.app.AlertDialog
5
+ import com.blankj.utilcode.util.ResourceUtils
7
6
import com.blankj.utilcode.util.ToastUtils
8
7
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
11
16
12
17
/* * 顯示輸入法內置數據列表,並回廠選中的數據 */
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 () {
17
20
18
- /* * 列表勾選狀態 */
19
- private var checkedStatus : BooleanArray = BooleanArray (assetItems !! .size )
21
+ /* * Internal assets. 内置资源文件列表 * */
22
+ private val assets : Array < String > ? = appContext.assets.list( " rime " )
20
23
21
- /* * 回廠對話框 */
22
- val resetDialog : AlertDialog
24
+ /* * List to show if items are checked. 检查单 */
25
+ private val checkedList : BooleanArray? = assets?.map { false }?.toBooleanArray()
23
26
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
+ }
39
32
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
+ }
44
44
}
45
45
ToastUtils .showShort(
46
46
if (res) R .string.reset_success else R .string.reset_failure
@@ -49,15 +49,17 @@ class ResetAssetsDialog(context: Context) {
49
49
50
50
/* * 彈出對話框 */
51
51
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
+ }
58
62
}
59
- window.addFlags(WindowManager .LayoutParams .FLAG_ALT_FOCUSABLE_IM )
60
- }
61
- resetDialog.show()
63
+ .create().popup()
62
64
}
63
65
}
0 commit comments