Advertisement
Guest User

Untitled

a guest
May 9th, 2021
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.14 KB | None | 0 0
  1. @Composable
  2. fun ContextMenu(
  3.     builder: ContextMenu.Builder.() -> Unit
  4. ) {
  5.     val contextMenu = remember(builder){
  6.         val contextMenuBuilder = ContextMenu.Builder()
  7.         builder.invoke(contextMenuBuilder)
  8.         contextMenuBuilder.build()
  9.     }
  10.  
  11.     for(i in contextMenu.sections.indices){
  12.         ContextMenuSection(contextMenu.sections[i])
  13.         if(i < contextMenu.sections.size - 1) ContextMenuSeparator()
  14.     }
  15. }
  16.  
  17. @Composable
  18. private fun ContextMenuSection(section: ContextMenu.Section){
  19.     if(section.title!!.isNotBlank() && false){
  20.         TextLine(
  21.             text = section.title!!,
  22.             style = TextStyle()
  23.         )
  24.     }
  25.  
  26.     for(option in section.options){
  27.         ContextMenuOption(option)
  28.     }
  29. }
  30.  
  31. @Composable
  32. private fun ContextMenuOption(option: ContextMenu.Option){
  33.     Row(
  34.         Modifier
  35.             .clickable(onClick = option.onClick)
  36.             .fillMaxWidth()
  37.             .padding(
  38.                 vertical = 16.dp,
  39.                 horizontal = 32.dp
  40.             )
  41.     ){
  42.         Icon(
  43.             painter = painterResource(option.drawableId),
  44.             contentDescription = ""
  45.         )
  46.         Text(
  47.             text = option.label,
  48.             modifier = Modifier
  49.                 .padding(start = 16.dp)
  50.         )
  51.     }
  52. }
  53.  
  54. @Composable
  55. private fun ContextMenuSeparator(){
  56.     Divider(
  57.         Modifier
  58.             .fillMaxWidth()
  59.             .height(1.dp)
  60.     )
  61. }
  62.  
  63. class ContextMenu(val sections: List<Section>) {
  64.  
  65.     class Builder {
  66.  
  67.         private val sections = mutableListOf<Section>()
  68.         private var currentSection = Section()
  69.  
  70.         fun setSectionTitle(title: String){
  71.             if(currentSection.title != null){
  72.                 throw IllegalStateException("A section is already active." +
  73.                         "To start a new section call addSeparator() first." +
  74.                         "If addOption() is used without a setSectionTitle before the section title will be empty.".trimIndent())
  75.             }
  76.  
  77.             currentSection.title = title
  78.         }
  79.  
  80.         fun addOption(@DrawableRes drawableId: Int, label: String, onClick: () -> Unit){
  81.             if(currentSection.title == null) currentSection.title = ""
  82.  
  83.             currentSection.options.add(Option(drawableId, label, onClick))
  84.         }
  85.  
  86.         fun addSeparator(){
  87.             if(currentSection.title == null){
  88.                 throw IllegalStateException("No Section active. Call setSectionTitle() or addOption() first.")
  89.             }
  90.  
  91.             sections.add(currentSection)
  92.             currentSection = Section()
  93.         }
  94.  
  95.         fun build(): ContextMenu {
  96.             if(currentSection.title != null) sections.add(currentSection)
  97.             return ContextMenu(sections)
  98.         }
  99.  
  100.     }
  101.  
  102.     class Option(
  103.         @DrawableRes val drawableId: Int,
  104.         val label: String,
  105.         val onClick: () -> Unit
  106.     )
  107.  
  108.     class Section {
  109.         var title: String? = null
  110.             set(title){
  111.                 field = title
  112.                 options = mutableListOf()
  113.             }
  114.  
  115.         internal var options = mutableListOf<Option>()
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement