Skip to content

Instantly share code, notes, and snippets.

@GeertVL-zz
Created November 6, 2015 09:29
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 GeertVL-zz/88c30a60084f632de2f7 to your computer and use it in GitHub Desktop.
Save GeertVL-zz/88c30a60084f632de2f7 to your computer and use it in GitHub Desktop.
public class TipCalculatorViewModel : INotifyPropertyChanged
{
private readonly TipCalculation _model;
private readonly ITipCalculatorRepository _repository;
public event PropertyChangedEventHandler PropertyChanged;
public TipCalculatorViewModel(ITipCalculatorRepository repository)
{
_model = new TipCalculation();
_repository = repository;
SaveCalculationCommand = new RelayCommand<TipCalculation>(SaveCalculation);
}
public decimal BillAmount
{
get { return _model.BillAmount; }
set
{
_model.BillAmount = value;
OnPropertyChanged(nameof(BillAmount));
}
}
public decimal TipRate
{
get { return _model.TipRate; }
set
{
_model.TipRate = value;
OnPropertyChanged(nameof(TipRate));
}
}
public decimal Tip => Math.Round(BillAmount * (TipRate / 100), 2);
public decimal Total => BillAmount + TipRate;
public ICommand SaveCalculationCommand { get; private set; }
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SaveCalculation(TipCalculation model)
{
model.Tip = Tip;
model.Total = Total;
//Save it
_repository.Save(model);
}
}
public class TipCalculatorViewModel : INotifyPropertyChanged
{
private readonly TipCalculation _model;
private readonly ITipCalculatorRepository _repository;
public event PropertyChangedEventHandler PropertyChanged;
public TipCalculatorViewModel(TipCalculation model, ITipCalculatorRepository repository)
{
_model = model;
_repository = repository;
SaveCalculationCommand = new RelayCommand<TipCalculation>(SaveCalculation);
}
public decimal BillAmount
{
get { return _model.BillAmount; }
set
{
_model.BillAmount = value;
OnPropertyChanged(nameof(BillAmount));
}
}
public decimal TipRate
{
get { return _model.TipRate; }
set
{
_model.TipRate = value;
OnPropertyChanged(nameof(TipRate));
}
}
public decimal Tip => Math.Round(BillAmount * (TipRate / 100), 2);
public decimal Total => BillAmount + TipRate;
public ICommand SaveCalculationCommand { get; private set; }
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void SaveCalculation(TipCalculation model)
{
model.Tip = Tip;
model.Total = Total;
//Save it
_repository.Save(model);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment