Skip to content

Commit 8a77115

Browse files
committed
fix comments
1 parent 8b24fee commit 8a77115

5 files changed

Lines changed: 99 additions & 114 deletions

File tree

app/src/keyboards/java/be/scri/helpers/ui/KeyboardUIManager.kt

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ import androidx.appcompat.content.res.AppCompatResources
1818
import androidx.core.content.ContextCompat
1919
import androidx.core.content.edit
2020
import androidx.core.graphics.toColorInt
21+
import androidx.core.view.updateLayoutParams
22+
import androidx.recyclerview.widget.GridLayoutManager
2123
import be.scri.R
2224
import be.scri.R.color.white
2325
import be.scri.databinding.InputMethodViewBinding
26+
import be.scri.helpers.AutoGridLayoutManager
27+
import be.scri.helpers.EMOJI_SPEC_FILE_PATH
28+
import be.scri.helpers.EmojiAdapter
29+
import be.scri.helpers.EmojiData
2430
import be.scri.helpers.KeyboardBase
2531
import be.scri.helpers.KeyboardLanguageMappingConstants.conjugatePlaceholder
2632
import be.scri.helpers.KeyboardLanguageMappingConstants.pluralPlaceholder
@@ -29,18 +35,11 @@ import be.scri.helpers.LanguageMappingConstants.getLanguageAlias
2935
import be.scri.helpers.PreferencesHelper
3036
import be.scri.helpers.PreferencesHelper.getIsDarkModeOrNot
3137
import be.scri.helpers.english.ENInterfaceVariables.ALREADY_PLURAL_MSG
38+
import be.scri.helpers.getCategoryIconRes
39+
import be.scri.helpers.parseRawEmojiSpecsFile
3240
import be.scri.models.ScribeState
3341
import be.scri.services.GeneralKeyboardIME
3442
import be.scri.views.KeyboardView
35-
import be.scri.helpers.EmojiAdapter
36-
import be.scri.helpers.EMOJI_SPEC_FILE_PATH
37-
import be.scri.helpers.parseRawEmojiSpecsFile
38-
import be.scri.helpers.getCategoryIconRes
39-
40-
import be.scri.helpers.AutoGridLayoutManager
41-
import be.scri.helpers.EmojiData
42-
import androidx.recyclerview.widget.GridLayoutManager
43-
import androidx.core.view.updateLayoutParams
4443

