|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.features.messages.impl.timeline.components.customreaction.picker |
| 9 | + |
| 10 | +import androidx.compose.foundation.layout.Arrangement |
| 11 | +import androidx.compose.foundation.layout.Column |
| 12 | +import androidx.compose.foundation.layout.PaddingValues |
| 13 | +import androidx.compose.foundation.layout.WindowInsets |
| 14 | +import androidx.compose.foundation.layout.aspectRatio |
| 15 | +import androidx.compose.foundation.layout.fillMaxSize |
| 16 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 17 | +import androidx.compose.foundation.layout.padding |
| 18 | +import androidx.compose.foundation.lazy.grid.GridCells |
| 19 | +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid |
| 20 | +import androidx.compose.foundation.lazy.grid.items |
| 21 | +import androidx.compose.foundation.pager.HorizontalPager |
| 22 | +import androidx.compose.foundation.pager.rememberPagerState |
| 23 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 24 | +import androidx.compose.material3.SecondaryTabRow |
| 25 | +import androidx.compose.material3.Tab |
| 26 | +import androidx.compose.runtime.Composable |
| 27 | +import androidx.compose.runtime.rememberCoroutineScope |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.res.stringResource |
| 30 | +import androidx.compose.ui.tooling.preview.PreviewParameter |
| 31 | +import androidx.compose.ui.unit.dp |
| 32 | +import io.element.android.emojibasebindings.Emoji |
| 33 | +import io.element.android.emojibasebindings.EmojibaseCategory |
| 34 | +import io.element.android.features.messages.impl.timeline.components.customreaction.EmojiItem |
| 35 | +import io.element.android.features.messages.impl.timeline.components.customreaction.icon |
| 36 | +import io.element.android.features.messages.impl.timeline.components.customreaction.title |
| 37 | +import io.element.android.libraries.designsystem.preview.ElementPreview |
| 38 | +import io.element.android.libraries.designsystem.preview.PreviewsDayNight |
| 39 | +import io.element.android.libraries.designsystem.text.toSp |
| 40 | +import io.element.android.libraries.designsystem.theme.components.Icon |
| 41 | +import io.element.android.libraries.designsystem.theme.components.SearchBar |
| 42 | +import io.element.android.libraries.ui.strings.CommonStrings |
| 43 | +import kotlinx.collections.immutable.ImmutableSet |
| 44 | +import kotlinx.collections.immutable.persistentSetOf |
| 45 | +import kotlinx.coroutines.launch |
| 46 | + |
| 47 | +@OptIn(ExperimentalMaterial3Api::class) |
| 48 | +@Composable |
| 49 | +fun EmojiPicker( |
| 50 | + onSelectEmoji: (Emoji) -> Unit, |
| 51 | + state: EmojiPickerState, |
| 52 | + selectedEmojis: ImmutableSet<String>, |
| 53 | + modifier: Modifier = Modifier, |
| 54 | +) { |
| 55 | + val coroutineScope = rememberCoroutineScope() |
| 56 | + val categories = state.categories |
| 57 | + val pagerState = rememberPagerState(pageCount = { EmojibaseCategory.entries.size }) |
| 58 | + |
| 59 | + Column(modifier) { |
| 60 | + SearchBar( |
| 61 | + modifier = Modifier.padding(bottom = 10.dp), |
| 62 | + query = state.searchQuery, |
| 63 | + onQueryChange = { state.eventSink(EmojiPickerEvents.UpdateSearchQuery(it)) }, |
| 64 | + resultState = state.searchResults, |
| 65 | + active = state.isSearchActive, |
| 66 | + onActiveChange = { state.eventSink(EmojiPickerEvents.ToggleSearchActive(it)) }, |
| 67 | + windowInsets = WindowInsets(0, 0, 0, 0), |
| 68 | + placeHolderTitle = stringResource(CommonStrings.emoji_picker_search_placeholder), |
| 69 | + ) { results -> |
| 70 | + val emojis = results |
| 71 | + LazyVerticalGrid( |
| 72 | + modifier = Modifier.fillMaxSize(), |
| 73 | + columns = GridCells.Adaptive(minSize = 48.dp), |
| 74 | + contentPadding = PaddingValues(vertical = 10.dp, horizontal = 16.dp), |
| 75 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 76 | + verticalArrangement = Arrangement.spacedBy(2.dp) |
| 77 | + ) { |
| 78 | + items(emojis, key = { it.unicode }) { item -> |
| 79 | + SelectableEmojiItem( |
| 80 | + item = item, |
| 81 | + selectedEmojis = selectedEmojis, |
| 82 | + onSelectEmoji = onSelectEmoji, |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (!state.isSearchActive) { |
| 89 | + SecondaryTabRow( |
| 90 | + selectedTabIndex = pagerState.currentPage, |
| 91 | + ) { |
| 92 | + EmojibaseCategory.entries.forEachIndexed { index, category -> |
| 93 | + Tab( |
| 94 | + icon = { |
| 95 | + Icon( |
| 96 | + imageVector = category.icon, |
| 97 | + contentDescription = stringResource(id = category.title) |
| 98 | + ) |
| 99 | + }, |
| 100 | + selected = pagerState.currentPage == index, |
| 101 | + onClick = { |
| 102 | + coroutineScope.launch { pagerState.animateScrollToPage(index) } |
| 103 | + } |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + HorizontalPager( |
| 109 | + state = pagerState, |
| 110 | + modifier = Modifier.fillMaxWidth(), |
| 111 | + ) { index -> |
| 112 | + val category = EmojibaseCategory.entries[index] |
| 113 | + val emojis = categories[category] ?: listOf() |
| 114 | + LazyVerticalGrid( |
| 115 | + modifier = Modifier.fillMaxSize(), |
| 116 | + columns = GridCells.Adaptive(minSize = 48.dp), |
| 117 | + contentPadding = PaddingValues(vertical = 10.dp, horizontal = 16.dp), |
| 118 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 119 | + verticalArrangement = Arrangement.spacedBy(2.dp) |
| 120 | + ) { |
| 121 | + items(emojis, key = { it.unicode }) { item -> |
| 122 | + SelectableEmojiItem( |
| 123 | + item = item, |
| 124 | + selectedEmojis = selectedEmojis, |
| 125 | + onSelectEmoji = onSelectEmoji, |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +@Composable |
| 135 | +private fun SelectableEmojiItem( |
| 136 | + item: Emoji, |
| 137 | + selectedEmojis: ImmutableSet<String>, |
| 138 | + onSelectEmoji: (Emoji) -> Unit, |
| 139 | +) { |
| 140 | + EmojiItem( |
| 141 | + modifier = Modifier.aspectRatio(1f), |
| 142 | + item = item, |
| 143 | + isSelected = selectedEmojis.contains(item.unicode), |
| 144 | + onSelectEmoji = onSelectEmoji, |
| 145 | + emojiSize = 32.dp.toSp(), |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +@PreviewsDayNight |
| 150 | +@Composable |
| 151 | +internal fun EmojiPickerPreview(@PreviewParameter(EmojiPickerStateProvider::class) state: EmojiPickerState) = ElementPreview { |
| 152 | + EmojiPicker( |
| 153 | + onSelectEmoji = {}, |
| 154 | + state = state, |
| 155 | + selectedEmojis = persistentSetOf("😀", "😄", "😃"), |
| 156 | + modifier = Modifier.fillMaxWidth(), |
| 157 | + ) |
| 158 | +} |
0 commit comments