Skip to content

Instantly share code, notes, and snippets.

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

  • Save JyotimoyKashyap/05820072df28ef33048a0511c712ca96 to your computer and use it in GitHub Desktop.

Select an option

Save JyotimoyKashyap/05820072df28ef33048a0511c712ca96 to your computer and use it in GitHub Desktop.
IconUtil Object Class
object IconUtil {
/**
* @param iconId name of the icon from the Icons.Filled class
* @return [ImageVector] object
*/
fun createImageVector(iconId: String): ImageVector {
try {
val className = "androidx.compose.material.icons.filled.${iconId}Kt"
val cl = Class.forName(className)
val method = cl.declaredMethods.first()
return method.invoke(null, Icons.Filled) as ImageVector
} catch (ex: Exception) {
return Icons.Filled.Error
}
}
/**
* @param iconNameList list of icon names from the Icons.Filled class
* @return [IconItem] list of IconItem object
*/
fun getListOfIcons(iconNameList: List<String>): List<IconItem> {
val icons = mutableListOf<IconItem>()
iconNameList.forEach { iconId ->
icons.add(
IconItem(
id = iconId,
image = createImageVector(iconId)
)
)
}
return icons
}
/***
* Sort by this example
* search string Ho
* icon names = {Home, AHome, AtHome, AteHome}
* in this example, the search string "Ho" is present in all the string in this following way
* Home -> 12
* AHome -> 23
* AtHome -> 34
* AteHome -> 45
*
* This is how the sorting should be
*/
fun sortIconList(iconList: List<IconItem>, searchString: String): List<IconItem> {
return iconList.sortedWith { o1, o2 ->
val index1 = o1.id.indexOf(searchString, ignoreCase = true)
val index2 = o2.id.indexOf(searchString, ignoreCase = true)
index1.compareTo(index2)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment