ReadUsersViewModel.cs (4661B)
1 using DynamicData.Binding; 2 3 using Microsoft.EntityFrameworkCore; 4 5 using ReactiveUI; 6 7 using StarSimLib.Contexts; 8 using StarSimLib.Models; 9 10 namespace StarSimGui.ViewModels.Database_ViewModels 11 { 12 /// <summary> 13 /// Represents the read users view. 14 /// </summary> 15 public class ReadUsersViewModel : 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="PublishedSystems"/> property. 24 /// </summary> 25 private IObservableCollection<PublishedSystem> publishedSystemsObservable; 26 27 /// <summary> 28 /// Backing field for the <see cref="Users"/> property. 29 /// </summary> 30 private IObservableCollection<User> usersObservable; 31 32 /// <summary> 33 /// Backing field for the <see cref="SelectedPublishedSystem"/> property. 34 /// </summary> 35 public PublishedSystem selectedPublishedSystem; 36 37 /// <summary> 38 /// Backing field for the <see cref="SelectedUser"/> property. 39 /// </summary> 40 public User selectedUser; 41 42 /// <summary> 43 /// Initialises a new instance of the <see cref="ReadUsersViewModel"/> class. 44 /// </summary> 45 public ReadUsersViewModel() 46 { 47 publishedSystemsObservable = new ObservableCollectionExtended<PublishedSystem>(); 48 49 usersObservable = new ObservableCollectionExtended<User>(); 50 } 51 52 /// <summary> 53 /// Initialises a new instance of the <see cref="ReadUsersViewModel"/> class. 54 /// </summary> 55 /// <param name="context">The <see cref="SimulatorContext"/> instance in which program data is stored.</param> 56 public ReadUsersViewModel(in SimulatorContext context) : this() 57 { 58 dbContext = context; 59 60 PublishedSystems.Load(dbContext.PublishedSystems.Include(system => system.System)); 61 62 Users.Load(dbContext.Users.Include(user => user.PublishedSystems).ThenInclude(system => system.System)); 63 } 64 65 /// <summary> 66 /// Whether the currently selected published system is null. 67 /// </summary> 68 public bool IsSelectedPublishedSystemNull 69 { 70 get { return SelectedPublishedSystem == null; } 71 } 72 73 /// <summary> 74 /// Whether the currently selected user is null. 75 /// </summary> 76 public bool IsSelectedUserNull 77 { 78 get { return SelectedUser == null; } 79 } 80 81 /// <summary> 82 /// The published systems held in the database. 83 /// </summary> 84 public IObservableCollection<PublishedSystem> PublishedSystems 85 { 86 get 87 { 88 return publishedSystemsObservable; 89 } 90 set 91 { 92 publishedSystemsObservable = value; 93 this.RaisePropertyChanged(); 94 } 95 } 96 97 /// <summary> 98 /// The currently selected published system from the <see cref="PublishedSystems"/> collection. 99 /// </summary> 100 public PublishedSystem SelectedPublishedSystem 101 { 102 get 103 { 104 return selectedPublishedSystem; 105 } 106 set 107 { 108 selectedPublishedSystem = value; 109 this.RaisePropertyChanged(); 110 111 this.RaisePropertyChanged(nameof(IsSelectedPublishedSystemNull)); 112 } 113 } 114 115 /// <summary> 116 /// The currently selected user from the <see cref="Users"/> collection. 117 /// </summary> 118 public User SelectedUser 119 { 120 get 121 { 122 return selectedUser; 123 } 124 set 125 { 126 selectedUser = value; 127 this.RaisePropertyChanged(); 128 129 this.RaisePropertyChanged(nameof(IsSelectedUserNull)); 130 } 131 } 132 133 /// <summary> 134 /// The users held in the database. 135 /// </summary> 136 public IObservableCollection<User> Users 137 { 138 get 139 { 140 return usersObservable; 141 } 142 set 143 { 144 usersObservable = value; 145 this.RaisePropertyChanged(); 146 } 147 } 148 149 /// <summary> 150 /// Refreshed the database sources for this view model. 151 /// </summary> 152 internal void HandleDatabaseRefresh() 153 { 154 PublishedSystems.Load(dbContext.PublishedSystems); 155 Users.Load(dbContext.Users); 156 } 157 } 158 }