mirror of
				https://github.com/SimpleMobileTools/Simple-Keyboard.git
				synced 2025-06-05 21:49:26 +02:00 
			
		
		
		
	Sentences capitalization refactored
This commit is contained in:
		| @@ -19,30 +19,41 @@ enum class ShiftState { | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         fun getShiftStateForText(context: Context, newText: CharSequence?): ShiftState { | ||||
|             if (context.config.enableSentencesCapitalization.not()) return OFF | ||||
|         fun getShiftStateForText(context: Context, newText: String?): ShiftState { | ||||
|             if (context.config.enableSentencesCapitalization.not()) { | ||||
|                 return OFF | ||||
|             } | ||||
|  | ||||
|             val twoLastSymbols = newText?.takeLast(2) | ||||
|             return when { | ||||
|                 twoLastSymbols?.getOrNull(1) == KEYCODE_SPACE.toChar() && endOfSentenceChars.contains(twoLastSymbols.getOrNull(0)) -> { | ||||
|                 shouldCapitalizeSentence(previousChar = twoLastSymbols?.getOrNull(0), currentChar = twoLastSymbols?.getOrNull(1)) -> { | ||||
|                     ON_ONE_CHAR | ||||
|                 } | ||||
|  | ||||
|                 else -> OFF | ||||
|                 else -> { | ||||
|                     OFF | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char): Boolean { | ||||
|         fun getCapitalizationOnDelete(context: Context, text: CharSequence?): ShiftState { | ||||
|             if (context.config.enableSentencesCapitalization.not()) { | ||||
|                 return OFF | ||||
|             } | ||||
|  | ||||
|             return if (text.isNullOrEmpty() || shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1))) { | ||||
|                 ON_ONE_CHAR | ||||
|             } else { | ||||
|                 OFF | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private fun shouldCapitalizeSentence(previousChar: Char?, currentChar: Char?): Boolean { | ||||
|             if (previousChar == null || currentChar == null) { | ||||
|                 return false | ||||
|             } | ||||
|  | ||||
|             return currentChar.code == KEYCODE_SPACE && endOfSentenceChars.contains(previousChar) | ||||
|         } | ||||
|  | ||||
|         fun shouldCapitalizeOnDelete(text: CharSequence?): Boolean { | ||||
|             if (text.isNullOrEmpty()) { | ||||
|                 return true | ||||
|             } | ||||
|  | ||||
|             return shouldCapitalizeSentence(currentChar = text.last(), previousChar = text.getOrNull(text.lastIndex - 1)) | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -95,15 +95,10 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL | ||||
|  | ||||
|         when (code) { | ||||
|             MyKeyboard.KEYCODE_DELETE -> { | ||||
|                 if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) { | ||||
|                     keyboard!!.setShifted(ShiftState.OFF) | ||||
|                 } | ||||
|  | ||||
|                 if (config.enableSentencesCapitalization) { | ||||
|                 if (keyboard!!.mShiftState != ShiftState.ON_PERMANENT) { | ||||
|                     val extractedText = inputConnection.getTextBeforeCursor(3, 0)?.dropLast(1) | ||||
|                     if (ShiftState.shouldCapitalizeOnDelete(text = extractedText)) { | ||||
|                         keyboard!!.setShifted(ShiftState.ON_ONE_CHAR) | ||||
|                     } | ||||
|                     keyboard!!.setShifted(ShiftState.getCapitalizationOnDelete(context = this, text = extractedText)) | ||||
|                 } | ||||
|  | ||||
|                 val selectedText = inputConnection.getSelectedText(0) | ||||
| @@ -192,16 +187,8 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL | ||||
|                     inputConnection.commitText(codeChar.toString(), 1) | ||||
|                 } | ||||
|  | ||||
|                 if (keyboardMode == KEYBOARD_LETTERS) { | ||||
|                     if (keyboard!!.mShiftState == ShiftState.ON_ONE_CHAR) { | ||||
|                         keyboard!!.setShifted(ShiftState.OFF) | ||||
|                     } | ||||
|                     if (config.enableSentencesCapitalization && ShiftState.shouldCapitalizeSentence( | ||||
|                             previousChar = originalText.lastOrNull(), currentChar = code.toChar() | ||||
|                         ) | ||||
|                     ) { | ||||
|                         keyboard!!.setShifted(ShiftState.ON_ONE_CHAR) | ||||
|                     } | ||||
|                 if (keyboardMode == KEYBOARD_LETTERS && keyboard!!.mShiftState != ShiftState.ON_PERMANENT) { | ||||
|                     keyboard!!.setShifted(ShiftState.getShiftStateForText(this, newText = "$originalText$codeChar")) | ||||
|                     keyboardView!!.invalidateAllKeys() | ||||
|                 } | ||||
|  | ||||
| @@ -218,7 +205,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL | ||||
|             // TODO: Change keyboardMode to enum class | ||||
|             keyboardMode = KEYBOARD_LETTERS | ||||
|             val text = currentInputConnection?.getExtractedText(ExtractedTextRequest(), 0)?.text | ||||
|             val newShiftState = ShiftState.getShiftStateForText(this, text) | ||||
|             val newShiftState = ShiftState.getShiftStateForText(this, text?.toString().orEmpty()) | ||||
|  | ||||
|             keyboard = MyKeyboard(this, getKeyboardLayoutXML(), enterKeyType, shiftState = newShiftState) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user