StarSim

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

MainWindowViewModel.cs (4223B)


      1 using ReactiveUI;
      2 
      3 using StarSimLib.Configuration;
      4 using StarSimLib.Contexts;
      5 using StarSimLib.Models;
      6 
      7 using System;
      8 
      9 namespace StarSimGui.ViewModels
     10 {
     11     /// <summary>
     12     /// Represents the main view.
     13     /// </summary>
     14     public class MainWindowViewModel : ViewModelBase
     15     {
     16         /// <summary>
     17         /// The database context in which the program should store data and which it should query to fetch data.
     18         /// </summary>
     19         private readonly SimulatorContext simulatorContext;
     20 
     21         /// <summary>
     22         /// Backing field for the <see cref="CurrentUser"/> property.
     23         /// </summary>
     24         private User currentUser;
     25 
     26         /// <summary>
     27         /// Initialises a new instance of the <see cref="MainWindowViewModel"/> class.
     28         /// </summary>
     29         public MainWindowViewModel()
     30         {
     31             simulatorContext = new SimulatorContext();
     32 
     33             simulatorContext.ChangeTracker.AutoDetectChangesEnabled = true;
     34 
     35             OverviewViewModel = new OverviewViewModel(in simulatorContext);
     36 
     37             UserLoginViewModel = new UserLoginViewModel(in simulatorContext);
     38 
     39             UserLoginViewModel.LoggedIn += user => CurrentUser = user;
     40             UserLoginViewModel.LoggedIn += OverviewViewModel.HandleLogin;
     41 
     42             UserLoginViewModel.LoggedOut += () => CurrentUser = null;
     43             UserLoginViewModel.LoggedOut += OverviewViewModel.HandleLogout;
     44         }
     45 
     46         /// <summary>
     47         /// Initialises a new instance of the <see cref="MainWindowViewModel"/> class.
     48         /// </summary>
     49         /// <param name="configuration">The current configuration of the application.</param>
     50         public MainWindowViewModel(Config configuration) : this()
     51         {
     52             DatabaseViewModel = new DatabaseViewModel(in simulatorContext, in configuration);
     53 
     54             SimulationViewModel = new SimulationViewModel(in simulatorContext, in configuration);
     55 
     56             UserLoginViewModel.LoggedIn += DatabaseViewModel.HandleLogin;
     57             UserLoginViewModel.LoggedIn += SimulationViewModel.HandleLogin;
     58 
     59             UserLoginViewModel.LoggedOut += DatabaseViewModel.HandleLogout;
     60             UserLoginViewModel.LoggedOut += SimulationViewModel.HandleLogout;
     61 
     62             SimulationViewModel.DatabaseUpdated += HandleDatabaseUpdated;
     63             DatabaseViewModel.DatabaseUpdated += HandleDatabaseUpdated;
     64 
     65 #if DEBUG
     66             // simulate a login to accelerate development of the application, as the implemented cryptographic features
     67             // of the login system make it slow to solve for the password hash. furthermore loading the database takes
     68             // a relatively long time as well - this wastes development time
     69             UserLoginViewModel.DebugSimulateLogin(new User(0, "Debug User", UserPrivileges.Admin, "debug@application.net"));
     70 #endif
     71         }
     72 
     73         /// <summary>
     74         /// The currently logged in user.
     75         /// </summary>
     76         public User CurrentUser
     77         {
     78             get
     79             {
     80                 return currentUser;
     81             }
     82             set
     83             {
     84                 currentUser = value;
     85                 this.RaisePropertyChanged();
     86             }
     87         }
     88 
     89         /// <summary>
     90         /// Represents the database view.
     91         /// </summary>
     92         public DatabaseViewModel DatabaseViewModel { get; set; }
     93 
     94         /// <summary>
     95         /// Represents the overview.
     96         /// </summary>
     97         public OverviewViewModel OverviewViewModel { get; set; }
     98 
     99         /// <summary>
    100         /// Represents the simulation view.
    101         /// </summary>
    102         public SimulationViewModel SimulationViewModel { get; set; }
    103 
    104         /// <summary>
    105         /// Represents the user login view.
    106         /// </summary>
    107         public UserLoginViewModel UserLoginViewModel { get; set; }
    108 
    109         /// <summary>
    110         /// Handles the DatabaseEdited event.
    111         /// </summary>
    112         private void HandleDatabaseUpdated()
    113         {
    114             Console.WriteLine($"Database has tracked changes?: {simulatorContext.ChangeTracker.HasChanges()}");
    115             Console.WriteLine($"Saved {simulatorContext.SaveChanges()} changed entities");
    116             DatabaseViewModel.RefreshDBSources();
    117         }
    118     }
    119 }