private void OnCancel() { if (AddMode) { EventCollection.Remove(SelectedEvent); SelectedEvent = null; AddMode = false; } else if (EditMode) { SelectedEvent.CancelEdit(); EditMode = false; } else if (RemoveMode) { foreach (var item in EventCollection) { if (item.IsChecked) { item.IsChecked = false; } } RemoveMode = false; } CanUseDatagrid = true; CanEditFields = false; }
public void Apply(ViewPacket packet) { Type entityType = null; if (!string.IsNullOrEmpty(packet.EntityType)) { foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { entityType = assembly.GetType(packet.EntityType); if (entityType != null) { break; } } } switch (packet.PacketType) { case ViewPacket.ViewPacketType.CreateEntity: var clonedEntity = (Entity)packet.Data.ToObject(entityType); clonedEntity.Id = packet.Entity; Entities.Add(clonedEntity.Id, clonedEntity); break; case ViewPacket.ViewPacketType.DestroyEntity: Entities.Remove(packet.Entity); break; case ViewPacket.ViewPacketType.SetFieldValue: var targetEntity = Entities[packet.Entity]; string[] path = packet.FieldPath.Split('.'); var targetedEventWrapper = targetEntity.SyncedObjects.Where(s => s.Key == path[0]).First().Value; for (int i = 1; i < path.Length; i++) { if (targetedEventWrapper is IEventCollection <string, ISyncField> collection) { targetedEventWrapper = collection[path[i]]; } else { throw new InvalidOperationException("Something went wrong!"); } } if (targetedEventWrapper is IEventField field) { object newValue = packet.Data.ToObject(entityType); field.SetValue(newValue); } break; } }
private async void OnSave() { if (AddMode) { var _event = Converter.ConvertFromEventModel(SelectedEvent); int id = 0; bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.InsertWithResult(_event); if (result.Status.Success) { id = result.Data.ID; } else { success = false; error = result.Status.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { SelectedEvent.Id = id; OnInformationRequested("Data added successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { EventCollection.Remove(SelectedEvent); SelectedEvent = null; OnErrorOccured(error); } AddMode = false; } else if (EditMode) { var _event = Converter.ConvertFromEventModel(SelectedEvent); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.Update(_event); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { SelectedEvent.EndEdit(); OnInformationRequested("Data updated successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { SelectedEvent.CancelEdit(); OnErrorOccured(error); } EditMode = false; } else if (RemoveMode) { var temp = from item in EventCollection where item.IsChecked select item; var eventsToRemove = temp.Select(x => Converter.ConvertFromEventModel(x)); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.Delete(eventsToRemove); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { //to avoid Invalid Operation Exception for (int i = 0; i < EventCollection.Count; i++) { if (EventCollection[i].IsChecked) { EventCollection.RemoveAt(i); i--; } } OnInformationRequested("Data removed successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { OnErrorOccured(error); } RemoveMode = false; } CanUseDatagrid = true; CanEditFields = false; }