4544
/**
4645
* Manages the UI elements and state transitions for the GeneralKeyboardIME.
@@ -764,23 +763,22 @@ class KeyboardUIManager(
764763
}
765764

766765
/**
767-
* Displays the emoji palette by showing the emoji palette holder and hiding the keyboard view.
768-
* Based on Fossify's openEmojiPalette() and setupEmojis() approach.
766+
* Displays the emoji palette and hides the keyboard view.
769767
* Loads emojis from the emoji spec file on a background thread and populates the grid.
770768
*/
771769
fun showEmojiPalette() {
772770
binding.keyboardView.post {
773771
val keyboardHeight = binding.keyboardView.measuredHeight
774-
val toolbarHeight = binding.commandOptionsBar.measuredHeight.takeIf { it > 0 }
775-
?: context.resources.getDimensionPixelSize(R.dimen.toolbar_height)
772+
val toolbarHeight =
773+
binding.commandOptionsBar.measuredHeight.takeIf { it > 0 }
774+
?: context.resources.getDimensionPixelSize(R.dimen.toolbar_height)
776775

777776
binding.emojiPaletteHolder.updateLayoutParams {
778777
height = keyboardHeight + toolbarHeight
779778
}
780779
binding.emojiPaletteHolder.requestLayout()
781780
}
782781

783-
784782
binding.emojiPaletteHolder.visibility = View.VISIBLE
785783

786784
binding.keyboardView.visibility = View.GONE
@@ -795,7 +793,7 @@ class KeyboardUIManager(
795793
}
796794
binding.emojiPaletteModeChange.text = "ABC"
797795
binding.emojiPaletteModeChange.setTextColor(
798-
ContextCompat.getColor(context, R.color.emoji_palette_icons)
796+
ContextCompat.getColor(context, R.color.emoji_palette_icons),
799797
)
800798

801799
binding.emojiPaletteBackspace.setOnClickListener {
@@ -804,28 +802,23 @@ class KeyboardUIManager(
804802

805803
Thread {
806804
val fullEmojiList = parseRawEmojiSpecsFile(context, EMOJI_SPEC_FILE_PATH)
807-
val systemFontPaint = android.graphics.Paint().apply {
808-
typeface = android.graphics.Typeface.DEFAULT
809-
}
810-
val emojis = fullEmojiList.filter { emoji ->
811-
systemFontPaint.hasGlyph(emoji.emoji)
812-
}
805+
val systemFontPaint =
806+
android.graphics.Paint().apply {
807+
typeface = android.graphics.Typeface.DEFAULT
808+
}
809+
val emojis =
810+
fullEmojiList.filter { emoji ->
811+
systemFontPaint.hasGlyph(emoji.emoji)
812+
}
813813

814814
android.os.Handler(android.os.Looper.getMainLooper()).post {
815815
setupEmojiAdapter(emojis)
816816
}
817817
}.start()
818818
}
819819

820-
/**
821-
* Sets up the emoji RecyclerView grid and category strip.
822-
*
823-
* @param items The flat list of category headers and emojis.
824-
* @param categories The map of category name to emoji list for category strip setup.
825-
*/
826820
/**
827821
* Sets up the emoji RecyclerView adapter and category strip.
828-
* Based on Fossify's setupEmojiAdapter() implementation.
829822
*
830823
* @param emojis The filtered list of emojis the device can render.
831824
*/
@@ -836,36 +829,35 @@ class KeyboardUIManager(
836829
val emojiItemSize = context.resources.getDimensionPixelSize(R.dimen.emoji_item_size)
837830
val emojiLayoutManager = AutoGridLayoutManager(context, emojiItemSize)
838831

839-
emojiLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
840-
override fun getSpanSize(position: Int): Int =
841-
if (emojiItems[position] is EmojiAdapter.Item.Category) {
842-
emojiLayoutManager.spanCount
843-
} else {
844-
1
845-
}
846-
}
832+
emojiLayoutManager.spanSizeLookup =
833+
object : GridLayoutManager.SpanSizeLookup() {
834+
override fun getSpanSize(position: Int): Int =
835+
if (emojiItems[position] is EmojiAdapter.Item.Category) {
836+
emojiLayoutManager.spanCount
837+
} else {
838+
1
839+
}
840+
}
847841

848842
binding.emojisList.layoutManager = emojiLayoutManager
849-
binding.emojisList.adapter = EmojiAdapter(context, emojiItems) { emojiData ->
850-
listener.onEmojiSelected(emojiData.emoji)
851-
}
843+
binding.emojisList.adapter =
844+
EmojiAdapter(context, emojiItems) { emojiData ->
845+
listener.onEmojiSelected(emojiData.emoji)
846+
}
852847

853848
setupEmojiCategoryStrip(emojiCategories, emojiItems, emojiLayoutManager)
854849
}
855850

856851
/**
857852
* Groups emojis by category.
858-
* Based on Fossify's prepareEmojiCategories() implementation.
859853
*
860854
* @param emojis The full list of emojis.
861-
* @return A map of category name to list of emojis in that category.
855+
* @return A map of category name to list of emojis in corresponding category.
862856
*/
863-
private fun prepareEmojiCategories(emojis: List<EmojiData>): Map<String, List<EmojiData>> =
864-
emojis.groupBy { it.category }
857+
private fun prepareEmojiCategories(emojis: List<EmojiData>): Map<String, List<EmojiData>> = emojis.groupBy { it.category }
865858

866859
/**
867-
* Builds a flat list of category headers and emoji items for the RecyclerView.
868-
* Based on Fossify's prepareEmojiItems() implementation.
860+
* Builds a list of category headers and emoji items for the RecyclerView.
869861
*
870862
* @param categories The map of categories to their emojis.
871863
* @return A flat list of [EmojiAdapter.Item] objects.
@@ -884,15 +876,6 @@ class KeyboardUIManager(
884876
* Tapping a category icon scrolls the emoji list to that category.
885877
*
886878
* @param categories The map of category names to their emojis.
887-
* @param items The full flat list used to find category positions.
888-
* @param layoutManager The GridLayoutManager used to scroll to positions.
889-
*/
890-
/**
891-
* Populates the emoji category strip at the bottom of the palette.
892-
* Tapping a category icon scrolls the emoji list to that category.
893-
* Based on Fossify's setupEmojiAdapter() category strip implementation.
894-
*
895-
* @param categories The map of category names to their emojis.
896879
* @param emojiItems The full flat list used to find category positions.
897880
* @param layoutManager The AutoGridLayoutManager used to scroll to positions.
898881
*/
@@ -906,29 +889,32 @@ class KeyboardUIManager(
906889
var activeButton: android.widget.ImageButton? = null
907890

908891
categories.keys.forEachIndexed { index, category ->
909-
val button = android.widget.ImageButton(context).apply {
910-
setImageResource(getCategoryIconRes(category))
911-
background = null
912-
imageAlpha = if (index == 0) 255 else 128 // 50% opacity for inactive
913-
layoutParams = android.widget.LinearLayout.LayoutParams(
914-
0,
915-
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
916-
1f,
917-
)
918-
setOnClickListener {
919-
activeButton?.imageAlpha = 128 // dim previous
920-
imageAlpha = 255 // full opacity for active
921-
activeButton = this
922-
923-
val position = emojiItems.indexOfFirst {
924-
it is EmojiAdapter.Item.Category && it.value == category
925-
}
926-
if (position != -1) {
927-
(layoutManager as androidx.recyclerview.widget.LinearLayoutManager)
928-
.scrollToPositionWithOffset(position, 0)
892+
val button =
893+
android.widget.ImageButton(context).apply {
894+
setImageResource(getCategoryIconRes(category))
895+
background = null
896+
imageAlpha = if (index == 0) 255 else 128 // 50% opacity for inactive
897+
layoutParams =
898+
android.widget.LinearLayout.LayoutParams(
899+
0,
900+
android.widget.LinearLayout.LayoutParams.MATCH_PARENT,
901+
1f,
902+
)
903+
setOnClickListener {
904+
activeButton?.imageAlpha = 128 // dim previous.
905+
imageAlpha = 255 // full opacity for active.
906+
activeButton = this
907+
908+
val position =
909+
emojiItems.indexOfFirst {
910+
it is EmojiAdapter.Item.Category && it.value == category
911+
}
912+
if (position != -1) {
913+
(layoutManager as androidx.recyclerview.widget.LinearLayoutManager)
914+
.scrollToPositionWithOffset(position, 0)
915+
}
929916
}
930917
}
931-
}
932918

933919
if (index == 0) activeButton = button
934920
binding.emojiCategoriesStrip.addView(button)

app/src/keyboards/java/be/scri/services/GeneralKeyboardIME.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,9 +527,11 @@ abstract class GeneralKeyboardIME(
527527
}
528528

529529
// MARK: Helper Methods
530+
530531
fun openEmojiKeyboard() {
531532
uiManager.showEmojiPalette()
532533
}
534+
533535
protected fun isPeriodAndCommaEnabled(): Boolean {
534536
val isPreferenceEnabled = PreferencesHelper.getEnablePeriodAndCommaABC(this, language)
535537
val isInSearchBar = isSearchBar()
@@ -1009,7 +1011,7 @@ abstract class GeneralKeyboardIME(
10091011
// MARK: Deletion Logic
10101012

10111013
/**
1012-
* Handles the logic for the Delete/Backspace key. It deletes characters from either
1014+
* Handles the logic for the Delete key. It deletes characters from either
10131015
* the main input field or the command bar, depending on the context.
10141016
* Delegated to BackspaceHandler.
10151017
*
@@ -1018,24 +1020,28 @@ abstract class GeneralKeyboardIME(
10181020
*/
10191021
fun handleDelete(isLongPress: Boolean = false) {
10201022
val inputConnection = currentInputConnection ?: return
1021-
val effectiveIsCommandBar = currentState != ScribeState.IDLE &&
1022-
currentState != ScribeState.SELECT_COMMAND
1023+
val effectiveIsCommandBar =
1024+
currentState != ScribeState.IDLE &&
1025+
currentState != ScribeState.SELECT_COMMAND
10231026

10241027
if (!effectiveIsCommandBar) {
10251028
val selectedText = inputConnection.getSelectedText(0)
10261029
if (selectedText.isNullOrEmpty()) {
1027-
// Use BreakIterator to delete full emoji characters
1030+
// Use BreakIterator to delete full emoji characters.
10281031
val prevText = inputConnection.getTextBeforeCursor(8, 0)
10291032
if (!prevText.isNullOrEmpty()) {
1030-
val breakIterator = android.icu.text.BreakIterator.getCharacterInstance()
1033+
val breakIterator =
1034+
android.icu.text.BreakIterator
1035+
.getCharacterInstance()
10311036
breakIterator.setText(prevText.toString())
10321037
val end = breakIterator.last()
10331038
val start = breakIterator.previous()
1034-
val count = if (start == android.icu.text.BreakIterator.DONE) {
1035-
1
1036-
} else {
1037-
(end - start).coerceAtLeast(1)
1038-
}
1039+
val count =
1040+
if (start == android.icu.text.BreakIterator.DONE) {
1041+
1
1042+
} else {
1043+
(end - start).coerceAtLeast(1)
1044+
}
10391045
inputConnection.deleteSurroundingText(count, 0)
10401046
return
10411047
}

app/src/main/java/be/scri/helpers/AutoGridLayoutManager.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.recyclerview.widget.RecyclerView
88
/**
99
* A GridLayoutManager that automatically calculates the number of columns
1010
* based on the available width and desired item width.
11-
* Based on Fossify's AutoGridLayoutManager approach.
1211
*
1312
* @param context The application context.
1413
* @param itemWidth The desired width of each item in pixels.
@@ -17,10 +16,7 @@ class AutoGridLayoutManager(
1716
context: Context,
1817
private val itemWidth: Int,
1918
) : GridLayoutManager(context, 1) {
20-
21-
/**
22-
* Recalculates the span count based on available width before laying out children.
23-
*/
19+
// Recalculates the span count based on available width before laying out children.
2420
override fun onLayoutChildren(
2521
recycler: RecyclerView.Recycler?,
2622
state: RecyclerView.State?,

0 commit comments

Comments
 (0)