Skip to content

Commit

Permalink
refactor: deprecate IMS instance getter
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Dec 29, 2024
1 parent 2c8e6fe commit 0be5946
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 56 deletions.
10 changes: 4 additions & 6 deletions app/src/main/java/com/osfans/trime/data/db/DraftHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package com.osfans.trime.data.db

import android.content.Context
import android.view.inputmethod.InputConnection
import androidx.room.Room
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.util.matchesAny
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -85,13 +85,11 @@ object DraftHelper : CoroutineScope by CoroutineScope(SupervisorJob() + Dispatch
updateItemCount()
}

fun onInputEventChanged() {
fun onExtractedTextChanged(inputConnection: InputConnection) {
if (!(limit != 0 && this::dftDao.isInitialized)) return

TrimeInputMethodService
.getServiceOrNull()
?.currentInputConnection
?.let { DatabaseBean.fromInputConnection(it) }
inputConnection
.let { DatabaseBean.fromInputConnection(it) }
?.takeIf {
it.text!!.isNotBlank() &&
!it.text.matchesAny(output)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
super.onCreate()
decorView = window.window!!.decorView
contentView = decorView.findViewById(android.R.id.content)
instance = this
// MUST WRAP all code within Service onCreate() in try..catch to prevent any crash loops
try {
// Additional try..catch wrapper as the event listeners chain or the super.onCreate() method
Expand Down Expand Up @@ -285,7 +284,6 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
ColorManager.removeOnChangedListener(onColorChangeListener)
super.onDestroy()
RimeDaemon.destroySession(javaClass.name)
instance = null
}

private fun handleReturnKey() {
Expand Down Expand Up @@ -508,7 +506,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
Timber.d("EditorInfo: normal -> exclude, packageName=%s", attribute.packageName)
} else {
normalTextEditor = true
DraftHelper.onInputEventChanged()
currentInputConnection?.let { DraftHelper.onExtractedTextChanged(it) }
}
}
}
Expand All @@ -519,13 +517,13 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
Timber.d("onFinishInputView: finishingInput=$finishingInput")
inputDeviceManager.onFinishInputView()
currentInputConnection?.apply {
if (normalTextEditor) {
DraftHelper.onExtractedTextChanged(this)
}
finishComposingText()
monitorCursorAnchor(false)
}
postRimeJob {
if (normalTextEditor) {
DraftHelper.onInputEventChanged()
}
clearComposition()
}
InputFeedbackManager.finishInput()
Expand All @@ -542,13 +540,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}
if (clearMeatKeyState) {
ic.clearMetaKeyStates(KeyEvent.getModifierMetaStateMask())
DraftHelper.onInputEventChanged()
}
}

private fun commitTextByChar(text: String) {
for (char in text) {
commitText(char.toString())
DraftHelper.onExtractedTextChanged(ic)
}
}

Expand Down Expand Up @@ -952,12 +944,5 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
companion object {
/** Delimiter regex to split language/locale tags. */
private val DELIMITER_SPLITTER = """[-_]""".toRegex()

var instance: TrimeInputMethodService? = null

@JvmStatic
fun getService(): TrimeInputMethodService = instance ?: throw IllegalStateException("TrimeInputMethodService is not initialized")

fun getServiceOrNull(): TrimeInputMethodService? = instance
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@

package com.osfans.trime.ui.fragments

import android.content.SharedPreferences
import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import com.osfans.trime.R
import com.osfans.trime.data.prefs.AppPrefs
import com.osfans.trime.data.theme.ThemeManager
import com.osfans.trime.ime.core.TrimeInputMethodService
import com.osfans.trime.ui.components.PaddingPreferenceFragment
import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.ui.main.settings.KeySoundEffectPickerDialog
import kotlinx.coroutines.launch

class KeyboardFragment :
PaddingPreferenceFragment(),
SharedPreferences.OnSharedPreferenceChangeListener {
class KeyboardFragment : PaddingPreferenceFragment() {
private val viewModel: MainViewModel by activityViewModels()

override fun onCreatePreferences(
Expand All @@ -35,31 +29,8 @@ class KeyboardFragment :
}
}

override fun onSharedPreferenceChanged(
sharedPreferences: SharedPreferences?,
key: String?,
) {
when (key) {
"keyboard__key_long_press_timeout",
"keyboard__key_repeat_interval",
"keyboard__show_key_popup",
AppPrefs.Keyboard.LANDSCAPE_MODE, AppPrefs.Keyboard.SPLIT_SPACE_PERCENT,
"keyboard__show_window",
"keyboard__inline_preedit", "keyboard__soft_cursor",
-> {
TrimeInputMethodService.getServiceOrNull()?.recreateInputView(ThemeManager.activeTheme)
}
}
}

override fun onResume() {
super.onResume()
viewModel.disableTopOptionsMenu()
preferenceScreen.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
}

override fun onPause() {
super.onPause()
preferenceScreen.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
}
}

0 comments on commit 0be5946

Please sign in to comment.