Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active August 29, 2015 14:26
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 BrianJVarley/24e37f7be0029e27cbf8 to your computer and use it in GitHub Desktop.
Save BrianJVarley/24e37f7be0029e27cbf8 to your computer and use it in GitHub Desktop.
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using LC_Points.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;
using System.Linq;
using Windows.UI.Popups;
using LC_Points.Services;
namespace LC_Points.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
private readonly IRepository<ScoreModel> _repository = App.ScoresRepository;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
//call methods to initilise list data
InitSubjectTypes();
InitOrdinaryGradePairs();
InitHigherGradePairs();
}
public List<ScoreModel> Subjects { get; set; }
public List<StringKeyValue> HigherGradePointKV { get; set; }
public List<StringKeyValue> OrdinaryGradePointKV { get; set; }
public List<StringKeyValue> HigherMathGradePointKV { get; set; }
private ScoreModel _selectedSubject;
public ScoreModel SelectedSubject
{
get { return _selectedSubject; }
set
{
if (value != _selectedSubject)
{
_selectedSubject = value;
RaisePropertyChanged("SelectedSubject");
RaisePropertyChanged("ButtonEnabled");
}
}
}
private StringKeyValue _selectedHigherGrade;
public StringKeyValue SelectedHigherGrade
{
get { return _selectedHigherGrade; }
set
{
if (value != _selectedHigherGrade)
{
_selectedHigherGrade = value;
RaisePropertyChanged("SelectedHigherGrade");
RaisePropertyChanged("ButtonEnabled");
}
}
}
private StringKeyValue _selectedHigherMathGrade;
public StringKeyValue SelectedHigherMathGrade
{
get { return _selectedHigherMathGrade; }
set
{
if (value != _selectedHigherMathGrade)
{
_selectedHigherMathGrade = value;
RaisePropertyChanged("SelectedHigherMathGrade");
RaisePropertyChanged("ButtonEnabled");
}
}
}
private StringKeyValue _selectedOrdinaryGrade;
public StringKeyValue SelectedOrdinaryGrade
{
get { return _selectedOrdinaryGrade; }
set
{
if (value != _selectedOrdinaryGrade)
{
_selectedOrdinaryGrade = value;
RaisePropertyChanged("SelectedOrdinaryGrade");
RaisePropertyChanged("ButtonEnabled");
}
}
}
private int _totalPoints;
public int TotalPoints
{
get { return _totalPoints; }
set
{
if (value != _totalPoints)
{
_totalPoints = value;
RaisePropertyChanged("TotalPoints");
}
}
}
//button enabled bool
public bool ButtonEnabled
{
get
{
return SelectedSubject != null && (SelectedOrdinaryGrade != null || SelectedHigherGrade != null || SelectedHigherMathGrade != null);
}
}
//Higher math toggle button bool
private bool _isHigherMath;
public bool IsHigherMath
{
get
{
return _isHigherMath;
}
set
{
_isHigherMath = value;
RaisePropertyChanged("IsHigherMath");
RaisePropertyChanged("IsEitherHigherVisible");
}
}
//Higher grades toggle button bool
private bool _isEitherHigherVisible;
public bool IsEitherHigherVisible
{
get
{
return _isHigherMath || _isHigher ;
}
}
//Higher toggle button bool property
private bool _isHigher;
public bool IsHigher
{
get
{
return _isHigher;
}
set
{
_isHigher = value;
RaisePropertyChanged("IsHigher");
RaisePropertyChanged("IsEitherHigherVisible");
}
}
//Method to store Subject and Grade from Combo Boxes
public void AddSubjectAndGrade()
{
string SelectedSubjectName = "null subject";
string HigherMaths = "Mathematics";
int SelectedPoints = 0;
SelectedSubjectName = SelectedSubject.Subject;
if(IsHigher && SelectedSubjectName.Equals(HigherMaths))
{
SelectedPoints = SelectedHigherMathGrade.Value;
}
else
{
SelectedPoints = IsHigher ? SelectedHigherGrade.Value : SelectedOrdinaryGrade.Value;
}
if (_repository.Count <= 5)
{
_repository.Add(new ScoreModel() {Subject = SelectedSubjectName, Points = SelectedPoints});
CalculateLeavingCertPoints();
}
}
//Method to calculate the points result
private void CalculateLeavingCertPoints()
{
//Logic:
//IF 6 subjects and grades
//add 6 grade points
//output result of addition
TotalPoints = _repository.Collection.Sum(x => x.Points);
}
RelayCommand viewGradesCommand;
public RelayCommand ViewGradesCommand
{
get
{
if (viewGradesCommand == null)
{
viewGradesCommand = new RelayCommand(() =>
{
//do something...
});
}
return viewGradesCommand;
}
}
RelayCommand addGradeCommand;
public RelayCommand AddGradeCommand
{
get
{
if (addGradeCommand == null)
{
addGradeCommand = new RelayCommand(() =>
{
AddSubjectAndGrade();
});
}
return addGradeCommand;
}
}
RelayCommand clearGradesCommand;
public RelayCommand ClearGradesCommand
{
get
{
if (clearGradesCommand == null)
{
clearGradesCommand = new RelayCommand(() =>
{
//call to empty collection items
_repository.Clear();
TotalPoints = 0;
});
}
return clearGradesCommand;
}
}
public class StringKeyValue
{
public string Key { get; set; }
public int Value { get; set; }
}
public void InitOrdinaryGradePairs()
{
List<StringKeyValue> ordinaryGradePointKVTemp = new List<StringKeyValue>();
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 60 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 50 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 45 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 40 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 35 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C1", Value = 30 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C2", Value = 25 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 20 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "D1", Value = 15 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "D2", Value = 10 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "D3", Value = 5 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "E,F,NG", Value = 0 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "Pass", Value = 30 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "Merit", Value = 50 });
ordinaryGradePointKVTemp.Add(new StringKeyValue { Key = "Distinction", Value = 70 });
OrdinaryGradePointKV = ordinaryGradePointKVTemp;
}
public void InitHigherGradePairs()
{
List<StringKeyValue> higherGradePointKVTemp = new List<StringKeyValue>();
higherGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 100 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 90 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 85 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 80 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 75 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C1 Higher Grades", Value = 70 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C2", Value = 65 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 60 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D1", Value = 55 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D2", Value = 50 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "D3", Value = 45 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "E,F,NG", Value = 0 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Pass", Value = 30 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Merit", Value = 50 });
higherGradePointKVTemp.Add(new StringKeyValue { Key = "Distinction", Value = 70 });
HigherGradePointKV = higherGradePointKVTemp;
}
public void InitHigherMathGradePairs()
{
List<StringKeyValue> higherMathGradePointKVTemp = new List<StringKeyValue>();
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "A1", Value = 125 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "A2", Value = 115 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "B1", Value = 110 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "B2", Value = 105 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "B3", Value = 100 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "C1", Value = 95 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "C2 Higher Maths", Value = 90 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "C3", Value = 85 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "D1", Value = 80 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "D2", Value = 75 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "D3", Value = 70 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "E,F,NG", Value = 0 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "Pass", Value = 30 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "Merit", Value = 50 });
higherMathGradePointKVTemp.Add(new StringKeyValue { Key = "Distinction", Value = 70 });
HigherMathGradePointKV = higherMathGradePointKVTemp;
}
public void InitSubjectTypes()
{
List<ScoreModel> subjectList = new List<ScoreModel>();
// Adding Subjects to List
subjectList.Add(new ScoreModel { Subject = "Accounting" });
subjectList.Add(new ScoreModel { Subject = "Agricultural Economics" });
subjectList.Add(new ScoreModel { Subject = "Agricultural Science" });
subjectList.Add(new ScoreModel { Subject = "Ancient Greek" });
subjectList.Add(new ScoreModel { Subject = "Applied Math" });
subjectList.Add(new ScoreModel { Subject = "Arabic" });
subjectList.Add(new ScoreModel { Subject = "Art" });
subjectList.Add(new ScoreModel { Subject = "Artistic & Creative Group" });
subjectList.Add(new ScoreModel { Subject = "Biology" });
subjectList.Add(new ScoreModel { Subject = "Business" });
subjectList.Add(new ScoreModel { Subject = "Business Group" });
subjectList.Add(new ScoreModel { Subject = "Chemistry" });
subjectList.Add(new ScoreModel { Subject = "Classical Studies" });
subjectList.Add(new ScoreModel { Subject = "Construction Studies" });
subjectList.Add(new ScoreModel { Subject = "Design & Comm Graphics" });
subjectList.Add(new ScoreModel { Subject = "Economics" });
subjectList.Add(new ScoreModel { Subject = "Engineering" });
subjectList.Add(new ScoreModel { Subject = "English" });
subjectList.Add(new ScoreModel { Subject = "French" });
subjectList.Add(new ScoreModel { Subject = "Geography" });
subjectList.Add(new ScoreModel { Subject = "German" });
subjectList.Add(new ScoreModel { Subject = "Hebrew Studies" });
subjectList.Add(new ScoreModel { Subject = "History" });
subjectList.Add(new ScoreModel { Subject = "Home Economics" });
subjectList.Add(new ScoreModel { Subject = "Irish" });
subjectList.Add(new ScoreModel { Subject = "Italian" });
subjectList.Add(new ScoreModel { Subject = "Japanese" });
subjectList.Add(new ScoreModel { Subject = "Languages & Humanities" });
subjectList.Add(new ScoreModel { Subject = "Latin" });
subjectList.Add(new ScoreModel { Subject = "Link Modules" });
subjectList.Add(new ScoreModel { Subject = "Mathematics" });
subjectList.Add(new ScoreModel { Subject = "Music" });
subjectList.Add(new ScoreModel { Subject = "Other Language" });
subjectList.Add(new ScoreModel { Subject = "Physics" });
subjectList.Add(new ScoreModel { Subject = "Physics & Chemistry" });
subjectList.Add(new ScoreModel { Subject = "Practical Group" });
subjectList.Add(new ScoreModel { Subject = "Religious Education" });
subjectList.Add(new ScoreModel { Subject = "Russian" });
subjectList.Add(new ScoreModel { Subject = "Science Group" });
subjectList.Add(new ScoreModel { Subject = "Social Group" });
subjectList.Add(new ScoreModel { Subject = "Spanish" });
subjectList.Add(new ScoreModel { Subject = "Technology" });
Subjects = subjectList;
}
}
}
<Page x:Class="LC_Points.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:Series="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
xmlns:charting="using:WinRTXamlToolkit.Controls.DataVisualization.Charting"
xmlns:controls="using:WinRTXamlToolkit.Controls"
xmlns:converter="using:LC_Points.Converter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:datavis="using:WinRTXamlToolkit.Controls.DataVisualization"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="using:LC_Points"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Page.Resources>
<converter:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
<converter:BoolToNonVisibilityConverter x:Key="BoolToNonVisibilityConverter" />
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40*" />
<RowDefinition Height="20*" />
<RowDefinition Height="30*" />
<RowDefinition Height="30*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="3*" />
</Grid.ColumnDefinitions>
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Style="{StaticResource SubheaderTextBlockStyle}" Text="LC POINTS" />
<TextBlock Margin="9,-7,0,0"
Foreground="DarkGreen"
Style="{StaticResource HeaderTextBlockStyle}"
Text="Home" />
</StackPanel>
<!-- TitlePanel contains the name of the application and page title -->
<ComboBox x:Name="subjectCmbBx"
Grid.Row="1"
Grid.ColumnSpan="2"
Width="199"
HorizontalAlignment="Left"
VerticalAlignment="Top"
DisplayMemberPath="Subject"
Header="Subjects"
ItemsSource="{Binding Subjects}"
PlaceholderText="Pick a subject"
SelectedItem="{Binding SelectedSubject,
Mode=TwoWay}" />
<ComboBox x:Name="ordinaryGradeCmbBx"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="170"
HorizontalAlignment="Right"
DisplayMemberPath="Key"
Header="Grades"
ItemsSource="{Binding OrdinaryGradePointKV}"
PlaceholderText="Pick a grade"
SelectedValue="{Binding SelectedOrdinaryGrade,
Mode=TwoWay}"
Visibility="{Binding IsEitherHigherVisible,
Mode=TwoWay,
Converter={StaticResource BoolToNonVisibilityConverter}}" />
<ComboBox x:Name="higherGradeCmbBx"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="170"
HorizontalAlignment="Right"
DisplayMemberPath="Key"
Header="Grades"
ItemsSource="{Binding HigherGradePointKV}"
PlaceholderText="Pick a grade"
SelectedValue="{Binding SelectedHigherGrade,
Mode=TwoWay}"
Visibility="{Binding IsHigher,
Mode=TwoWay,
Converter={StaticResource BoolToVisibilityConverter}}" />
<ComboBox x:Name="higherMathGradeCmbBx"
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Width="170"
HorizontalAlignment="Right"
DisplayMemberPath="Key"
Header="Grades"
ItemsSource="{Binding HigherMathGradePointKV}"
PlaceholderText="Pick a grade"
SelectedValue="{Binding SelectedHigherMathGrade,
Mode=TwoWay}"
Visibility="{Binding IsHigherMath,
Mode=TwoWay,
Converter={StaticResource BoolToVisibilityConverter}}" />
<Button x:Name="addGradeBtn"
Grid.Row="2"
HorizontalAlignment="Left"
Command="{Binding Path=AddGradeCommand}"
Content="Add Grade"
IsEnabled="{Binding ButtonEnabled,
Mode=TwoWay}" />
<ToggleButton x:Name="higherTglBtn"
Grid.Row="2"
Grid.ColumnSpan="2"
Margin="0,26.5,114,25.667"
HorizontalAlignment="Right"
Content="Higher"
IsChecked="{Binding IsHigher,
Mode=TwoWay}" />
<ToggleButton x:Name="higherMathTglBtn"
Grid.Row="2"
Grid.ColumnSpan="2"
HorizontalAlignment="Right"
Content="H/Math"
IsChecked="{Binding IsHigherMath,
Mode=TwoWay}" />
<Border Grid.Row="3"
Grid.RowSpan="3"
Grid.ColumnSpan="2"
Width="400"
Margin="0"
Background="Black">
<controls:Gauge Grid.Row="3"
Grid.RowSpan="3"
Grid.ColumnSpan="2"
Width="400"
Maximum="600"
Minimum="0"
NeedleBrush="Transparent"
ScaleBrush="DarkSlateGray"
ScaleTickBrush="Black"
TickBrush="Transparent"
TrailBrush="DarkSeaGreen"
Unit="Leaving Cert Points"
UnitBrush="DarkGray"
ValueBrush="DarkSeaGreen"
ValueStringFormat="N1"
Value="{Binding TotalPoints,
Mode=TwoWay}" />
</Border>
</Grid>
<Page.BottomAppBar>
<CommandBar x:Name="appBar" IsSticky="True">
<CommandBar.PrimaryCommands>
<AppBarButton x:Name="ClearAppBarButton"
Command="{Binding Path=ClearGradesCommand}"
Icon="Delete"
IsCompact="False"
Label="Clear All" />
<AppBarButton x:Name="ViewListAppBarButton"
Icon="ViewAll"
IsCompact="False"
IsEnabled="{Binding ButtonEnabled,
Mode=TwoWay}"
Label="View">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:NavigateToPageAction TargetPage="LC_Points.View.ViewSubjectGradePage" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</AppBarButton>
<AppBarButton x:Name="AboutAppBarButton"
Icon="Help"
IsCompact="False"
Label="About">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:NavigateToPageAction TargetPage="LC_Points.View.AboutPage" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</AppBarButton>
</CommandBar.PrimaryCommands>
<CommandBar.SecondaryCommands>
<AppBarButton Label="Share score!">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click">
<Core:NavigateToPageAction TargetPage="LC_Points.View.LoginPage" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</AppBarButton>
</CommandBar.SecondaryCommands>
</CommandBar>
</Page.BottomAppBar>
</Page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment