vi studio

Lessons · Examples · 32 of 36

Example: to-do app

Lists, inputs, repeat and an empty state.

The To-do template. src/tasks.vi holds the list and everything that changes it; the screen is built from a header, an input bar, one card per task (repeat: task in tasks) and an empty state that shows when the list is empty.

src/main.vi
src/config.vi
src/tasks.vi
src/theme.vi
src/components/empty_state.vi
src/components/header.vi
src/components/input_bar.vi
src/components/task_row.vi

src/main.vi

<# A to-do list.
   config.vi    app name, icon and splash screen
   theme.vi     colours
   tasks.vi     the list and the functions that change it
   components/  the pieces the screen is built from #>

main home:
  color: page
  space_inside: top 24, left right 20
  space_between: 20
  children:
    header
    input_bar
    task_list

  <# Scrolls when the list is longer than the screen #>
  task_list:
    size: auto, max
    arrange: scroll
    space_between: 10
    children:
      task_row(tasks) if (tasks.count > 0) else empty_state

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/tasks.vi

<# The list of tasks and everything that changes it. No screens here. #>
tasks = [
  { text: "Plan the week", done: true },
  { text: "Buy fresh flowers", done: false },
  { text: "Call grandma", done: false },
  { text: "Go for a run", done: false }
]

add_task(text):
  if (text != ""):
    tasks.add({ text: text, done: false })

toggle_task(task):
  task.done = not task.done

delete_task(task):
  tasks.remove(task)

done_count():
  n = 0
  for task in tasks:
    if (task.done):
      n = n + 1
  return n

<# How much is done, from 0 to 100 #>
progress():
  if (tasks.count == 0):
    return 0
  return done_count() * 100 / tasks.count

summary():
  if (tasks.count == 0):
    return "Nothing on the list"
  if (done_count() == tasks.count):
    return "All done. Time for a break!"
  return "{done_count()} of {tasks.count} done"

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)
faint = rgb(190, 186, 206)
page = rgb(247, 246, 252)
track = rgb(230, 227, 243)

src/components/empty_state.vi

<# Shown when the list is empty #>
empty_state:
  size: max, auto
  space_inside: top 48
  space_between: 10
  arrange: center
  children:
    empty_badge
    empty_title
    empty_text

  empty_badge:
    size: 20
    shape: circle
    color: violet_soft
    arrange: center
    children:
      empty_icon

    empty_icon:
      icon: "check"
      text_style: 34, violet

  empty_title:
    text: "You're all clear"
    text_style: 20, bold, ink

  empty_text:
    text: "Add a task above to get started."
    text_style: 15, muted

src/components/header.vi

<# Title, how much is done, and a bar that fills up #>
header:
  size: max, auto
  space_between: 6
  children:
    eyebrow
    title
    summary_line
    bar

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

  title:
    text: "My tasks"
    text_style: 32, bold, ink

  summary_line:
    text: summary()
    text_style: 15, muted

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

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

src/components/input_bar.vi

<# Add what was typed, then empty the field #>
add_from_input():
  add_task(new_task.text)
  new_task:
    text: ""

<# A white bar with a text field and a round + button #>
input_bar:
  size: max, auto
  color: white
  shape: rounded
  shadow: true
  arrange: row, center
  space_inside: 8
  space_between: 8
  children:
    new_task
    add_button

new_task:
  input: "Add a task…"
  size: max, auto
  on_submit: add_from_input()

add_button:
  size: 14
  shape: circle
  color: violet
  arrange: center
  on_tap: add_from_input()
  children:
    add_icon

  add_icon:
    icon: "plus"
    text_style: 26, white

src/components/task_row.vi

<# One white card per task. repeat makes one for each task in the list. #>
task_row(tasks):
  repeat: task in tasks
  size: max, auto
  color: white
  shape: rounded
  shadow: true
  arrange: row, center
  space_between: 8
  space_inside: 10
  children:
    check(task)
    task_text(task)
    delete_button(task)
  on_swipe:
    left: delete_task(task)

check(task):
  icon: "check_circle_filled" if (task.done) else "circle_outline"
  space_inside: 10
  text_style: 26, violet if (task.done) else faint
  spoken: "Mark as not done" if (task.done) else "Mark as done"
  on_tap: toggle_task(task)

task_text(task):
  size: max, auto
  space_inside: top bottom 12
  text: task.text
  text_style: 17, "strikethrough" if (task.done) else "normal", muted if (task.done) else ink
  on_tap: toggle_task(task)

delete_button(task):
  icon: "close"
  space_inside: 12
  text_style: 20, faint
  spoken: "Delete task"
  on_tap: delete_task(task)

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.