Skip to content

Instantly share code, notes, and snippets.

@TimLariviere
Last active August 30, 2020 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimLariviere/fbd015030cc28f6698f15d9114dceb16 to your computer and use it in GitHub Desktop.
Save TimLariviere/fbd015030cc28f6698f15d9114dceb16 to your computer and use it in GitHub Desktop.
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