Last active
August 30, 2020 16:57
-
-
Save TimLariviere/fbd015030cc28f6698f15d9114dceb16 to your computer and use it in GitHub Desktop.
Todo list app in Fabulous.XamarinForms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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