Skip to content

Commit

Permalink
refactor(components): migrate AlertDialog to AndroidX
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck authored and Bambooin committed Oct 23, 2021
1 parent da18332 commit 6305d67
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.osfans.trime.settings.components

import android.app.AlertDialog
import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.os.Build
import android.view.WindowManager
import com.osfans.trime.R
import com.osfans.trime.ime.core.Preferences
import com.osfans.trime.ime.core.Trime
Expand All @@ -17,39 +19,50 @@ class ColorPickerDialog(
val config: Config = Config.get(context)
private val prefs get() = Preferences.defaultInstance()
private var colorKeys: Array<String>
private var checkedColor: Int = 0
private var checkedColorKey: Int = 0
val pickerDialog: AlertDialog

init {
val colorScheme = prefs.looks.selectedColor
colorKeys = config.colorKeys
colorKeys.sort()
val colorNames = config.getColorNames(colorKeys)
checkedColor = colorKeys.binarySearch(colorScheme)
checkedColorKey = colorKeys.binarySearch(colorScheme)

pickerDialog = AlertDialog.Builder(context).apply {
pickerDialog = AlertDialog.Builder(context, R.style.AlertDialogTheme).apply {
setTitle(R.string.looks__selected_color_title)
setCancelable(true)
setNegativeButton(android.R.string.cancel, null)
setPositiveButton(android.R.string.ok) { _, _ ->
selectColor()
}
setSingleChoiceItems(
colorNames, checkedColor
) { _, id -> checkedColor = id }
colorNames, checkedColorKey
) { _, id -> checkedColorKey = id }
}.create()
}

private fun selectColor() {
Timber.i("select")
if (checkedColor < 0 || checkedColor >= colorKeys.size) return
val colorKey = colorKeys[checkedColor]
if (checkedColorKey !in colorKeys.indices) return
val colorKey = colorKeys[checkedColorKey]
prefs.looks.selectedColor = colorKey
Timber.i("initKeyboard")
Trime.getService()?.initKeyboard() // 立刻重初始化键盘生效
Trime.getService().initKeyboard() // 立刻重初始化键盘生效
Timber.i("done")
}

/** 调用该方法显示对话框 **/
fun show() = pickerDialog.show()
fun show() {
pickerDialog.window?.let { window ->
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
}
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}
pickerDialog.show()
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.osfans.trime.settings.components

import android.app.AlertDialog
import androidx.appcompat.app.AlertDialog
import android.content.Context
import android.os.Build
import android.view.WindowManager
import com.blankj.utilcode.util.ToastUtils
import com.osfans.trime.R
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.setup.Config

/** 顯示輸入法內置數據列表,並回廠選中的數據 */
Expand Down Expand Up @@ -45,5 +48,16 @@ class ResetAssetsDialog(context: Context) {
}

/** 彈出對話框 */
fun show() = resetDialog.show()
fun show() {
resetDialog.window?.let { window ->
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
}
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}
resetDialog.show()
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package com.osfans.trime.settings.components

import android.app.AlertDialog
import androidx.appcompat.app.AlertDialog
import android.app.Dialog
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.view.WindowManager
import com.osfans.trime.R
import com.osfans.trime.Rime
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.settings.PrefMainActivity
import com.osfans.trime.setup.Config
import com.osfans.trime.util.RimeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.delay
import timber.log.Timber
import kotlin.coroutines.CoroutineContext
import kotlin.system.exitProcess

class SchemaPickerDialog(
private val context: Context,
private val token: IBinder?
private val context: Context
) : CoroutineScope {

private val job = Job()
Expand Down Expand Up @@ -52,8 +52,6 @@ class SchemaPickerDialog(
}
}

constructor(context: Context) : this(context, null)

init {
@Suppress("DEPRECATION")
progressDialog = ProgressDialog(context).apply {
Expand All @@ -63,7 +61,7 @@ class SchemaPickerDialog(
}

private fun showPickerDialog() {
pickerDialogBuilder = AlertDialog.Builder(context).apply {
pickerDialogBuilder = AlertDialog.Builder(context, R.style.AlertDialogTheme).apply {
setTitle(R.string.pref_schemas)
setCancelable(true)
setPositiveButton(android.R.string.ok, null)
Expand All @@ -75,7 +73,7 @@ class SchemaPickerDialog(
setNegativeButton(android.R.string.cancel, null)
setPositiveButton(android.R.string.ok) { _, _ ->
@Suppress("DEPRECATION")
progressDialog = ProgressDialog(context).apply {
progressDialog = ProgressDialog(context).also {
setMessage(context.getString(R.string.deploy_progress))
}.also {
appendDialogParams(it)
Expand Down Expand Up @@ -105,24 +103,19 @@ class SchemaPickerDialog(
}
}
val pickerDialog = pickerDialogBuilder!!.create()
if (token != null) appendDialogParams(pickerDialog)
appendDialogParams(pickerDialog)
pickerDialog.show()
}

private fun appendDialogParams(dialog: AlertDialog) {
val window = dialog.window
val lp = window?.attributes
lp?.let {
it.token = token
it.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
private fun appendDialogParams(dialog: Dialog) {
dialog.window?.let { window ->
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
}
}
window?.let {
it.attributes = lp
it.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}
}

Expand Down Expand Up @@ -179,7 +172,7 @@ class SchemaPickerDialog(
}

private fun onPreExecute() {
if (token != null) appendDialogParams(progressDialog)
appendDialogParams(progressDialog)
progressDialog.show()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
package com.osfans.trime.settings.components

import android.app.AlertDialog
import androidx.appcompat.app.AlertDialog
import android.app.Dialog
import android.app.ProgressDialog
import android.content.Context
import android.os.Build
import android.os.IBinder
import android.view.WindowManager
import com.osfans.trime.R
import com.osfans.trime.ime.core.Trime
import com.osfans.trime.setup.Config
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/** 顯示配色方案列表 */
class ThemePickerDialog(
private val context: Context,
private val token: IBinder?
) : CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() = Dispatchers.Main + job
private val context: Context
) : CoroutineScope by MainScope() {

private val config = Config.get(context)
private val themeKeys: Array<String?>
Expand All @@ -33,7 +30,6 @@ class ThemePickerDialog(
@Suppress("DEPRECATION")
private val progressDialog: ProgressDialog

constructor(context: Context) : this(context, null)
init {
val themeFile = config.theme + ".yaml"
themeKeys = Config.getThemeKeys(true)
Expand All @@ -56,7 +52,7 @@ class ThemePickerDialog(
}
}
// Init picker
pickerDialog = AlertDialog.Builder(context).apply {
pickerDialog = AlertDialog.Builder(context, R.style.AlertDialogTheme).apply {
setTitle(R.string.looks__selected_theme_title)
setCancelable(true)
setNegativeButton(android.R.string.cancel, null)
Expand All @@ -73,28 +69,23 @@ class ThemePickerDialog(
}
}

private fun appendDialogParams(dialog: AlertDialog) {
val window = dialog.window
val lp = window?.attributes
lp?.let {
it.token = token
it.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
private fun appendDialogParams(dialog: Dialog) {
dialog.window?.let { window ->
window.attributes.token = Trime.getServiceOrNull()?.window?.window?.decorView?.windowToken
window.attributes.type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG
}
}
window?.let {
it.attributes = lp
it.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}
}

private fun setTheme() { config.theme = themeKeys[checkedId]?.replace(".yaml", "") }

/** 调用该方法显示对话框 **/
fun show() {
if (token != null) appendDialogParams(pickerDialog)
appendDialogParams(pickerDialog)
pickerDialog.show()
}

Expand All @@ -105,12 +96,13 @@ class ThemePickerDialog(
}

private fun onPreExecute() {
if (token != null) appendDialogParams(progressDialog)
appendDialogParams(progressDialog)
progressDialog.show()
}

private suspend fun doInBackground(): String = withContext(Dispatchers.IO) {
setTheme()
delay(500) // Simulate async task
return@withContext "OK"
}

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@
<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:colorBackground">@color/windowBackground</item>
</style>

<style name="AlertDialogTheme" parent="Theme.AppCompat.DayNight.Dialog.Alert">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorAccent" tools:targetApi="lollipop">@color/colorAccent</item>

<item name="android:windowBackground">@color/windowBackground</item>
<item name="android:colorBackground">@color/windowBackground</item>

</style>
</resources>

0 comments on commit 6305d67

Please sign in to comment.