|
1 | 1 | package com.osfans.trime.ui.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
|
7 | 5 | import com.osfans.trime.R
|
8 | 6 | import com.osfans.trime.data.AppPrefs
|
9 | 7 | import com.osfans.trime.data.Config
|
10 | 8 | import com.osfans.trime.ime.core.Trime
|
11 | 9 | import com.osfans.trime.util.popup
|
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import kotlinx.coroutines.MainScope |
| 12 | +import kotlinx.coroutines.launch |
12 | 13 | import timber.log.Timber
|
13 | 14 |
|
14 | 15 | /** 顯示配色方案列表
|
15 | 16 | * Show Color Scheme List
|
16 | 17 | * **/
|
17 | 18 | class ColorPickerDialog(
|
18 |
| - context: Context |
19 |
| -) { |
20 |
| - val config: Config = Config.get(context) |
21 |
| - private val prefs get() = AppPrefs.defaultInstance() |
22 |
| - private var colorKeys: Array<String> |
23 |
| - private var checkedColorKey: Int = 0 |
24 |
| - val pickerDialog: AlertDialog |
| 19 | + private val context: Context |
| 20 | +) : CoroutineScope by MainScope() { |
25 | 21 |
|
26 |
| - init { |
27 |
| - val colorScheme = prefs.looks.selectedColor |
28 |
| - colorKeys = config.colorKeys |
29 |
| - colorKeys.sort() |
30 |
| - val colorNames = config.getColorNames(colorKeys) |
31 |
| - checkedColorKey = colorKeys.binarySearch(colorScheme) |
| 22 | + private val prefs get() = AppPrefs.defaultInstance() |
| 23 | + private lateinit var allColorKeys: Array<String> |
| 24 | + private lateinit var allColorNames: Array<String> |
| 25 | + private var checkedItem: Int = -1 |
32 | 26 |
|
33 |
| - pickerDialog = AlertDialog.Builder(context, R.style.Theme_AppCompat_DayNight_Dialog_Alert).apply { |
34 |
| - setTitle(R.string.looks__selected_color_title) |
35 |
| - setCancelable(true) |
36 |
| - setNegativeButton(android.R.string.cancel, null) |
37 |
| - setPositiveButton(android.R.string.ok) { _, _ -> |
38 |
| - selectColor() |
| 27 | + private fun buildAndShowDialog() { |
| 28 | + AlertDialog.Builder(context, R.style.Theme_AppCompat_DayNight_Dialog_Alert) |
| 29 | + .setTitle(R.string.looks__selected_color_title) |
| 30 | + .setNegativeButton(android.R.string.cancel, null) |
| 31 | + .setSingleChoiceItems( |
| 32 | + allColorNames, checkedItem |
| 33 | + ) { _, id -> checkedItem = id } |
| 34 | + .setPositiveButton(android.R.string.ok) { _, _ -> |
| 35 | + if (checkedItem in allColorKeys.indices) { |
| 36 | + launch { |
| 37 | + Timber.i("Applying color scheme ...") |
| 38 | + prefs.looks.selectedColor = allColorKeys[checkedItem] |
| 39 | + Timber.i("Initializing keyboard ...") |
| 40 | + Trime.getServiceOrNull()?.initKeyboard() // 立刻重初始化键盘生效 |
| 41 | + Timber.i("Applying done") |
| 42 | + } |
| 43 | + } |
39 | 44 | }
|
40 |
| - setSingleChoiceItems( |
41 |
| - colorNames, checkedColorKey |
42 |
| - ) { _, id -> checkedColorKey = id } |
43 |
| - }.create() |
| 45 | + .create().popup() |
44 | 46 | }
|
45 | 47 |
|
46 |
| - private fun selectColor() { |
47 |
| - Timber.i("select") |
48 |
| - if (checkedColorKey !in colorKeys.indices) return |
49 |
| - val colorKey = colorKeys[checkedColorKey] |
50 |
| - prefs.looks.selectedColor = colorKey |
51 |
| - Timber.i("initKeyboard") |
52 |
| - Trime.getServiceOrNull()?.initKeyboard() // 立刻重初始化键盘生效 |
53 |
| - Timber.i("done") |
| 48 | + private fun init() { |
| 49 | + val allColors = Config.get(context).presetColorSchemes.toTypedArray() |
| 50 | + allColorKeys = allColors.map { a -> a[0] }.toTypedArray() |
| 51 | + allColorNames = allColors.map { a -> a[1] }.toTypedArray() |
| 52 | + val activeColor = prefs.looks.selectedColor |
| 53 | + Timber.d("activeColor = $activeColor") |
| 54 | + checkedItem = allColorKeys.indexOf(activeColor) |
54 | 55 | }
|
55 | 56 |
|
56 | 57 | /** 调用该方法显示对话框 **/
|
57 |
| - fun show() { |
58 |
| - pickerDialog.popup() |
| 58 | + fun show() = launch { |
| 59 | + init() |
| 60 | + buildAndShowDialog() |
59 | 61 | }
|
60 | 62 | }
|
0 commit comments