Fix category coloring behavior

This commit is contained in:
Ensar Sarajčić 2023-08-30 11:25:50 +02:00
parent e38ba4ed19
commit e915500171
3 changed files with 28 additions and 36 deletions

View File

@ -20,6 +20,7 @@ import android.util.TypedValue
import android.view.* import android.view.*
import android.view.animation.AccelerateInterpolator import android.view.animation.AccelerateInterpolator
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.widget.ImageButton
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.PopupWindow import android.widget.PopupWindow
import android.widget.RadioGroup import android.widget.RadioGroup
@ -1560,30 +1561,25 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
allItems.addAll(emojis.map(EmojisAdapter.Item::Emoji)) allItems.addAll(emojis.map(EmojisAdapter.Item::Emoji))
} }
val checkIds = mutableMapOf<Int, String>() val checkIds = mutableMapOf<Int, String>()
val checkedChangedListener: (RadioGroup, Int) -> Unit = { _, checkedId ->
(keyboardViewBinding?.emojisList?.layoutManager as? GridLayoutManager)?.scrollToPositionWithOffset(
allItems.indexOfFirst { it is EmojisAdapter.Item.Category && it.value == checkIds[checkedId] },
0
)
}
keyboardViewBinding?.emojiCategoriesStrip?.apply { keyboardViewBinding?.emojiCategoriesStrip?.apply {
val strip = this
removeAllViews() removeAllViews()
this.setOnCheckedChangeListener(checkedChangedListener)
categories.entries.forEach { (category, emojis) -> categories.entries.forEach { (category, emojis) ->
ItemEmojiCategoryBinding.inflate(LayoutInflater.from(context), this, true).apply { ItemEmojiCategoryBinding.inflate(LayoutInflater.from(context), this, true).apply {
root.id = generateViewId() root.id = generateViewId()
checkIds[root.id] = category checkIds[root.id] = category
root.setButtonDrawable(emojis.first().getCategoryIcon()) root.setImageResource(emojis.first().getCategoryIcon())
root.buttonTintList = ColorStateList( root.setOnClickListener {
arrayOf( strip.children.filterIsInstance<ImageButton>().forEach {
intArrayOf(android.R.attr.state_checked), it.imageTintList = ColorStateList.valueOf(context.getProperTextColor())
intArrayOf(-android.R.attr.state_checked), }
), root.imageTintList = ColorStateList.valueOf(context.getProperPrimaryColor())
intArrayOf( (keyboardViewBinding?.emojisList?.layoutManager as? GridLayoutManager)?.scrollToPositionWithOffset(
context.getProperPrimaryColor(), allItems.indexOfFirst { it is EmojisAdapter.Item.Category && it.value == category },
context.getProperTextColor() 0
) )
) }
root.imageTintList = ColorStateList.valueOf(context.getProperTextColor())
} }
} }
} }
@ -1615,10 +1611,12 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
.lastOrNull { it.value is EmojisAdapter.Item.Category && it.index <= firstVisibleIndex } .lastOrNull { it.value is EmojisAdapter.Item.Category && it.index <= firstVisibleIndex }
?.also { activeCategory -> ?.also { activeCategory ->
val id = checkIds.entries.first { it.value == (activeCategory.value as EmojisAdapter.Item.Category).value }.key val id = checkIds.entries.first { it.value == (activeCategory.value as EmojisAdapter.Item.Category).value }.key
keyboardViewBinding?.emojiCategoriesStrip?.apply { keyboardViewBinding?.emojiCategoriesStrip?.children?.filterIsInstance<ImageButton>()?.forEach {
setOnCheckedChangeListener(null) if (it.id == id) {
check(id) it.imageTintList = ColorStateList.valueOf(context.getProperPrimaryColor())
setOnCheckedChangeListener(checkedChangedListener) } else {
it.imageTintList = ColorStateList.valueOf(context.getProperTextColor())
}
} }
} }
} }

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RadioButton <ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emoji_value" android:id="@+id/emoji_value"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/ripple_all_corners_medium" android:background="@drawable/ripple_all_corners_medium"
android:gravity="center" android:gravity="center"
android:importantForAccessibility="no"
android:padding="@dimen/small_margin" android:padding="@dimen/small_margin"
android:button="@drawable/ic_emoji_category_smileys" /> android:src="@drawable/ic_emoji_category_activities" />

View File

@ -202,21 +202,15 @@
android:text="@string/abc" android:text="@string/abc"
android:textStyle="bold" /> android:textStyle="bold" />
<HorizontalScrollView <LinearLayout
android:id="@+id/emoji_categories_scrollable_strip" android:id="@+id/emoji_categories_strip"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginStart="@dimen/activity_margin" android:layout_gravity="center"
android:layout_toStartOf="@+id/emoji_palette_backspace"
android:layout_toEndOf="@+id/emoji_palette_mode_change" android:layout_toEndOf="@+id/emoji_palette_mode_change"
android:layout_toStartOf="@+id/emoji_palette_backspace"> android:gravity="center"
<RadioGroup android:orientation="horizontal" />
android:id="@+id/emoji_categories_strip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"/>
</HorizontalScrollView>
<ImageButton <ImageButton
android:id="@+id/emoji_palette_backspace" android:id="@+id/emoji_palette_backspace"