Skip to content

Commit

Permalink
basic export to csv functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
agailloty committed Feb 1, 2025
1 parent 2374185 commit 5b82545
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
34 changes: 34 additions & 0 deletions XpathRunner/Service/DialogService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
Expand Down Expand Up @@ -29,6 +30,37 @@ public class DialogService
return fileList;
}

public async Task SaveResultsToCsvAsync(string[] content)
{
var result = string.Join(Environment.NewLine, content);
await ExportFile(result);
}

private async Task ExportFile(string content)
{
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime
{
MainWindow.StorageProvider: { } provider
}) return;

var options = new FilePickerSaveOptions
{
Title = "Save File",
SuggestedFileName = "NewFile.txt",
DefaultExtension = "txt",
ShowOverwritePrompt = true
};

var file = await provider.SaveFilePickerAsync(options);

if (file != null)
{
await using var stream = await file.OpenWriteAsync();
await using var writer = new StreamWriter(stream);
await writer.WriteAsync(content);
}
}

private static FilePickerFileType[] AllowedFileTypes()
{
return new[]
Expand All @@ -39,4 +71,6 @@ private static FilePickerFileType[] AllowedFileTypes()
}
};
}


}
9 changes: 9 additions & 0 deletions XpathRunner/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public MainWindowViewModel()
UpdateSelectedFilesLabel();
}
};

ExportResultsCommand = new RelayCommand(async () =>
{
IsBusy = true;
var content = XpathResults.ToArray();
await _dialogService.SaveResultsToCsvAsync(content);
IsBusy = false;
});
}

#region Public properties
Expand Down Expand Up @@ -139,6 +147,7 @@ public string SelectedFilesLabel
public ICommand GetXpathResultsCommand { get; }
public ICommand AddFilesFileCommand { get; }
public ICommand RemoveFileCommand { get; }
public ICommand ExportResultsCommand { get; }

public ObservableCollection<FileInfo>? SelectedFiles
{
Expand Down
1 change: 1 addition & 0 deletions XpathRunner/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

<views:XpathResults Margin="10 0 0 0" Grid.Row="1" Grid.Column="1" IsVisible="{Binding !IsXpathResultsEmpty}"/>
<StackPanel Grid.Row="2" Grid.Column="1" IsVisible="{Binding !IsXpathResultsEmpty}" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Grid.Row="2" Content="Export as CSV" ClickMode="Press" HorizontalAlignment="Left" Margin="-10 2 0 0" Command="{Binding ExportResultsCommand}"/>
<Label Grid.Row="2" Content="Total : " FontSize="15"/>
<Label Grid.Row="2" Content="{Binding XpathResultsCount}" FontSize="15"/>
</StackPanel>
Expand Down

0 comments on commit 5b82545

Please sign in to comment.