|
| 1 | +package com.algolia.exchange.query.categories.hits |
| 2 | + |
| 3 | +import android.R |
| 4 | +import androidx.compose.foundation.Image |
| 5 | +import androidx.compose.foundation.background |
| 6 | +import androidx.compose.foundation.layout.* |
| 7 | +import androidx.compose.foundation.rememberScrollState |
| 8 | +import androidx.compose.foundation.verticalScroll |
| 9 | +import androidx.compose.material.Icon |
| 10 | +import androidx.compose.material.MaterialTheme |
| 11 | +import androidx.compose.material.Text |
| 12 | +import androidx.compose.material.icons.Icons |
| 13 | +import androidx.compose.material.icons.filled.Category |
| 14 | +import androidx.compose.runtime.Composable |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.layout.ContentScale |
| 17 | +import androidx.compose.ui.text.AnnotatedString |
| 18 | +import androidx.compose.ui.text.style.TextOverflow |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import coil.compose.rememberImagePainter |
| 21 | +import com.algolia.instantsearch.compose.highlighting.toAnnotatedString |
| 22 | +import com.algolia.instantsearch.compose.hits.HitsState |
| 23 | +import com.algolia.instantsearch.compose.searchbox.SearchBox |
| 24 | +import com.algolia.instantsearch.compose.searchbox.SearchBoxState |
| 25 | +import com.algolia.instantsearch.core.highlighting.DefaultPostTag |
| 26 | +import com.algolia.instantsearch.core.highlighting.DefaultPreTag |
| 27 | +import com.algolia.instantsearch.core.highlighting.HighlightTokenizer |
| 28 | +import com.algolia.search.model.search.Facet |
| 29 | + |
| 30 | +@Composable |
| 31 | +fun SearchScreen( |
| 32 | + modifier: Modifier = Modifier, |
| 33 | + searchBoxState: SearchBoxState, |
| 34 | + hitsState: HitsState<Product>, |
| 35 | + categoriesState: HitsState<Facet>, |
| 36 | +) { |
| 37 | + val scrollState = rememberScrollState() |
| 38 | + Column( |
| 39 | + modifier = modifier |
| 40 | + .fillMaxWidth() |
| 41 | + .verticalScroll(scrollState) |
| 42 | + ) { |
| 43 | + SearchBox( |
| 44 | + modifier = Modifier |
| 45 | + .fillMaxWidth() |
| 46 | + .padding(12.dp), |
| 47 | + searchBoxState = searchBoxState, |
| 48 | + ) |
| 49 | + |
| 50 | + Categories(categories = categoriesState.hits) |
| 51 | + Products(products = hitsState.hits) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +@Composable |
| 56 | +private fun Categories( |
| 57 | + modifier: Modifier = Modifier, |
| 58 | + categories: List<Facet>, |
| 59 | +) { |
| 60 | + Column(modifier) { |
| 61 | + SectionTitle( |
| 62 | + modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 4.dp), |
| 63 | + title = "Categories" |
| 64 | + ) |
| 65 | + categories.forEach { category -> |
| 66 | + CategoryRow(category = category) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@Composable |
| 72 | +private fun CategoryRow( |
| 73 | + modifier: Modifier = Modifier, |
| 74 | + category: Facet |
| 75 | +) { |
| 76 | + Row( |
| 77 | + modifier |
| 78 | + .background(MaterialTheme.colors.surface) |
| 79 | + .fillMaxWidth() |
| 80 | + .padding(horizontal = 24.dp, vertical = 12.dp) |
| 81 | + ) { |
| 82 | + Icon( |
| 83 | + imageVector = Icons.Default.Category, |
| 84 | + tint = MaterialTheme.colors.onSurface.copy(alpha = 0.2f), |
| 85 | + contentDescription = null |
| 86 | + ) |
| 87 | + Text( |
| 88 | + text = category.highlighted.toAnnotatedString(), |
| 89 | + modifier = Modifier.padding(start = 12.dp), |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +@Composable |
| 96 | +private fun Products(products: List<Product>) { |
| 97 | + SectionTitle( |
| 98 | + modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp), |
| 99 | + title = "Products" |
| 100 | + ) |
| 101 | + products.forEach { product -> |
| 102 | + ProductRow(product = product) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +@Composable |
| 107 | +private fun ProductRow(modifier: Modifier = Modifier, product: Product) { |
| 108 | + Row( |
| 109 | + modifier |
| 110 | + .background(MaterialTheme.colors.surface) |
| 111 | + .padding(12.dp) |
| 112 | + ) { |
| 113 | + Image( |
| 114 | + modifier = Modifier.size(64.dp), |
| 115 | + contentScale = ContentScale.Crop, |
| 116 | + painter = rememberImagePainter( |
| 117 | + data = product.image, |
| 118 | + builder = { |
| 119 | + placeholder(R.drawable.ic_menu_report_image) |
| 120 | + error(R.drawable.ic_menu_report_image) |
| 121 | + }, |
| 122 | + ), |
| 123 | + contentDescription = product.name, |
| 124 | + ) |
| 125 | + |
| 126 | + Column(Modifier.padding(start = 8.dp)) { |
| 127 | + Text( |
| 128 | + text = product.name, |
| 129 | + maxLines = 2, |
| 130 | + overflow = TextOverflow.Ellipsis, |
| 131 | + style = MaterialTheme.typography.subtitle2 |
| 132 | + ) |
| 133 | + Text( |
| 134 | + text = product.description, |
| 135 | + maxLines = 2, |
| 136 | + overflow = TextOverflow.Ellipsis, |
| 137 | + style = MaterialTheme.typography.caption, |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +@Composable |
| 144 | +private fun SectionTitle(modifier: Modifier = Modifier, title: String) { |
| 145 | + Text( |
| 146 | + modifier = modifier, |
| 147 | + text = title, style = MaterialTheme.typography.subtitle2, |
| 148 | + color = MaterialTheme.colors.onBackground.copy(alpha = 0.4f), |
| 149 | + ) |
| 150 | +} |
| 151 | + |
| 152 | +private fun String.toAnnotatedString(): AnnotatedString { |
| 153 | + return HighlightTokenizer(DefaultPreTag, DefaultPostTag)(this).toAnnotatedString() |
| 154 | +} |
0 commit comments