DeleteSystemsViewModel.cs (9628B)
1 using DynamicData.Binding; 2 3 using ReactiveUI; 4 5 using StarSimLib.Contexts; 6 using StarSimLib.Models; 7 8 using System; 9 using System.Windows.Input; 10 11 namespace StarSimGui.ViewModels.Database_ViewModels 12 { 13 /// <summary> 14 /// Represents the delete systems view. 15 /// </summary> 16 public class DeleteSystemsViewModel : ViewModelBase 17 { 18 /// <summary> 19 /// The program database. 20 /// </summary> 21 private readonly SimulatorContext dbContext; 22 23 /// <summary> 24 /// Backing field for the <see cref="Bodies"/> property. 25 /// </summary> 26 private IObservableCollection<Body> bodies; 27 28 /// <summary> 29 /// Backing field for the <see cref="Bodies"/> property. 30 /// </summary> 31 private IObservableCollection<PublishedSystem> publishedSystems; 32 33 /// <summary> 34 /// Backing field for the <see cref="SelectedBody"/> property. 35 /// </summary> 36 private Body selectedBody; 37 38 /// <summary> 39 /// Backing field for the <see cref="SelectedPublishedSystem"/> property. 40 /// </summary> 41 private PublishedSystem selectedPublishedSystem; 42 43 /// <summary> 44 /// Backing field for the <see cref="SelectedSystem"/> property. 45 /// </summary> 46 private StarSimLib.Models.System selectedSystem; 47 48 /// <summary> 49 /// Backing field for the <see cref="Bodies"/> property. 50 /// </summary> 51 private IObservableCollection<StarSimLib.Models.System> systems; 52 53 /// <summary> 54 /// Initialises a new instance of the <see cref="DeleteUsersViewModel"/> class. 55 /// </summary> 56 public DeleteSystemsViewModel() 57 { 58 bodies = new ObservableCollectionExtended<Body>(); 59 60 systems = new ObservableCollectionExtended<StarSimLib.Models.System>(); 61 62 publishedSystems = new ObservableCollectionExtended<PublishedSystem>(); 63 64 IObservable<bool> canRemoveBody = this.WhenAnyValue(x => x.SelectedBody, selector: user => user != null); 65 66 RemoveBodyCommand = ReactiveCommand.Create(RemoveBodyCommandImpl, canRemoveBody); 67 68 IObservable<bool> canRemoveSystem = 69 this.WhenAnyValue(x => x.SelectedSystem, selector: system => system != null); 70 71 RemoveSystemCommand = ReactiveCommand.Create(RemoveSystemCommandImpl, canRemoveSystem); 72 73 IObservable<bool> canRemovePublishedSystem = this.WhenAnyValue(x => x.SelectedPublishedSystem, 74 selector: publishedSystem => publishedSystem != null); 75 76 RemovePublishedSystemCommand = ReactiveCommand.Create(RemovePublishedSystemCommandImpl, canRemovePublishedSystem); 77 } 78 79 /// <summary> 80 /// Initialises a new instance of the <see cref="DeleteUsersViewModel"/> class. 81 /// </summary> 82 /// <param name="context">The <see cref="SimulatorContext"/> instance in which program data is stored.</param> 83 public DeleteSystemsViewModel(in SimulatorContext context) : this() 84 { 85 dbContext = context; 86 87 Bodies.Load(dbContext.Bodies); 88 89 Systems.Load(dbContext.Systems); 90 91 PublishedSystems.Load(dbContext.PublishedSystems); 92 } 93 94 /// <summary> 95 /// Signifies that the database should be updated. 96 /// </summary> 97 public event Action DatabaseEdited; 98 99 /// <summary> 100 /// The users held in the database. 101 /// </summary> 102 public IObservableCollection<Body> Bodies 103 { 104 get 105 { 106 return bodies; 107 } 108 set 109 { 110 bodies = value; 111 this.RaisePropertyChanged(); 112 } 113 } 114 115 /// <summary> 116 /// Provides feedback about the changes made to the body database. 117 /// </summary> 118 public string BodyFeedback { get; private set; } 119 120 /// <summary> 121 /// Provides feedback about the changes made to the published system database. 122 /// </summary> 123 public string PublishedSystemFeedback { get; private set; } 124 125 /// <summary> 126 /// The users held in the database. 127 /// </summary> 128 public IObservableCollection<PublishedSystem> PublishedSystems 129 { 130 get 131 { 132 return publishedSystems; 133 } 134 set 135 { 136 publishedSystems = value; 137 this.RaisePropertyChanged(); 138 } 139 } 140 141 /// <summary> 142 /// Command invoked whenever the user wants to remove the currently selected body from the database. 143 /// </summary> 144 public ICommand RemoveBodyCommand { get; } 145 146 /// <summary> 147 /// Command invoked whenever the user wants to remove the currently selected published system from the database. 148 /// </summary> 149 public ICommand RemovePublishedSystemCommand { get; } 150 151 /// <summary> 152 /// Command invoked whenever the user wants to remove the currently selected system from the database. 153 /// </summary> 154 public ICommand RemoveSystemCommand { get; } 155 156 /// <summary> 157 /// The currently selected body. 158 /// </summary> 159 public Body SelectedBody 160 { 161 get 162 { 163 return selectedBody; 164 } 165 set 166 { 167 selectedBody = value; 168 this.RaisePropertyChanged(); 169 } 170 } 171 172 /// <summary> 173 /// The currently selected published system. 174 /// </summary> 175 public PublishedSystem SelectedPublishedSystem 176 { 177 get 178 { 179 return selectedPublishedSystem; 180 } 181 set 182 { 183 selectedPublishedSystem = value; 184 this.RaisePropertyChanged(); 185 } 186 } 187 188 /// <summary> 189 /// The currently selected system. 190 /// </summary> 191 public StarSimLib.Models.System SelectedSystem 192 { 193 get 194 { 195 return selectedSystem; 196 } 197 set 198 { 199 selectedSystem = value; 200 this.RaisePropertyChanged(); 201 } 202 } 203 204 /// <summary> 205 /// Provides feedback about the changes made to the system database. 206 /// </summary> 207 public string SystemFeedback { get; private set; } 208 209 /// <summary> 210 /// The users held in the database. 211 /// </summary> 212 public IObservableCollection<StarSimLib.Models.System> Systems 213 { 214 get 215 { 216 return systems; 217 } 218 set 219 { 220 systems = value; 221 this.RaisePropertyChanged(); 222 } 223 } 224 225 /// <summary> 226 /// Invokes the <see cref="DatabaseEdited"/> event. 227 /// </summary> 228 private void OnDatabaseEdited() 229 { 230 DatabaseEdited?.Invoke(); 231 } 232 233 /// <summary> 234 /// Invoked whenever the user wants to remove the currently selected body from the database. 235 /// </summary> 236 private void RemoveBodyCommandImpl() 237 { 238 try 239 { 240 dbContext.Bodies.Remove(SelectedBody); 241 242 BodyFeedback = "Body was successfully removed from the database."; 243 this.RaisePropertyChanged(nameof(BodyFeedback)); 244 245 OnDatabaseEdited(); 246 } 247 catch (Exception) 248 { 249 BodyFeedback = "Body could not be removed from the database."; 250 this.RaisePropertyChanged(nameof(BodyFeedback)); 251 } 252 } 253 254 /// <summary> 255 /// Invoked whenever the user wants to remove the currently selected published system from the database. 256 /// </summary> 257 private void RemovePublishedSystemCommandImpl() 258 { 259 try 260 { 261 dbContext.PublishedSystems.Remove(SelectedPublishedSystem); 262 263 PublishedSystemFeedback = "Published system was successfully removed from the database."; 264 this.RaisePropertyChanged(nameof(PublishedSystemFeedback)); 265 266 OnDatabaseEdited(); 267 } 268 catch (Exception) 269 { 270 PublishedSystemFeedback = "Published system could not be removed from the database."; 271 this.RaisePropertyChanged(nameof(PublishedSystemFeedback)); 272 } 273 } 274 275 /// <summary> 276 /// Invoked whenever the user wants to remove the currently selected system from the database. 277 /// </summary> 278 private void RemoveSystemCommandImpl() 279 { 280 try 281 { 282 dbContext.Systems.Remove(SelectedSystem); 283 284 SystemFeedback = "System was successfully removed from the database."; 285 this.RaisePropertyChanged(nameof(SystemFeedback)); 286 287 OnDatabaseEdited(); 288 } 289 catch (Exception) 290 { 291 SystemFeedback = "System could not be removed from the database."; 292 this.RaisePropertyChanged(nameof(SystemFeedback)); 293 } 294 } 295 296 /// <summary> 297 /// Refreshed the database sources for this view model. 298 /// </summary> 299 internal void HandleDatabaseRefresh() 300 { 301 Bodies.Load(dbContext.Bodies); 302 Systems.Load(dbContext.Systems); 303 PublishedSystems.Load(dbContext.PublishedSystems); 304 } 305 } 306 }