vi studio

Lessons · Examples · 31 of 36

Example: counter

A tally with a goal, split into files.

The Counter template: the counter from Your first app, grown into a small app. The count and its functions live in src/counter.vi, the colours in src/theme.vi, and each piece of the screen in src/components/. src/main.vi only puts the pieces together; every file sees the names in every other file, so there are no imports. Two spaces with size: max share the free room, so the card floats in the middle and the buttons sit near your thumb.

src/main.vi
src/config.vi
src/counter.vi
src/theme.vi
src/components/progress_card.vi
src/components/round_button.vi

src/main.vi

<# A counter with a goal.
   config.vi    app name, icon and splash screen
   theme.vi     colours
   counter.vi   the count and the functions that change it
   components/  the pieces the screen is built from #>

main home:
  color: page
  space_inside: top bottom 24, left right 20
  space_between: 24
  arrange: top center
  children:
    header
    space_above
    progress_card
    space_below
    buttons
    reset_link

  header:
    size: max, auto
    space_between: 4
    children:
      eyebrow
      title

    eyebrow:
      text: "COUNTER"
      text_style: 13, bold, violet

    title:
      text: "Today's tally"
      text_style: 30, bold, ink

  buttons:
    size: max, auto
    arrange: row, center
    space_between: 20
    children:
      round_button(-1, 18, false)
      round_button(1, 26, true)
      round_button(5, 18, false)

  reset_link:
    text: "Start over"
    text_style: muted, underline
    space_inside: 12, left right 16
    on_tap: start_over()

<# The two spaces share the free height: the card floats in the middle
   and the buttons sit low, near your thumb. #>
space_above, space_below:
  size: auto, max

src/config.vi

<# App settings: name, version, launcher icon, splash screen and screen sizes.
   Every file in src/ can see them, so nothing needs importing.
   Before your first store build, add a store id that never changes again:
     id: com.yourname.myapp #>
config:
  name: "{{app_name}}"
  version: "1.0.0"
  icon: @icon.png
  text_size: follow_phone
  splash:
    color: white
    logo: @splash.png
    time: 1 second
  screens:
    mobile: 0 to 600
    tablet: 600 to 1200
    desktop: 1200 to max

src/counter.vi

<# The data and what can change it. No screens here. #>
count = 0
goal = 20

change(amount):
  count = count + amount
  if (count < 0):
    count = 0

start_over():
  count = 0

<# How far to the goal, from 0 to 100 #>
progress():
  if (count >= goal):
    return 100
  return count * 100 / goal

status():
  if (count >= goal):
    return "Goal reached. Nice work!"
  return "{goal - count} more to reach {goal}"

src/theme.vi

<# The app's colours, in one place. Change one here and every screen follows. #>
violet = rgb(109, 74, 255)
violet_soft = rgb(237, 233, 255)
ink = rgb(28, 25, 46)
muted = rgb(124, 120, 145)
page = rgb(247, 246, 252)
track = rgb(230, 227, 243)

src/components/progress_card.vi

<# The white card with the big number and a bar that fills up to the goal #>
progress_card:
  size: max, auto
  color: white
  shape: rounded
  space_inside: top bottom 28, left right 24
  space_between: 14
  arrange: center
  children:
    big_number
    caption
    bar
    status_line

  big_number:
    text: "{count}"
    text_style: 88, bold, violet

  caption:
    text: "taps today"
    text_style: 15, muted

  bar:
    size: max, auto
    arrange: row
    shape: rounded
    color: track
    children:
      bar_fill

    <# Its space_inside makes the bar 8 points tall #>
    bar_fill:
      size: progress(), auto
      space_inside: 4
      color: violet
      shape: rounded

  status_line:
    text: status()
    text_style: 15, semibold, ink

src/components/round_button.vi

<# A round button that changes the count by `amount`.
   The big one (the + 1) is filled; the others are soft. #>
round_button(amount, size, filled):
  size: size
  color: violet if (filled) else violet_soft
  shape: circle
  arrange: center
  children:
    round_button_label(amount, filled)
  on_tap: change(amount)

round_button_label(amount, filled):
  text: "+{amount}" if (amount > 0) else "{amount}"
  text_style: 26 if (filled) else 18, bold, white if (filled) else violet

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.

More in Examples

About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.