Skip to content

Instantly share code, notes, and snippets.

View stanch's full-sized avatar

Nick stanch

  • Product Manager at @snowplow
  • Lisbon, Portugal
View GitHub Profile
@stanch
stanch / gist:6421641
Created September 3, 2013 09:31
Annotation-based API for scala-workflow
/* Basic example */
@workflow[List] val x = List(1, 2) * List(4, 5)
/* Using @context */
@context[Option] val x = {
$(Some(42) + 1) should equal (Some(43))
$(Some(10) + Some(5) * Some(2)) should equal (Some(20))
}
@context[List] val x = {
l[ScrollView](
l[LinearLayout](
w[TextView],
w[Button],
l[LinearLayout](
w[ImageView],
w[ImageView]
),
// this will use ProgressBar(context, attrs, defStyle); context is passed for you automatically!
w[ProgressBar](null, android.R.attr.progressBarStyleLarge)
l[LinearLayout](
w[TextView] ~> text("Howdy?") ~> TextSize.large ~>
// this will automatically use LinearLayout.LayoutParams
layoutParams(MATCH_PARENT, WRAP_CONTENT),
w[ImageView] ~> hide ~> { x ⇒
// custom initialization
x.setScaleType(ImageView.ScaleType.FIT_START)
x.setAdjustViewBounds(true)
}
var status = slot[TextView]
setContentView {
l[LinearLayout](
w[TextView] ~> text("Downloading") ~> wire(status),
w[TextView] ~> text("the internet...") ~> id(Id.inet)
)
}
// after some time...
var greeting = slot[TextView]
l[LinearLayout](
w[Button] ~>
text("Greet me!") ~>
On.click {
greeting ~> text("Hello there") ~> show
},
w[TextView] ~> hide ~> wire(greeting)
// if screen width > 1000 dip, make layout horizontal, else make it vertical
l[LinearLayout](...) ~> (minWidth(1000 dp) ? horizontal | vertical)
// use different widget depending on screen width
(widerThan(1000 dp) ? w[BigTextView] |
widerThan(600 dp) ? w[MediumTextView] |
w[TextView]) ~> text("Gibberish")
// tweak a widget only if a condition is met
w[TextView] ~> text("Balderdash!") ~> (hdpi ? TextSize.large)
l[LinearLayout](
// It.stuff generates a new id
// Tag.stuff just returns "stuff", but boy! isn’t it fancy?
f[MyAwesomeFragment](Id.stuff, Tag.stuff, "items" → 4, "title" → "buzz"),
f[SupportMapFragment](Id.map, Tag.mainMap)
)
trait MyStyles extends LayoutDsl with MediaQueries with ExtraTweaks {
// sets text, large font size and a long click handler
def caption(cap: String)(implicit ctx: Context): Tweak[TextView] =
text(cap) + TextSize.large + On.longClick {
Toast.makeText(ctx, "I’m a caption", Toast.LENGTH_SHORT).show()
true
}
// sets large font size for high-density screens, medium font size for medium-density screens
// does nothing in other cases
myTextView ~>
text("Voilà") ~@>
fadeIn(400) ~@>
delay(500) ~>
text("Bye!") ~@>
fadeOut(400) ~>
text("Nobody will see this text")
// apply optional tweak: Option[Tweak[_]]
def large(implicit ctx: Context): Option[Tweak[TextView]] = hdpi ? TextSize.large
myTextView ~> large
// apply a list of tweaks: List[Tweak[_]]
myButton ~> List(text("The red button"), id(Id.redButton))
// functional reactive programming: Rx[Tweak[_]]
val caption = rx.Var("Olá")
myTextView ~> caption.map(c ⇒ text(c)) // sets text to “Olá”