Skip to content

Commit

Permalink
Merge pull request #1 from ReedCopsey/master
Browse files Browse the repository at this point in the history
Simplified and clarified example
  • Loading branch information
marisks committed May 12, 2015
2 parents 5842fb6 + 4c21e6d commit b90a735
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 71 deletions.
7 changes: 3 additions & 4 deletions src/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Controls:MetroWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ViewModels;assembly=gasby.Wpf"
xmlns:model="clr-namespace:ViewModels;assembly=gasby.Wpf"
xmlns:local="clr-namespace:Views;assembly=gasby.Wpf"
xmlns:fsx="clr-namespace:FsXaml;assembly=FsXaml.Wpf"
xmlns:fsxaml="http://github.com/fsprojects/FsXaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Expand Down Expand Up @@ -32,7 +31,7 @@
<Border Background="#FF024D70" Grid.Column="0">
<Grid>
<Viewbox>
<Label Content="{Binding ScoreA}" FontFamily="Lucida Console"></Label>
<Label Content="{Binding Score.ScoreA}" ContentStringFormat="D2" FontFamily="Lucida Console"></Label>
</Viewbox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding DecA}" Height="70" Width="70" VerticalAlignment="Bottom" Opacity="0.5" Margin="20">
Expand All @@ -47,7 +46,7 @@
<Border Background="#FF7E0E03" Grid.Column="1">
<Grid>
<Viewbox>
<Label Content="{Binding ScoreB}" FontFamily="Lucida Console"></Label>
<Label Content="{Binding Score.ScoreB}" ContentStringFormat="D2" FontFamily="Lucida Console"></Label>
</Viewbox>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Command="{Binding DecB}" Height="70" Width="70" VerticalAlignment="Bottom" Opacity="0.5" Margin="20">
Expand Down
64 changes: 3 additions & 61 deletions src/MainWindow.xaml.fs
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)
16 changes: 16 additions & 0 deletions src/Model.fs
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
23 changes: 23 additions & 0 deletions src/ViewModel.fs
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
15 changes: 9 additions & 6 deletions src/gasby.Wpf.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<OutputType>WinExe</OutputType>
<RootNamespace>gasby.Wpf</RootNamespace>
<AssemblyName>gasby.Wpf</AssemblyName>
<targetframeworkversion>v4.5</targetframeworkversion>
<RestorePackages>true</RestorePackages>
<Targetframeworkversion>v4.5</Targetframeworkversion>
<TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
<Name>gasby.Wpf</Name>
</PropertyGroup>
Expand All @@ -35,6 +36,8 @@
<DocumentationFile>bin\Release\gasby.Wpf.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="Model.fs" />
<Compile Include="ViewModel.fs" />
<Resource Include="MainWindow.xaml" />
<Compile Include="MainWindow.xaml.fs" />
<Resource Include="App.xaml" />
Expand All @@ -45,19 +48,19 @@
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="FSharp.ViewModule.Core.Wpf">
<HintPath>..\packages\FSharp.ViewModule.Core.0.9.9.1\lib\net45\FSharp.ViewModule.Core.Wpf.dll</HintPath>
<HintPath>packages\FSharp.ViewModule.Core.0.9.9.1\lib\net45\FSharp.ViewModule.Core.Wpf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FsXaml.Wpf">
<HintPath>..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.dll</HintPath>
<HintPath>packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FsXaml.Wpf.TypeProvider">
<HintPath>..\packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.TypeProvider.dll</HintPath>
<HintPath>packages\FsXaml.Wpf.0.9.9\lib\net45\FsXaml.Wpf.TypeProvider.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MahApps.Metro">
<HintPath>..\packages\MahApps.Metro.1.0.0.0\lib\net45\MahApps.Metro.dll</HintPath>
<HintPath>packages\MahApps.Metro.1.0.0.0\lib\net45\MahApps.Metro.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="mscorlib" />
Expand Down Expand Up @@ -89,7 +92,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System.Windows.Interactivity">
<HintPath>..\packages\MahApps.Metro.1.0.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
<HintPath>packages\MahApps.Metro.1.0.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xaml" />
Expand Down

0 comments on commit b90a735

Please sign in to comment.