-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ReedCopsey/master
Simplified and clarified example
- Loading branch information
Showing
5 changed files
with
54 additions
and
71 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,8 @@ | ||
namespace ViewModels | ||
namespace Views | ||
|
||
open System | ||
open System.Windows | ||
open FSharp.ViewModule | ||
open FsXaml | ||
|
||
type MainView = XAML<"MainWindow.xaml", true> | ||
|
||
type Score = { | ||
ScoreA: int | ||
ScoreB: int | ||
} with | ||
static member zero = {ScoreA = 0; ScoreB = 0} | ||
|
||
|
||
type ScoringEvent = IncA | DecA | IncB | DecB | New | ||
|
||
type Controller = Score -> ScoringEvent -> Score | ||
|
||
|
||
type MainViewModel(controller : Controller) as self = | ||
inherit EventViewModelBase<ScoringEvent>() | ||
|
||
let mutable score = Score.zero | ||
|
||
let scoreA = self.Factory.Backing(<@ self.ScoreA @>, "00") | ||
let scoreB = self.Factory.Backing(<@ self.ScoreB @>, "00") | ||
|
||
let updateView () = | ||
self.ScoreA <- score.ScoreA.ToString("D2") | ||
self.ScoreB <- score.ScoreB.ToString("D2") | ||
|
||
let eventHandler ev = | ||
score <- controller score ev | ||
|
||
do | ||
self.EventStream | ||
|> Observable.subscribe (eventHandler >> updateView) | ||
|> ignore | ||
|
||
member self.IncA = self.Factory.EventValueCommand(IncA) | ||
member self.DecA = self.Factory.EventValueCommand(DecA) | ||
member self.IncB = self.Factory.EventValueCommand(IncB) | ||
member self.DecB = self.Factory.EventValueCommand(DecB) | ||
member self.NewGame = self.Factory.EventValueCommand(New) | ||
|
||
member self.ScoreA with get() = scoreA.Value | ||
and set value = scoreA.Value <- value | ||
member self.ScoreB with get() = scoreB.Value | ||
and set value = scoreB.Value <- value | ||
|
||
|
||
module Handling = | ||
|
||
let controller score ev = | ||
match ev with | ||
| IncA -> {score with ScoreA = score.ScoreA + 1} | ||
| DecA -> {score with ScoreA = score.ScoreA - 1} | ||
| IncB -> {score with ScoreB = score.ScoreB + 1} | ||
| DecB -> {score with ScoreB = score.ScoreB - 1} | ||
| New -> Score.zero | ||
|
||
type MainView = XAML<"MainWindow.xaml"> | ||
|
||
type CompositionRoot() = | ||
member x.ViewModel with get() = MainViewModel(Handling.controller) | ||
member __.ViewModel = ViewModel.MainViewModel(ScoreModel.Score.adjustScore) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace ScoreModel | ||
|
||
[<AutoOpen>] | ||
module ScoreTypes = | ||
type Score = { ScoreA: int ; ScoreB: int } | ||
type ScoringEvent = IncA | DecA | IncB | DecB | New | ||
|
||
module Score = | ||
let zero = {ScoreA = 0; ScoreB = 0} | ||
let adjustScore score event = | ||
match event with | ||
| IncA -> {score with ScoreA = score.ScoreA + 1} | ||
| DecA -> {score with ScoreA = max (score.ScoreA - 1) 0} | ||
| IncB -> {score with ScoreB = score.ScoreB + 1} | ||
| DecB -> {score with ScoreB = max (score.ScoreB - 1) 0} | ||
| New -> zero |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace ViewModel | ||
|
||
open ScoreModel | ||
open FSharp.ViewModule | ||
|
||
type MainViewModel(controller : Score -> ScoringEvent -> Score) as self = | ||
inherit EventViewModelBase<ScoringEvent>() | ||
|
||
let score = self.Factory.Backing(<@ self.Score @>, Score.zero) | ||
|
||
let eventHandler ev = score.Value <- controller score.Value ev | ||
|
||
do | ||
self.EventStream | ||
|> Observable.add eventHandler | ||
|
||
member this.IncA = this.Factory.EventValueCommand(IncA) | ||
member this.DecA = this.Factory.EventValueCommandChecked(DecA, (fun _ -> this.Score.ScoreA > 0), [ <@@ this.Score @@> ]) | ||
member this.IncB = this.Factory.EventValueCommand(IncB) | ||
member this.DecB = this.Factory.EventValueCommandChecked(DecB, (fun _ -> this.Score.ScoreB > 0), [ <@@ this.Score @@> ]) | ||
member this.NewGame = this.Factory.EventValueCommand(New) | ||
|
||
member __.Score = score.Value |
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