using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Core.DI; using Tango.CSV; using Tango.FSE.Common; using Tango.FSE.Common.Notifications; using Tango.FSE.Procedures.CSV; using Tango.FSE.Procedures.Dialogs; namespace Tango.FSE.Procedures.ViewModels { public class ResultsViewVM : FSEViewModel { private List _results; public List Results { get { return _results; } set { _results = value; RaisePropertyChangedAuto(); } } public RelayCommand DisplayResultGridCommand { get; set; } public RelayCommand ExportResultGridCommand { get; set; } public ResultsViewVM() { TangoIOC.Default.Inject(this); Results = new List(); DisplayResultGridCommand = new RelayCommand(DisplayResultGrid); ExportResultGridCommand = new RelayCommand(ExportResultGrid); } private void DisplayResultGrid(Result result) { if (result != null) { ResultGridViewVM vm = new ResultGridViewVM(result); NotificationProvider.ShowDialog(vm); } } private async void ExportResultGrid(Result result) { if (result != null && result.IsValueArray && (result.Value as IList).Count > 0) { var r = await StorageProvider.SaveFile("Export procedure result", "CSV Files|*.csv", result.Name + ".csv", ".csv"); if (r.Confirmed) { using (NotificationProvider.PushTaskItem("Exporting csv file...")) { try { await Task.Factory.StartNew(() => { List values = (result.Value as IEnumerable).Cast().ToList(); var model = values.First(); if (model.GetType().IsValueTypeOrString()) { CsvFile csvFile = new CsvFile(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = new List() { "Values" } }); foreach (var value in values) { csvFile.Append(new CsvRow() { V0 = value }); } csvFile.Dispose(); } else { List columns = new List(); //prop columns foreach (var prop in model.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { var att = prop.GetCustomAttribute(); columns.Add(new CsvColumn() { Name = prop.Name, Description = att != null ? att.Description : prop.Name, IsProperty = true, PropertyInfo = prop }); } //field columns foreach (var field in model.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance)) { var att = field.GetCustomAttribute(); columns.Add(new CsvColumn() { Name = field.Name, Description = att != null ? att.Description : field.Name, FieldInfo = field }); } CsvFile csvFile = new CsvFile(new CsvDestination(r.SelectedItem), new CsvDefinition() { Columns = columns.Select(x => x.Description).ToList() }); List rowProps = typeof(CsvRow).GetProperties().ToList(); foreach (var value in values) { CsvRow row = new CsvRow(); for (int i = 0; i < columns.Count; i++) { var column = columns[i]; rowProps[i].SetValue(row, column.GetValue(value)); } csvFile.Append(row); } csvFile.Dispose(); } }); NotificationProvider.PushSnackbarItem(MessageType.Success, "CSV file exported", true, "Procedure result was exported to file.\nTap to browse the file location.", TimeSpan.FromSeconds(5), null, () => { StorageProvider.ShowInExplorer(r.SelectedItem); }); } catch (Exception ex) { LogManager.Log(ex, "Error exporting csv file."); await NotificationProvider.ShowError($"Error occurred while exporting the csv file.\n{ex.FlattenMessage()}"); } } } } } } }