Skip to content

Commit

Permalink
Merge pull request #216 from growabeard/fix-toomanyfunctions-keyboards
Browse files Browse the repository at this point in the history
Combining and pulling members up to the superclass.
  • Loading branch information
andrewtavis authored Oct 22, 2024
2 parents 7018590 + cb6026f commit a25f8d4
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 1,570 deletions.
203 changes: 1 addition & 202 deletions app/src/main/java/be/scri/services/EnglishKeyboardIME.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package be.scri.services

import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import android.text.InputType
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
import be.scri.R
import be.scri.databinding.KeyboardViewCommandOptionsBinding
import be.scri.databinding.KeyboardViewKeyboardBinding
import be.scri.helpers.MyKeyboard
import be.scri.helpers.MyKeyboard.Companion.KEYCODE_ENTER
import be.scri.views.MyKeyboardView

class EnglishKeyboardIME : SimpleKeyboardIME() {
class EnglishKeyboardIME : SimpleKeyboardIME("English") {
override fun getKeyboardLayoutXML(): Int = R.xml.keys_letters_english

override var shiftPermToggleSpeed = 500
Expand All @@ -33,53 +27,6 @@ class EnglishKeyboardIME : SimpleKeyboardIME() {
override var hasTextBeforeCursor = false
override lateinit var binding: KeyboardViewCommandOptionsBinding

enum class ScribeState {
IDLE,
SELECT_COMMAND,
TRANSLATE,
CONJUGATE,
PLURAL,
SELECT_VERB_CONJUNCTION,
SELECT_CASE_DECLENSION,
ALREADY_PLURAL,
INVALID,
DISPLAY_INFORMATION,
}

private var currentState: ScribeState = ScribeState.IDLE
private lateinit var keyboardBinding: KeyboardViewKeyboardBinding
private var isAutoSuggestEnabled: Boolean = false

private fun shouldCommitPeriodAfterSpace(language: String): Boolean {
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
return sharedPref.getBoolean("period_on_double_tap_$language", false)
}

override fun onStartInputView(
editorInfo: EditorInfo?,
restarting: Boolean,
) {
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isSystemDarkMode = currentNightMode == Configuration.UI_MODE_NIGHT_YES
val isUserDarkMode = sharedPref.getBoolean("dark_mode", isSystemDarkMode)
updateEnterKeyColor(isUserDarkMode)
initializeEmojiButtons()
isAutoSuggestEnabled = sharedPref.getBoolean("emoji_suggestions_English", true)
updateButtonVisibility(isAutoSuggestEnabled)
setupIdleView()
super.onStartInputView(editorInfo, restarting)
setupCommandBarTheme(binding)
}

override fun commitPeriodAfterSpace() {
if (shouldCommitPeriodAfterSpace("English")) {
val inputConnection = currentInputConnection ?: return
inputConnection.deleteSurroundingText(1, 0)
inputConnection.commitText(". ", 1)
}
}

override fun onCreateInputView(): View {
binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
setupCommandBarTheme(binding)
Expand All @@ -97,89 +44,6 @@ class EnglishKeyboardIME : SimpleKeyboardIME() {
return keyboardHolder
}

private fun setupIdleView() {
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isSystemDarkMode = currentNightMode == Configuration.UI_MODE_NIGHT_YES
val isUserDarkMode = sharedPref.getBoolean("dark_mode", isSystemDarkMode)
when (isUserDarkMode) {
true -> {
binding.translateBtn.setBackgroundColor(getColor(R.color.transparent))
binding.conjugateBtn.setBackgroundColor(getColor(R.color.transparent))
binding.pluralBtn.setBackgroundColor(getColor(R.color.transparent))
binding.translateBtn.setTextColor(Color.WHITE)
binding.conjugateBtn.setTextColor(Color.WHITE)
binding.pluralBtn.setTextColor(Color.WHITE)
binding.separator2.setBackgroundColor(getColor(R.color.special_key_dark))
binding.separator3.setBackgroundColor(getColor(R.color.special_key_dark))
}
else -> {
binding.translateBtn.setBackgroundColor(getColor(R.color.transparent))
binding.conjugateBtn.setBackgroundColor(getColor(R.color.transparent))
binding.pluralBtn.setBackgroundColor(getColor(R.color.transparent))
binding.translateBtn.setTextColor(Color.BLACK)
binding.conjugateBtn.setTextColor(Color.BLACK)
binding.pluralBtn.setTextColor(Color.BLACK)
binding.separator2.setBackgroundColor(getColor(R.color.special_key_light))
binding.separator3.setBackgroundColor(getColor(R.color.special_key_light))
}
}
setupCommandBarTheme(binding)
binding.translateBtn.text = "Suggestion"
binding.conjugateBtn.text = "Suggestion"
binding.pluralBtn.text = "Suggestion"
binding.separator2.visibility = View.VISIBLE
binding.separator3.visibility = View.VISIBLE
binding.scribeKey.setOnClickListener {
updateButtonVisibility(false)
currentState = ScribeState.SELECT_COMMAND
Log.i("MY-TAG", "SELECT COMMAND STATE FROM English IME")
binding.scribeKey.foreground = getDrawable(R.drawable.close)
updateUI()
}
}

private fun setupSelectCommandView() {
binding.translateBtn.setBackgroundDrawable(getDrawable(R.drawable.button_background_rounded))
binding.conjugateBtn.setBackgroundDrawable(getDrawable(R.drawable.button_background_rounded))
binding.pluralBtn.setBackgroundDrawable(getDrawable(R.drawable.button_background_rounded))
binding.translateBtn.text = "Translate"
binding.conjugateBtn.text = "Conjugate"
binding.pluralBtn.text = "Plural"
binding.separator2.visibility = View.GONE
binding.separator3.visibility = View.GONE
super.setupCommandBarTheme(binding)
binding.scribeKey.setOnClickListener {
currentState = ScribeState.IDLE
Log.i("MY-TAG", "IDLE STATE")
binding.scribeKey.foreground = getDrawable(R.drawable.ic_scribe_icon_vector)
updateUI()
}
binding.translateBtn.setOnClickListener {
currentState = ScribeState.TRANSLATE
Log.i("MY-TAG", "TRANSLATE STATE")
updateUI()
}
binding.conjugateBtn.setOnClickListener {
Log.i("MY-TAG", "CONJUGATE STATE")
currentState = ScribeState.CONJUGATE
updateUI()
}
binding.pluralBtn.setOnClickListener {
Log.i("MY-TAG", "PLURAL STATE")
currentState = ScribeState.PLURAL
updateUI()
}
}

private fun updateEnterKeyColor(isDarkMode: Boolean? = null) {
when (currentState) {
ScribeState.IDLE -> keyboardView?.setEnterKeyColor(null, isDarkMode = isDarkMode)
ScribeState.SELECT_COMMAND -> keyboardView?.setEnterKeyColor(null, isDarkMode = isDarkMode)
else -> keyboardView?.setEnterKeyColor(getColor(R.color.dark_scribe_blue))
}
}

override fun onKey(code: Int) {
val inputConnection = currentInputConnection
if (keyboard == null || inputConnection == null) {
Expand Down Expand Up @@ -230,75 +94,10 @@ class EnglishKeyboardIME : SimpleKeyboardIME() {
}
}

private fun switchToToolBar() {
this.keyboardBinding = initializeKeyboardBinding()
val keyboardHolder = keyboardBinding.root
super.setupToolBarTheme(keyboardBinding)
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isSystemDarkMode = currentNightMode == Configuration.UI_MODE_NIGHT_YES
val isUserDarkMode = sharedPref.getBoolean("dark_mode", isSystemDarkMode)
when (isUserDarkMode) {
true -> {
keyboardBinding.topKeyboardDivider.setBackgroundColor(getColor(R.color.special_key_dark))
}
false -> {
keyboardBinding.topKeyboardDivider.setBackgroundColor(getColor(R.color.special_key_light))
}
}
keyboardView = keyboardBinding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
keyboardView!!.mOnKeyboardActionListener = this
keyboardBinding.scribeKey.setOnClickListener {
currentState = ScribeState.IDLE
switchToCommandToolBar()
updateUI()
}
setInputView(keyboardHolder)
}

private fun switchToCommandToolBar() {
val binding = KeyboardViewCommandOptionsBinding.inflate(layoutInflater)
this.binding = binding
val keyboardHolder = binding.root
setupCommandBarTheme(binding)
keyboardView = binding.keyboardView
keyboardView!!.setKeyboard(keyboard!!)
keyboardView!!.mOnKeyboardActionListener = this
keyboardBinding.scribeKey.setOnClickListener {
currentState = ScribeState.IDLE
setupSelectCommandView()
updateUI()
}
setInputView(keyboardHolder)
}

override fun onCreate() {
super.onCreate()
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType)
onCreateInputView()
setupCommandBarTheme(binding)
}

private fun updateUI() {
val sharedPref = getSharedPreferences("app_preferences", Context.MODE_PRIVATE)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val isSystemDarkMode = currentNightMode == Configuration.UI_MODE_NIGHT_YES
val isUserDarkMode = sharedPref.getBoolean("dark_mode", isSystemDarkMode)
when (currentState) {
ScribeState.IDLE -> {
setupIdleView()
initializeEmojiButtons()
updateButtonVisibility(isAutoSuggestEnabled)
}
ScribeState.SELECT_COMMAND -> setupSelectCommandView()
else -> switchToToolBar()
}
updateEnterKeyColor(isUserDarkMode)
}

private fun initializeKeyboardBinding(): KeyboardViewKeyboardBinding {
val keyboardBinding = KeyboardViewKeyboardBinding.inflate(layoutInflater)
return keyboardBinding
}
}
Loading

0 comments on commit a25f8d4

Please sign in to comment.