Skip to content

Instantly share code, notes, and snippets.

@JyotimoyKashyap
Created April 12, 2025 07:27
Show Gist options
  • Select an option

  • Save JyotimoyKashyap/21b56900487c0d60f3fe3f93769b10aa to your computer and use it in GitHub Desktop.

Select an option

Save JyotimoyKashyap/21b56900487c0d60f3fe3f93769b10aa to your computer and use it in GitHub Desktop.
Icon Picker
@Composable
fun IconPicker(
onIconSelected: (String, ImageVector) -> Unit,
iconPickerHeight: Float = 1f,
) {
val icons = Util.iconUtil.getListOfIcons(
iconNameList = stringArrayResource(R.array.icon_names)
.toList()
)
val searchText = remember { mutableStateOf("") }
val filteredIcons = remember { mutableStateOf(icons) }
LaunchedEffect(searchText.value) {
if (searchText.value.isEmpty()) {
filteredIcons.value = icons
} else {
filteredIcons.value = icons.filter { iconItem ->
val regex =
Regex(searchText.value.replace("\\s".toRegex(), ""), RegexOption.IGNORE_CASE)
regex.containsMatchIn(iconItem.id)
}
filteredIcons.value =
IconUtil.sortIconList(filteredIcons.value, searchString = searchText.value)
}
}
val iconsSearch = remember { derivedStateOf { filteredIcons } }
SpeseTheme {
Surface(
shape = RoundedCornerShape(
topStart = MaterialTheme.spacing.small,
topEnd = MaterialTheme.spacing.small
)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(iconPickerHeight)
) {
SearchBar(
onTextChange = {
searchText.value = it
}
)
if (iconsSearch.value.value.isEmpty()) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
contentAlignment = Alignment.Center
) {
val noDataMsg = stringResource(
R.string.no_icons_found_for, searchText.value
)
Text(
text = noDataMsg,
style = MaterialTheme.typography.bodyMedium
)
}
} else {
LazyHorizontalGrid(
modifier = Modifier
.padding(horizontal = MaterialTheme.spacing.extraSmall),
rows = GridCells.Adaptive(minSize = 45.dp),
state = rememberLazyGridState()
) {
items(
count = iconsSearch.value.value.size,
itemContent = { index ->
IconItemView(
iconItem = iconsSearch.value.value[index],
onIconSelected = onIconSelected
)
}
)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment