forked from android/compose-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Categories.kt
169 lines (161 loc) · 5.97 KB
/
Categories.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.jetsnack.ui.home.search
import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.dp
import com.example.jetsnack.model.SearchCategory
import com.example.jetsnack.model.SearchCategoryCollection
import com.example.jetsnack.ui.components.SnackImage
import com.example.jetsnack.ui.components.VerticalGrid
import com.example.jetsnack.ui.theme.JetsnackTheme
import kotlin.math.max
@Composable
fun SearchCategories(
categories: List<SearchCategoryCollection>
) {
LazyColumn {
itemsIndexed(categories) { index, collection ->
SearchCategoryCollection(collection, index)
}
}
Spacer(Modifier.height(8.dp))
}
@Composable
private fun SearchCategoryCollection(
collection: SearchCategoryCollection,
index: Int,
modifier: Modifier = Modifier
) {
Column(modifier) {
Text(
text = collection.name,
style = MaterialTheme.typography.h6,
color = JetsnackTheme.colors.textPrimary,
modifier = Modifier
.heightIn(min = 56.dp)
.padding(horizontal = 24.dp, vertical = 4.dp)
.wrapContentHeight()
)
VerticalGrid(Modifier.padding(horizontal = 16.dp)) {
val gradient = when (index % 2) {
0 -> JetsnackTheme.colors.gradient2_2
else -> JetsnackTheme.colors.gradient2_3
}
collection.categories.forEach { category ->
SearchCategory(
category = category,
gradient = gradient,
modifier = Modifier.padding(8.dp)
)
}
}
Spacer(Modifier.height(4.dp))
}
}
private val MinImageSize = 134.dp
private val CategoryShape = RoundedCornerShape(10.dp)
private const val CategoryTextProportion = 0.55f
@Composable
private fun SearchCategory(
category: SearchCategory,
gradient: List<Color>,
modifier: Modifier = Modifier
) {
Layout(
modifier = modifier
.aspectRatio(1.45f)
.shadow(elevation = 3.dp, shape = CategoryShape)
.clip(CategoryShape)
.background(Brush.horizontalGradient(gradient))
.clickable { /* todo */ },
content = {
Text(
text = category.name,
style = MaterialTheme.typography.subtitle1,
color = JetsnackTheme.colors.textSecondary,
modifier = Modifier
.padding(4.dp)
.padding(start = 8.dp)
)
SnackImage(
imageUrl = category.imageUrl,
contentDescription = null,
modifier = Modifier.fillMaxSize()
)
}
) { measurables, constraints ->
// Text given a set proportion of width (which is determined by the aspect ratio)
val textWidth = (constraints.maxWidth * CategoryTextProportion).toInt()
val textPlaceable = measurables[0].measure(Constraints.fixedWidth(textWidth))
// Image is sized to the larger of height of item, or a minimum value
// i.e. may appear larger than item (but clipped to the item bounds)
val imageSize = max(MinImageSize.roundToPx(), constraints.maxHeight)
val imagePlaceable = measurables[1].measure(Constraints.fixed(imageSize, imageSize))
layout(
width = constraints.maxWidth,
height = constraints.minHeight
) {
textPlaceable.placeRelative(
x = 0,
y = (constraints.maxHeight - textPlaceable.height) / 2 // centered
)
imagePlaceable.placeRelative(
// image is placed to end of text i.e. will overflow to the end (but be clipped)
x = textWidth,
y = (constraints.maxHeight - imagePlaceable.height) / 2 // centered
)
}
}
}
@Preview("default")
@Preview("dark theme", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Preview("large font", fontScale = 2f)
@Composable
private fun SearchCategoryPreview() {
JetsnackTheme {
SearchCategory(
category = SearchCategory(
name = "Desserts",
imageUrl = ""
),
gradient = JetsnackTheme.colors.gradient3_2
)
}
}