Skip to content

Instantly share code, notes, and snippets.

@nsk-mironov
Last active August 9, 2018 13:32
Show Gist options
  • Save nsk-mironov/e29364c9b64a8a10da94d79bcf1ba4d8 to your computer and use it in GitHub Desktop.
Save nsk-mironov/e29364c9b64a8a10da94d79bcf1ba4d8 to your computer and use it in GitHub Desktop.
Litho DSL
import com.facebook.litho.Column
import com.facebook.litho.Component
import com.facebook.litho.ComponentContext
import com.facebook.litho.Row
import com.facebook.litho.fresco.FrescoImage
import com.facebook.litho.sections.widget.RecyclerCollectionComponent
import com.facebook.litho.widget.Card
import com.facebook.litho.widget.CardClip
import com.facebook.litho.widget.EditText
import com.facebook.litho.widget.EmptyComponent
import com.facebook.litho.widget.Image
import com.facebook.litho.widget.Progress
import com.facebook.litho.widget.Recycler
import com.facebook.litho.widget.SolidColor
import com.facebook.litho.widget.Text
import com.facebook.litho.widget.VerticalScroll
@DslMarker
@Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE)
annotation class LithoDslMarker
@LithoDslMarker
object LithoDsl
inline fun LithoDsl.column(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Column.Builder).() -> Unit
): Column {
return Column.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.row(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Row.Builder).() -> Unit
): Row {
return Row.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.card(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Card.Builder).() -> Unit
): Card {
return Card.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.cardClip(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker CardClip.Builder).() -> Unit
): CardClip {
return CardClip.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.text(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Text.Builder).() -> Unit
): Text {
return Text.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.editText(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker EditText.Builder).() -> Unit
): EditText {
return EditText.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.empty(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker EmptyComponent.Builder).() -> Unit
): EmptyComponent {
return EmptyComponent.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.image(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Image.Builder).() -> Unit
): Image {
return Image.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.frescoImage(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker FrescoImage.Builder).() -> Unit
): FrescoImage {
return FrescoImage.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.solidColor(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker SolidColor.Builder).() -> Unit
): SolidColor {
return SolidColor.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.progress(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Progress.Builder).() -> Unit
): Progress {
return Progress.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.verticalScroll(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker VerticalScroll.Builder).() -> Unit
): VerticalScroll {
return VerticalScroll.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.recycler(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Recycler.Builder).() -> Unit
): Recycler {
return Recycler.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun LithoDsl.recyclerCollection(
context: ComponentContext,
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker RecyclerCollectionComponent.Builder).() -> Unit
): RecyclerCollectionComponent {
return RecyclerCollectionComponent.create(context, defStyleAttr, defStyleRes).apply(builder).build()
}
inline fun <C : Component.ContainerBuilder<C>> C.column(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Column.Builder).() -> Unit
): C {
return child(Column.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.row(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Row.Builder).() -> Unit
): C {
return child(Row.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.card(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Card.Builder).() -> Unit
): C {
return child(Card.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.cardClip(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker CardClip.Builder).() -> Unit
): C {
return child(CardClip.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.text(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Text.Builder).() -> Unit
): C {
return child(Text.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.editText(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker EditText.Builder).() -> Unit
): C {
return child(EditText.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.empty(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker EmptyComponent.Builder).() -> Unit
): C {
return child(EmptyComponent.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.image(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Image.Builder).() -> Unit
): C {
return child(Image.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.frescoImage(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker FrescoImage.Builder).() -> Unit
): C {
return child(FrescoImage.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.solidColor(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker SolidColor.Builder).() -> Unit
): C {
return child(SolidColor.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.progress(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Progress.Builder).() -> Unit
): C {
return child(Progress.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.verticalScroll(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker VerticalScroll.Builder).() -> Unit
): C {
return child(VerticalScroll.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.recycler(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker Recycler.Builder).() -> Unit
): C {
return child(Recycler.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
inline fun <C : Component.ContainerBuilder<C>> C.recyclerCollection(
defStyleAttr: Int = 0,
defStyleRes: Int = 0,
builder: (@LithoDslMarker RecyclerCollectionComponent.Builder).() -> Unit
): C {
return child(RecyclerCollectionComponent.create(context, defStyleAttr, defStyleRes).apply(builder).build())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment