Skip to content

Instantly share code, notes, and snippets.

@pillowsoft
Created October 14, 2014 15:00
Show Gist options
  • Save pillowsoft/c2cf1c3f7c9dc2ffa9a4 to your computer and use it in GitHub Desktop.
Save pillowsoft/c2cf1c3f7c9dc2ffa9a4 to your computer and use it in GitHub Desktop.
TipCalculator View Model using FSharp.ViewModule.Core
type TipCalculatorViewModel() as this =
inherit ViewModelBaseWithNavigation()
// Create our backing fields
let subTotal = this.Factory.Backing(<@ this.SubTotal @>, 0.0)
let receiptTotal = this.Factory.Backing(<@ this.ReceiptTotal @>, 0.0)
let tipPercent = this.Factory.Backing(<@ this.TipPercent @>, 15.0)
// This is only used to disable the button
let handlePaymentCommand = this.Factory.CommandSyncChecked((fun _ -> ()), (fun _ -> false))
// Add dependencies for INotifyPropertyChanged to be handled correctly
do
this.DependencyTracker.AddPropertyDependencies(<@ this.TipAmount @>, [ <@@ this.SubTotal @@> ; <@@ this.ReceiptTotal @@> ; <@@ this.TipPercent @@>])
this.DependencyTracker.AddPropertyDependencies(<@ this.Total @>, [ <@@ this.SubTotal @@> ; <@@ this.ReceiptTotal @@> ; <@@ this.TipPercent @@>])
member this.SubTotal with get() = subTotal.Value and set(v) = subTotal.Value <- v
member this.ReceiptTotal with get() = receiptTotal.Value and set(v) = receiptTotal.Value <- v
member this.TipPercent with get() = tipPercent.Value and set(v) = tipPercent.Value <- v
member this.TipAmount = Math.Round(this.TipPercent * this.SubTotal / 100.0, 2)
/// Total value, rounded to nearest quarter.
member this.Total = Math.Round(4.0 * (this.ReceiptTotal + this.TipAmount)) / 4.0
member this.HandlePayment = handlePaymentCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment