Skip to content

Commit d14d634

Browse files
committed
Sentences capitalization refactored
1 parent 129eea6 commit d14d634

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

app/src/main/kotlin/com/simplemobiletools/keyboard/helpers/Constants.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,40 @@ enum class ShiftState {
1919
}
2020
}
2121

22-
fun getShiftStateForText(context: Context, newText: CharSequence?): ShiftState {
23-
if (context.config.enableSentencesCapitalization.not()) return OFF
22+
fun getShiftStateForText(context: Context, newText: String?): ShiftState {
23+
if (context.config.enableSentencesCapitalization.not()) {
24+
return OFF
25+
}
2426

2527
val twoLastSymbols = newText?.takeLast(2)
2628
return when {
27-
twoLastSymbols?.getOrNull(1) == KEYCODE_SPACE.toChar() && endOfSentenceChars.contains(twoLastSymbols.getOrNull(0)) -> {
29+
shouldCapitalizeSentence(previousChar = twoLastSymbols?.getOrNull(0), currentChar = twoLastSymbols?.getOrNull(1)) -> {
2830
ON_ONE_CHAR
2931
}
30-
31-
else -> OFF
32+
else -> {
33+
OFF
34+
}
3235
}
3336
}
3437

35-
fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char): Boolean {
36-
return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar)
38+
fun getCapitalizationOnDelete(context: Context, text: CharSequence?): ShiftState {
39+
if (context.config.enableSentencesCapitalization.not()) {
40+
return OFF
41+
}
42+
43+
return if (text.isNullOrEmpty() || shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))) {
44+
ON_ONE_CHAR
45+
} else {
46+
OFF
47+
}
3748
}
3849

39-
fun shouldCapitalizeOnDelete(text: CharSequence?): Boolean {
40-
if (text.isNullOrEmpty()) {
41-
return true
50+
private fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char?): Boolean {
51+
if (previousChar == null || currentChar == null) {
52+
return false
4253
}
4354

44-
return shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))
55+
return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar)
4556
}
4657
}
4758
}

app/src/main/kotlin/com/simplemobiletools/keyboard/services/SimpleKeyboardIME.kt

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
9595

9696
when (code) {
9797
MyKeyboard.KEYCODE_DELETE -> {
98-
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) {
99-
keyboard!!.setShifted(ShiftState.OFF)
100-
}
10198

102-
if (config.enableSentencesCapitalization) {
99+
if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
103100
val extractedText = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1)
104-
if (ShiftState.shouldCapitalizeOnDelete(text = extractedText)) {
105-
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
106-
}
101+
keyboard!!.setShifted(ShiftState.getCapitalizationOnDelete(context = this, text = extractedText))
107102
}
108103

109104
val selectedText = inputConnection.getSelectedText(0)
@@ -192,16 +187,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
192187
inputConnection.commitText(codeChar.toString(), 1)
193188
}
194189

195-
if (keyboardMode == KEYBOARD_LETTERS) {
196-
if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) {
197-
keyboard!!.setShifted(ShiftState.OFF)
198-
}
199-
if (config.enableSentencesCapitalization && ShiftState.shouldCapitalizeSentence(
200-
previousChar = originalText.lastOrNull(), currentChar = code.toChar()
201-
)
202-
) {
203-
keyboard!!.setShifted(ShiftState.ON_ONE_CHAR)
204-
}
190+
if (keyboardMode == KEYBOARD_LETTERS && keyboard!!.mShiftState != ShiftState.ON_PERMANENT) {
191+
keyboard!!.setShifted(ShiftState.getShiftStateForText(this, newText = "$originalText$codeChar"))
205192
keyboardView!!.invalidateAllKeys()
206193
}
207194

@@ -218,7 +205,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
218205
// TODO: Change keyboardMode to enum class
219206
keyboardMode = KEYBOARD_LETTERS
220207
val text = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0)?.text
221-
val newShiftState = ShiftState.getShiftStateForText(this, text)
208+
val newShiftState = ShiftState.getShiftStateForText(this, text?.toString().orEmpty())
222209

223210
keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState)
224211

0 commit comments

Comments
 (0)