Skip to content

Instantly share code, notes, and snippets.

@TimLariviere

TimLariviere/TodoList.fs

Last active Aug 30, 2020
Embed
What would you like to do?
Todo list app in Fabulous.XamarinForms
namespace TodoList
module App =
type Todo =
{ Task: string }
type Model =
{ Todos: Todo list
EntryText: string }
type Msg =
| AddTodoFromEntryText
| RemoveTodo of Todo
| EntryTextChanged of string
let init () =
{ Todos = []; EntryText = "" }
let update msg model =
match msg with
| AddTodoFromEntryText ->
let newTodo = { Task = model.EntryText }
{ model with Todos = newTodo :: model.Todos; EntryText = "" }
| RemoveTodo todo ->
let filteredTodos = model.Todos |> List.filter (fun t -> t <> todo)
{ model with Todos = filteredTodos }
| EntryTextChanged newText ->
{ model with EntryText = newText }
let view model dispatch =
View.ContentPage(
View.StackLayout(
padding = (
if Device.RuntimePlatform = Device.iOS then
Thickness(20., 40., 20., 20.)
else
Thickness(20., 0.)
),
children = [
// Title
View.Label(
text = "My To-Do list",
fontSize = FontSize.fromNamedSize NamedSize.Title,
horizontalOptions = LayoutOptions.Center
)
// Add new todo
View.Grid(
coldefs = [ Star; Auto ],
children = [
View.Entry(
text = model.EntryText,
textChanged = fun e -> dispatch (EntryTextChanged e.NewTextValue)
)
View.Button(
text = "Add todo",
command = fun () -> dispatch AddTodoFromEntryText)
).Column(1)
]
)
// Todo list
View.ListView([
for todo in List.rev model.Todos ->
View.TextCell(
text = todo.Task,
contextActions = [
View.MenuItem(
text = "Complete",
command = fun () -> dispatch (RemoveTodo todo)
)
]
)
])
]
)
)
let program = XamarinFormsProgram.mkSimple init update view
type App() as app =
inherit Xamarin.Forms.Application()
let runner =
App.program
|> Program.withConsoleTrace
|> XamarinFormsProgram.run app
#if DEBUG
do runner.EnableLiveUpdate()
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment