Skip to content

Commit

Permalink
refactor: use native apis to get stuffs from system's resource instance
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jun 14, 2024
1 parent 99dc458 commit 95272b0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/com/osfans/trime/data/AppPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.osfans.trime.R
import com.osfans.trime.data.base.DataManager
import com.osfans.trime.ime.enums.FullscreenMode
import com.osfans.trime.ime.enums.InlinePreeditMode
import com.osfans.trime.ime.keyboard.KeyboardPrefs
import com.osfans.trime.util.appContext
import java.lang.ref.WeakReference
import java.util.Calendar
Expand Down Expand Up @@ -201,8 +200,16 @@ class AppPrefs(
var switchArrowEnabled: Boolean = false
get() = prefs.getPref(SWITCH_ARROW_ENABLED, true)
private set
val splitOption: String
get() = prefs.getPref(SPLIT, KeyboardPrefs.SPLIT_OPTION_NEVER)

enum class SplitOption {
AUTO,
LANDSCAPE,
NEVER,
ALWAYS,
}

val splitOption: SplitOption
get() = SplitOption.valueOf(prefs.getPref(SPLIT, SplitOption.NEVER.name))
val splitSpacePercent: Int
get() = prefs.getPref(SPLIT_SPACE_PERCENT, 100)
var candidatePageSize: Int = 0
Expand Down
16 changes: 9 additions & 7 deletions app/src/main/java/com/osfans/trime/ime/keyboard/Keyboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ package com.osfans.trime.ime.keyboard

import android.content.res.Configuration
import android.view.KeyEvent
import com.blankj.utilcode.util.ScreenUtils
import com.osfans.trime.data.AppPrefs.Companion.defaultInstance
import com.osfans.trime.data.theme.EventManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.keyboard.KeyboardPrefs.isLandscapeMode
import com.osfans.trime.util.CollectionUtils.obtainBoolean
import com.osfans.trime.util.CollectionUtils.obtainFloat
import com.osfans.trime.util.CollectionUtils.obtainInt
import com.osfans.trime.util.CollectionUtils.obtainString
import com.osfans.trime.util.appContext
import com.osfans.trime.util.config.ConfigMap
import com.osfans.trime.util.isLandscape
import com.osfans.trime.util.sp
import splitties.dimensions.dp
import timber.log.Timber
Expand Down Expand Up @@ -222,7 +223,7 @@ class Keyboard() {
keyboardHeight = getKeyboardHeight(theme, keyboardConfig)
val keyboardKeyWidth = obtainFloat(keyboardConfig, "width", 0f)
val maxColumns = if (columns == -1) Int.MAX_VALUE else columns
val isSplit = KeyboardPrefs().isLandscapeMode() && isLandscapeSplit
val isSplit = appContext.isLandscapeMode() && isLandscapeSplit
val (rowWidthTotalWeight, oneWeightWidthPx, multiplier, scaledHeight, scaledVerticalGap) =
KeyboardSizeCalculator(
name,
Expand Down Expand Up @@ -434,7 +435,7 @@ class Keyboard() {

private fun getKeyboardHeightFromKeyboardConfig(keyboardConfig: Map<String, Any?>?): Int {
var mkeyboardHeight = appContext.sp(obtainFloat(keyboardConfig, "keyboard_height", 0f)).toInt()
if (ScreenUtils.isLandscape()) {
if (appContext.resources.configuration.isLandscape()) {
val mkeyBoardHeightLand =
appContext.sp(
obtainFloat(keyboardConfig, "keyboard_height_land", 0f),
Expand Down Expand Up @@ -642,13 +643,14 @@ class Keyboard() {

val keyboardSidePaddingPx =
appContext.dp(
when (appContext.resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> keyboardSidePaddingLandscape
else -> keyboardSidePadding
if (appContext.resources.configuration.isLandscape()) {
keyboardSidePaddingLandscape
} else {
keyboardSidePadding
},
)

mDisplayWidth = ScreenUtils.getAppScreenWidth() - 2 * keyboardSidePaddingPx
mDisplayWidth = appContext.resources.displayMetrics.widthPixels - 2 * keyboardSidePaddingPx

// Height of the screen
// final int mDisplayHeight = dm.heightPixels;
Expand Down
29 changes: 12 additions & 17 deletions app/src/main/java/com/osfans/trime/ime/keyboard/KeyboardPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,26 @@

package com.osfans.trime.ime.keyboard

import com.blankj.utilcode.util.ScreenUtils
import android.content.Context
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.util.isLandscape

class KeyboardPrefs {
object KeyboardPrefs {
private val prefs = AppPrefs.defaultInstance()

fun isLandscapeMode(): Boolean {
private const val WIDE_SCREEN_WIDTH_DP = 600

fun Context.isLandscapeMode(): Boolean {
return when (prefs.keyboard.splitOption) {
SPLIT_OPTION_AUTO -> isWideScreen()
SPLIT_OPTION_LANDSCAPE -> ScreenUtils.isLandscape()
SPLIT_OPTION_ALWAYS -> true
AppPrefs.Keyboard.SplitOption.AUTO -> isWideScreen()
AppPrefs.Keyboard.SplitOption.LANDSCAPE -> resources.configuration.isLandscape()
AppPrefs.Keyboard.SplitOption.ALWAYS -> true
else -> false
}
}

private fun isWideScreen(): Boolean {
return ScreenUtils.getAppScreenWidth() / ScreenUtils.getScreenDensity() > WIDE_SCREEN_WIDTH_DP
}

companion object {
const val SPLIT_OPTION_AUTO = "auto"
const val SPLIT_OPTION_LANDSCAPE = "landscape"
const val SPLIT_OPTION_NEVER = "never"
const val SPLIT_OPTION_ALWAYS = "always"

private const val WIDE_SCREEN_WIDTH_DP = 600
private fun Context.isWideScreen(): Boolean {
val metrics = resources.displayMetrics
return metrics.widthPixels / metrics.density > WIDE_SCREEN_WIDTH_DP
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.osfans.trime.core.Rime
import com.osfans.trime.data.AppPrefs
import com.osfans.trime.data.schema.SchemaManager
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.keyboard.KeyboardPrefs.isLandscapeMode
import com.osfans.trime.util.appContext
import timber.log.Timber

Expand All @@ -20,7 +21,6 @@ object KeyboardSwitcher {
private var currentKeyboardId: String? = null
private var lastKeyboardId: String? = null
private var lastLockKeyboardId: String? = null
private val keyboardPrefs = KeyboardPrefs()
lateinit var currentKeyboard: Keyboard

private fun getKeyboard(name: String): Keyboard {
Expand Down Expand Up @@ -87,7 +87,7 @@ object KeyboardSwitcher {
if ("mini" in allKeyboardIds) mappedName = "mini"
}
// 切换到横屏布局
if (keyboardPrefs.isLandscapeMode()) {
if (appContext.isLandscapeMode()) {
val landscapeKeyboard = getKeyboard(mappedName).landscapeKeyboard
if (landscapeKeyboard != null && landscapeKeyboard in allKeyboardIds) {
mappedName = landscapeKeyboard
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/osfans/trime/util/Configuration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.osfans.trime.util

import android.content.res.Configuration

fun Configuration.isNightMode() = uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

fun Configuration.isLandscape() = orientation == Configuration.ORIENTATION_LANDSCAPE
3 changes: 0 additions & 3 deletions app/src/main/java/com/osfans/trime/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.osfans.trime.util

import android.content.Context
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -142,5 +141,3 @@ fun Context.requestExternalStoragePermission() {
},
)
}

fun Configuration.isNightMode() = uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

0 comments on commit 95272b0

Please sign in to comment.