Skip to content

Instantly share code, notes, and snippets.

@timrijckaert
Last active Oct 22, 2021
Embed
What would you like to do?
Similar to how Jetpack Compose's Modifier works, but for Cell NEBA
package com.bnpp.easybanking.zeplin.molecule
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextDecoration
import com.bnpp.easybanking.zeplin.molecule.CellData.Companion.DefaultMaxLines
import com.bnpp.easybanking.zeplin.molecule.CellData.Companion.DefaultTextDecoration
import com.bnpp.easybanking.zeplin.molecule.CellData.Companion.DefaultTextModifier
import com.bnppf.easybanking.exhaustive
interface Cell {
fun <R> fold(initial: R, operation: (R, Property) -> R): R
fun then(next: Cell): Cell = LinkedCell(this, next)
sealed interface Property : Cell {
override fun <R> fold(initial: R, operation: (R, Property) -> R): R =
operation(initial, this)
}
companion object : Cell {
override fun <R> fold(initial: R, operation: (R, Property) -> R): R = initial
override fun then(next: Cell): Cell = next
}
private class LinkedCell(
private val outer: Cell,
private val inner: Cell,
) : Cell {
override fun <R> fold(initial: R, operation: (R, Property) -> R): R =
inner.fold(outer.fold(initial, operation), operation)
}
}
sealed class Text : Cell.Property {
enum class Position {
Label1,
Label2,
Label3,
Label4,
Label5,
Label6,
Label7,
Label8,
Label9,
}
abstract val position: Position
abstract val textStyle: TextStyle
abstract val textDecoration: TextDecoration
abstract val maxLines: Int
abstract val modifierFunc: @Composable (Modifier) -> Modifier
data class Str(
private val text: String,
override val textStyle: TextStyle,
override val textDecoration: TextDecoration,
override val maxLines: Int,
override val modifierFunc: @Composable (Modifier) -> Modifier,
override val position: Position,
) : Text()
data class Res(
private val resourceId: Int,
override val textStyle: TextStyle,
override val textDecoration: TextDecoration,
override val maxLines: Int,
override val modifierFunc: @Composable (Modifier) -> Modifier,
override val position: Position,
) : Text()
}
class CellSelectionState(internal val isSelected: Boolean) : Cell.Property
class CellData private constructor() {
private var isSelected: Boolean = false
private var label1: Text? = null
private var label2: Text? = null
private var label3: Text? = null
private var label4: Text? = null
private var label5: Text? = null
private var label6: Text? = null
private var label7: Text? = null
private var label8: Text? = null
private var label9: Text? = null
constructor(cell: Cell) : this() {
cell.fold(this) { _, cellProp ->
when (cellProp) {
is Text -> {
when (cellProp.position) {
Text.Position.Label1 -> label1 = cellProp
Text.Position.Label2 -> label2 = cellProp
Text.Position.Label3 -> label3 = cellProp
Text.Position.Label4 -> label4 = cellProp
Text.Position.Label5 -> label5 = cellProp
Text.Position.Label6 -> label6 = cellProp
Text.Position.Label7 -> label7 = cellProp
Text.Position.Label8 -> label8 = cellProp
Text.Position.Label9 -> label9 = cellProp
}
}
is CellSelectionState -> isSelected = cellProp.isSelected
}.exhaustive
this
}
}
internal companion object {
val DefaultTextDecoration: TextDecoration = TextDecoration.None
val DefaultTextModifier: @Composable (Modifier) -> Modifier = { it }
const val DefaultMaxLines: Int = -1
}
}
fun Cell.isSelected(isSelected: Boolean): Cell = then(CellSelectionState(isSelected))
fun Cell.label1(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label1))
fun Cell.label2(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label2))
fun Cell.label3(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label3))
fun Cell.label4(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label4))
fun Cell.label5(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label5))
fun Cell.label6(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label6))
fun Cell.label7(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label7))
fun Cell.label8(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label8))
fun Cell.label9(
text: String,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Str(text, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label9))
fun Cell.label1(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label1))
fun Cell.label2(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label2))
fun Cell.label3(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label3))
fun Cell.label4(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label4))
fun Cell.label5(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label5))
fun Cell.label6(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label6))
fun Cell.label7(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label7))
fun Cell.label8(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label8))
fun Cell.label9(
@StringRes resId: Int,
textStyle: TextStyle,
maxLines: Int = DefaultMaxLines,
textDecoration: TextDecoration = DefaultTextDecoration,
modifierF: @Composable (Modifier) -> Modifier = DefaultTextModifier,
): Cell = then(Text.Res(resId, textStyle, textDecoration, maxLines, modifierF, Text.Position.Label9))
val a = Cell
.isSelected(true)
.label1("hello label 1", NEBATextStyles.Subtitle1Black87MediumLeft)
.label2("hello label 2", NEBATextStyles.Subtitle1Black87MediumLeft)
.label3(R.string.id_51332, NEBATextStyles.Subtitle1Black87MediumLeft)
.label4("hello 4", NEBATextStyles.Subtitle1Black87MediumLeft)
.label6("hello 6", NEBATextStyles.Subtitle1Black87MediumLeft)
.label5("hello 5!", NEBATextStyles.Subtitle1Black87MediumLeft)
val cellData = CellData(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment