StarSim

StarSim.git
git clone git://git.lenczewski.org/StarSim.git
Log | Files | Refs | README | LICENSE

CreateSystemsViewModel.cs (4900B)


      1 using ReactiveUI;
      2 
      3 using StarSimLib.Contexts;
      4 using StarSimLib.Models;
      5 
      6 using System;
      7 using System.Linq;
      8 using System.Windows.Input;
      9 
     10 namespace StarSimGui.ViewModels.Database_ViewModels
     11 {
     12     /// <summary>
     13     /// Represents the create systems view.
     14     /// </summary>
     15     public class CreateSystemsViewModel : ViewModelBase
     16     {
     17         /// <summary>
     18         /// The program database.
     19         /// </summary>
     20         private readonly SimulatorContext dbContext;
     21 
     22         /// <summary>
     23         /// Backing field for the <see cref="BodyMass"/> property.
     24         /// </summary>
     25         private double bodyMass;
     26 
     27         /// <summary>
     28         /// Backing field for the <see cref="BodyName"/> property.
     29         /// </summary>
     30         private string bodyName;
     31 
     32         /// <summary>
     33         /// Initialises a new instance of the <see cref="CreateSystemsViewModel"/> class.
     34         /// </summary>
     35         public CreateSystemsViewModel()
     36         {
     37             IObservable<bool> canCreate = this.WhenAnyValue(x => x.BodyName, x => x.BodyMass,
     38                 (name, mass) => !string.IsNullOrEmpty(name) && !string.IsNullOrWhiteSpace(name) && mass > 0);
     39 
     40             CreateBodyCommand = ReactiveCommand.Create(CreateBodyCommandImpl, canCreate);
     41 
     42             ResetBodyCommand = ReactiveCommand.Create(ResetBodyCommandImpl);
     43         }
     44 
     45         /// <summary>
     46         /// Initialises a new instance of the <see cref="CreateSystemsViewModel"/> class.
     47         /// </summary>
     48         /// <param name="context">The <see cref="SimulatorContext"/> instance in which program data is stored.</param>
     49         public CreateSystemsViewModel(in SimulatorContext context) : this()
     50         {
     51             dbContext = context;
     52         }
     53 
     54         /// <summary>
     55         /// Signifies that the database should be updated.
     56         /// </summary>
     57         public event Action DatabaseEdited;
     58 
     59         /// <summary>
     60         /// The mass of the body to create.
     61         /// </summary>
     62         public double BodyMass
     63         {
     64             get
     65             {
     66                 return bodyMass;
     67             }
     68             set
     69             {
     70                 bodyMass = value;
     71                 this.RaisePropertyChanged();
     72             }
     73         }
     74 
     75         /// <summary>
     76         /// The name of the body to create.
     77         /// </summary>
     78         public string BodyName
     79         {
     80             get
     81             {
     82                 return bodyName;
     83             }
     84             set
     85             {
     86                 bodyName = value;
     87                 this.RaisePropertyChanged();
     88             }
     89         }
     90 
     91         /// <summary>
     92         /// Command invoked whenever the user wants to add the currently edited body to the database.
     93         /// </summary>
     94         public ICommand CreateBodyCommand { get; }
     95 
     96         /// <summary>
     97         /// Provides feedback about the changes made to the database.
     98         /// </summary>
     99         public string Feedback { get; private set; }
    100 
    101         /// <summary>
    102         /// Command invoked whenever the user wants to reset the currently edited body.
    103         /// </summary>
    104         public ICommand ResetBodyCommand { get; }
    105 
    106         /// <summary>
    107         /// Invoked whenever the user wants to add the currently edited body to the database.
    108         /// </summary>
    109         private void CreateBodyCommandImpl()
    110         {
    111             if (dbContext.Bodies.Any(body => body.Name.Equals(BodyName)))
    112             {
    113                 Feedback = "Body with the same name already exists in the database.";
    114                 this.RaisePropertyChanged(nameof(Feedback));
    115             }
    116 
    117             try
    118             {
    119                 Body lastBody = dbContext.Bodies.OrderBy(body => body.Id).Last();
    120 
    121                 Body newBody = new Body(lastBody.Id + 1, BodyName, BodyMass);
    122 
    123                 dbContext.Bodies.Add(newBody);
    124 
    125                 Feedback = "Body was successfully added to the database.";
    126                 this.RaisePropertyChanged(nameof(Feedback));
    127 
    128                 OnDatabaseEdited();
    129             }
    130             catch (Exception)
    131             {
    132                 Feedback = "Body could not be added to the database.";
    133                 this.RaisePropertyChanged(nameof(Feedback));
    134             }
    135         }
    136 
    137         /// <summary>
    138         /// Invokes the <see cref="DatabaseEdited"/> event.
    139         /// </summary>
    140         private void OnDatabaseEdited()
    141         {
    142             DatabaseEdited?.Invoke();
    143         }
    144 
    145         /// <summary>
    146         /// Invoked whenever the user wants to reset the currently edited body.
    147         /// </summary>
    148         private void ResetBodyCommandImpl()
    149         {
    150             BodyName = string.Empty;
    151             BodyMass = 0;
    152             Feedback = string.Empty;
    153             this.RaisePropertyChanged(nameof(Feedback));
    154         }
    155 
    156         /// <summary>
    157         /// Refreshed the database sources for this view model.
    158         /// </summary>
    159         internal void HandleDatabaseRefresh()
    160         {
    161         }
    162     }
    163 }