Skip to content

Commit

Permalink
Replace MutableState with MutableStateFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
bingzheung committed Nov 4, 2024
1 parent ebb59d8 commit a3f2ee3
Show file tree
Hide file tree
Showing 47 changed files with 546 additions and 465 deletions.
19 changes: 13 additions & 6 deletions app/src/main/java/org/jyutping/jyutping/ComposeKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.os.Build
import androidx.annotation.DeprecatedSinceApi
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.AbstractComposeView
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalView
Expand All @@ -31,17 +33,22 @@ class ComposeKeyboardView(context: Context) : AbstractComposeView(context) {
@Composable
override fun Content() {
val ctx = context as JyutpingInputMethodService
LocalView.current.isSoundEffectsEnabled = ctx.isAudioFeedbackOn.value
LocalView.current.isHapticFeedbackEnabled = ctx.isHapticFeedbackOn.value
when (ctx.keyboardForm.value) {
KeyboardForm.Alphabetic -> when (ctx.qwertyForm.value) {
val isAudioFeedbackOn by ctx.isAudioFeedbackOn.collectAsState()
val isHapticFeedbackOn by ctx.isHapticFeedbackOn.collectAsState()
LocalView.current.isSoundEffectsEnabled = isAudioFeedbackOn
LocalView.current.isHapticFeedbackEnabled = isHapticFeedbackOn
val keyboardForm by ctx.keyboardForm.collectAsState()
val qwertyForm by ctx.qwertyForm.collectAsState()
val inputMethodMode by ctx.inputMethodMode.collectAsState()
when (keyboardForm) {
KeyboardForm.Alphabetic -> when (qwertyForm) {
QwertyForm.Cangjie -> CangjieKeyboard(keyHeight = responsiveKeyHeight())
QwertyForm.Stroke -> StrokeKeyboard(keyHeight = responsiveKeyHeight())
else -> AlphabeticKeyboard(keyHeight = responsiveKeyHeight())
}
KeyboardForm.CandidateBoard -> CandidateBoard(height = keyboardHeight())
KeyboardForm.Numeric -> if (ctx.inputMethodMode.value.isABC()) NumericKeyboard(keyHeight = responsiveKeyHeight()) else CantoneseNumericKeyboard(keyHeight = responsiveKeyHeight())
KeyboardForm.Symbolic -> if (ctx.inputMethodMode.value.isABC()) SymbolicKeyboard(keyHeight = responsiveKeyHeight()) else CantoneseSymbolicKeyboard(keyHeight = responsiveKeyHeight())
KeyboardForm.Numeric -> if (inputMethodMode.isABC()) NumericKeyboard(keyHeight = responsiveKeyHeight()) else CantoneseNumericKeyboard(keyHeight = responsiveKeyHeight())
KeyboardForm.Symbolic -> if (inputMethodMode.isABC()) SymbolicKeyboard(keyHeight = responsiveKeyHeight()) else CantoneseSymbolicKeyboard(keyHeight = responsiveKeyHeight())
KeyboardForm.Settings -> SettingsScreen(height = keyboardHeight())
KeyboardForm.EmojiBoard -> EmojiBoard(height = keyboardHeight())
KeyboardForm.EditingPanel -> EditingPanel(height = keyboardHeight())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.ExtractedTextRequest
import android.view.inputmethod.InputMethodManager
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelStore
Expand Down Expand Up @@ -83,7 +79,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
super.onStartInput(attribute, restarting)
inputClientMonitorJob?.cancel()
val isNightMode = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
val isNightMode: Boolean = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
window?.window?.navigationBarColor = if (isNightMode) PresetColor.keyboardDarkBackground.toArgb() else PresetColor.keyboardLightBackground.toArgb()
isDarkMode.value = isNightMode
inputMethodMode.value = InputMethodMode.Cantonese
Expand Down Expand Up @@ -137,12 +133,12 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),

private val sharedPreferences by lazy { getSharedPreferences(UserSettingsKey.PreferencesFileName, Context.MODE_PRIVATE) }

val isDarkMode: MutableState<Boolean> by lazy {
val isDarkMode: MutableStateFlow<Boolean> by lazy {
val isNightMode: Boolean = (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
mutableStateOf(isNightMode)
MutableStateFlow(isNightMode)
}

val spaceKeyForm: MutableState<SpaceKeyForm> by lazy { mutableStateOf(SpaceKeyForm.Fallback) }
val spaceKeyForm: MutableStateFlow<SpaceKeyForm> by lazy { MutableStateFlow(SpaceKeyForm.Fallback) }
private fun updateSpaceKeyForm() {
val newForm: SpaceKeyForm = when {
inputMethodMode.value.isABC() -> SpaceKeyForm.English
Expand All @@ -169,7 +165,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
}

val returnKeyForm: MutableState<ReturnKeyForm> by lazy { mutableStateOf(ReturnKeyForm.StandbyTraditional) }
val returnKeyForm: MutableStateFlow<ReturnKeyForm> by lazy { MutableStateFlow(ReturnKeyForm.StandbyTraditional) }
private fun updateReturnKeyForm() {
val newForm: ReturnKeyForm = when (inputMethodMode.value) {
InputMethodMode.ABC -> ReturnKeyForm.StandbyABC
Expand All @@ -186,7 +182,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
}

val inputMethodMode: MutableState<InputMethodMode> by lazy { mutableStateOf(InputMethodMode.Cantonese) }
val inputMethodMode: MutableStateFlow<InputMethodMode> by lazy { MutableStateFlow(InputMethodMode.Cantonese) }
fun toggleInputMethodMode() {
val newMode: InputMethodMode = when (inputMethodMode.value) {
InputMethodMode.Cantonese -> InputMethodMode.ABC
Expand All @@ -197,14 +193,14 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
updateReturnKeyForm()
}

val qwertyForm: MutableState<QwertyForm> by lazy { mutableStateOf(QwertyForm.Jyutping) }
val qwertyForm: MutableStateFlow<QwertyForm> by lazy { MutableStateFlow(QwertyForm.Jyutping) }
private fun updateQwertyForm(form: QwertyForm) {
if (qwertyForm.value != form) {
qwertyForm.value = form
}
}

val keyboardForm: MutableState<KeyboardForm> by lazy { mutableStateOf(KeyboardForm.Alphabetic) }
val keyboardForm: MutableStateFlow<KeyboardForm> by lazy { MutableStateFlow(KeyboardForm.Alphabetic) }
fun transformTo(destination: KeyboardForm) {
if (isBuffering.value) {
val shouldKeepBuffer: Boolean = (destination == KeyboardForm.Alphabetic) || (destination == KeyboardForm.CandidateBoard)
Expand All @@ -221,7 +217,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
updateSpaceKeyForm()
}

val keyboardCase: MutableState<KeyboardCase> by lazy { mutableStateOf(KeyboardCase.Lowercased) }
val keyboardCase: MutableStateFlow<KeyboardCase> by lazy { MutableStateFlow(KeyboardCase.Lowercased) }
private fun updateKeyboardCase(case: KeyboardCase) {
keyboardCase.value = case
updateSpaceKeyForm()
Expand All @@ -248,10 +244,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
}

val characterStandard: MutableState<CharacterStandard> by lazy {
val characterStandard: MutableStateFlow<CharacterStandard> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.CharacterStandard, CharacterStandard.Traditional.identifier)
val standard: CharacterStandard = CharacterStandard.standardOf(savedValue)
mutableStateOf(standard)
MutableStateFlow(standard)
}
fun updateCharacterStandard(standard: CharacterStandard) {
characterStandard.value = standard
Expand All @@ -261,10 +257,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.CharacterStandard, standard.identifier)
editor.apply()
}
val isAudioFeedbackOn: MutableState<Boolean> by lazy {
val isAudioFeedbackOn: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.AudioFeedback, 101)
val isOn: Boolean = (savedValue == 101)
mutableStateOf(isOn)
MutableStateFlow(isOn)
}
fun updateAudioFeedback(isOn: Boolean) {
isAudioFeedbackOn.value = isOn
Expand All @@ -273,10 +269,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.AudioFeedback, value)
editor.apply()
}
val isHapticFeedbackOn: MutableState<Boolean> by lazy {
val isHapticFeedbackOn: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.HapticFeedback, 101)
val isOn: Boolean = (savedValue == 101)
mutableStateOf(isOn)
MutableStateFlow(isOn)
}
fun updateHapticFeedback(isOn: Boolean) {
isHapticFeedbackOn.value = isOn
Expand All @@ -285,10 +281,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.HapticFeedback, value)
editor.apply()
}
val showLowercaseKeys: MutableState<Boolean> by lazy {
val showLowercaseKeys: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.KeyCase, 1)
val isLowercase: Boolean = (savedValue == 1)
mutableStateOf(isLowercase)
MutableStateFlow(isLowercase)
}
fun updateShowLowercaseKeys(isOn: Boolean) {
showLowercaseKeys.value = isOn
Expand All @@ -297,10 +293,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.KeyCase, value2save)
editor.apply()
}
val previewKeyText: MutableState<Boolean> by lazy {
val previewKeyText: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.KeyTextPreview, 1)
val shouldPreview: Boolean = (savedValue == 1)
mutableStateOf(shouldPreview)
MutableStateFlow(shouldPreview)
}
fun updatePreviewKeyText(isOn: Boolean) {
previewKeyText.value = isOn
Expand All @@ -321,10 +317,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.HighContrast, value2save)
editor.apply()
}
val needsInputModeSwitchKey: MutableState<Boolean> by lazy {
val needsInputModeSwitchKey: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.GlobeKey, 0)
val needs: Boolean = (savedValue == 1)
mutableStateOf(needs)
MutableStateFlow(needs)
}
fun updateNeedsInputModeSwitchKey(needs: Boolean) {
needsInputModeSwitchKey.value = needs
Expand All @@ -333,10 +329,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.GlobeKey, value2save)
editor.apply()
}
val needsLeftKey: MutableState<Boolean> by lazy {
val needsLeftKey: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.LeftKey, 1)
val needs: Boolean = (savedValue == 1)
mutableStateOf(needs)
MutableStateFlow(needs)
}
fun updateNeedsLeftKey(needs: Boolean) {
needsLeftKey.value = needs
Expand All @@ -345,10 +341,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.LeftKey, value2save)
editor.apply()
}
val needsRightKey: MutableState<Boolean> by lazy {
val needsRightKey: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.RightKey, 1)
val needs: Boolean = (savedValue == 1)
mutableStateOf(needs)
MutableStateFlow(needs)
}
fun updateNeedsRightKey(needs: Boolean) {
needsRightKey.value = needs
Expand All @@ -357,10 +353,10 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.RightKey, value2save)
editor.apply()
}
val isEmojiSuggestionsOn: MutableState<Boolean> by lazy {
val isEmojiSuggestionsOn: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.Emoji, 1)
val isOn: Boolean = (savedValue == 1)
mutableStateOf(isOn)
MutableStateFlow(isOn)
}
fun updateEmojiSuggestionsState(isOn: Boolean) {
isEmojiSuggestionsOn.value = isOn
Expand All @@ -369,32 +365,32 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
editor.putInt(UserSettingsKey.Emoji, value2save)
editor.apply()
}
val commentStyle: MutableState<CommentStyle> by lazy {
val commentStyle: MutableStateFlow<CommentStyle> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.CommentStyle, CommentStyle.AboveCandidates.identifier)
val style: CommentStyle = CommentStyle.styleOf(savedValue)
mutableStateOf(style)
MutableStateFlow(style)
}
fun updateCommentStyle(style: CommentStyle) {
commentStyle.value = style
val editor = sharedPreferences.edit()
editor.putInt(UserSettingsKey.CommentStyle, style.identifier)
editor.apply()
}
val cangjieVariant: MutableState<CangjieVariant> by lazy {
val cangjieVariant: MutableStateFlow<CangjieVariant> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.CangjieVariant, CangjieVariant.Cangjie5.identifier)
val variant: CangjieVariant = CangjieVariant.variantOf(savedValue)
mutableStateOf(variant)
MutableStateFlow(variant)
}
fun updateCangjieVariant(variant: CangjieVariant) {
cangjieVariant.value = variant
val editor = sharedPreferences.edit()
editor.putInt(UserSettingsKey.CangjieVariant, variant.identifier)
editor.apply()
}
val isInputMemoryOn: MutableState<Boolean> by lazy {
val isInputMemoryOn: MutableStateFlow<Boolean> by lazy {
val savedValue: Int = sharedPreferences.getInt(UserSettingsKey.InputMemory, 1)
val isOn: Boolean = (savedValue == 1)
mutableStateOf(isOn)
MutableStateFlow(isOn)
}
fun updateInputMemoryState(isOn: Boolean) {
isInputMemoryOn.value = isOn
Expand All @@ -410,8 +406,8 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
userDB.deleteAll()
}

val candidateState: MutableIntState by lazy { mutableIntStateOf(1) }
val candidates: MutableState<List<Candidate>> by lazy { mutableStateOf(listOf()) }
val candidateState: MutableStateFlow<Int> by lazy { MutableStateFlow(1) }
val candidates: MutableStateFlow<List<Candidate>> by lazy { MutableStateFlow(listOf()) }
private val db by lazy { DatabaseHelper(this, DatabasePreparer.databaseName) }
private var bufferText: String = PresetString.EMPTY
set(value) {
Expand Down Expand Up @@ -556,11 +552,11 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}
}
}
candidateState.intValue += 1
candidateState.value += 1
updateSpaceKeyForm()
updateReturnKeyForm()
}
val isBuffering: MutableState<Boolean> by lazy { mutableStateOf(false) }
val isBuffering: MutableStateFlow<Boolean> by lazy { MutableStateFlow(false) }
fun clearBuffer() {
bufferText = PresetString.EMPTY
}
Expand Down Expand Up @@ -709,7 +705,7 @@ class JyutpingInputMethodService: LifecycleInputMethodService(),
}

// region EditingPanel
val isClipboardEmpty: MutableState<Boolean> by lazy { mutableStateOf(true) }
val isClipboardEmpty: MutableStateFlow<Boolean> by lazy { MutableStateFlow(true) }
private fun isCurrentClipboardEmpty(): Boolean {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
if (clipboard.hasPrimaryClip().not()) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
Expand All @@ -24,7 +25,7 @@ import kotlin.math.min
@Composable
fun EditingPanel(height: Dp) {
val context = LocalContext.current as JyutpingInputMethodService
val isDarkMode = remember { context.isDarkMode }
val isDarkMode by context.isDarkMode.collectAsState()
val screenWidth: Float = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val windowMetrics = context.windowManager.currentWindowMetrics
(windowMetrics.bounds.width() / windowMetrics.density)
Expand All @@ -39,7 +40,7 @@ fun EditingPanel(height: Dp) {
val keyHeight: Dp = height / 4.0f
Row(
modifier = Modifier
.background(if (isDarkMode.value) PresetColor.keyboardDarkBackground else PresetColor.keyboardLightBackground)
.background(if (isDarkMode) PresetColor.keyboardDarkBackground else PresetColor.keyboardLightBackground)
.systemBarsPadding()
.height(height)
.fillMaxWidth()
Expand Down
Loading

0 comments on commit a3f2ee3

Please sign in to comment.